size_t vs uintptr_t

Guillaume Boucher via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 14 15:38:40 PDT 2016


On Tuesday, 14 June 2016 at 21:59:32 UTC, Walter Bright wrote:
> A related issue is I see much code of the form:
>
>     cast(size_t)ptr & 3
>
> to check alignment. A better (possibly faster) method is:
>
>     cast(uint)ptr & 3
>
> because even 64 bit CPUs often operate faster with 32 bit 
> operations (thanks to some research by Andrei).

Isn't it guaranteed that x.sizeof >= x.alignof?  (At least it is 
in C and C++.)  So the alignment should be of type size_t and not 
of type uintptr_t.

Also in general cast(uint)ptr%alignment is wrong since alignment 
does not need to be 32 bit.  However, cast(size_t)ptr%alignment 
is be correct in any case.


More information about the Digitalmars-d mailing list