Align a variable on the stack.
    Nicholas Wilson via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Tue Nov  3 17:14:30 PST 2015
    
    
  
On Tuesday, 3 November 2015 at 23:29:45 UTC, TheFlyingFiddle 
wrote:
> Is there a built in way to do this in dmd?
>
> Basically I want to do this:
>
> auto decode(T)(...)
> {
>    while(...)
>    {
>       T t = T.init; //I want this aligned to 64 bytes.
>    }
> }
>
>
> Currently I am using:
>
> align(64) struct Aligner(T)
> {
>    T value;
> }
>
> auto decode(T)(...)
> {
>    Aligner!T t = void;
>    while(...)
>    {
>       t.value = T.init;
>    }
> }
>
> But is there a less hacky way? From the documentation of align 
> it seems i cannot use that for this kind of stuff. Also I don't 
> want to have to use align(64) on my T struct type since for my 
> usecase I am decoding arrays of T.
>
> The reason that I want to do this in the first place is that if 
> the variable is aligned i get about a 2.5x speedup (i don't 
> really know why... found it by accident)
Note that there are two different alignments:
          to control padding between instances on the stack 
(arrays)
          to control padding between members of a struct
align(64) //arrays
struct foo
{
       align(16) short baz; //between members
       align (1) float quux;
}
your 2.5x speedup is due to aligned vs. unaligned loads and 
stores which for SIMD type stuff has a really big effect. 
Basically misaligned stuff is really slow. IIRC there was a 
(blog/paper?) of someone on a uC spending a vast amount of time 
in ONE misaligned integer assignment causing traps and getting 
the kernel involved. Not quite as bad on x86 but still with doing.
As to a less jacky solution I'm not sure there is one.
    
    
More information about the Digitalmars-d-learn
mailing list