digitalmars.D - struct field alignment
- Walter Bright (6/6) Oct 17 2010 http://www.digitalmars.com/d/2.0/attribute.html#align
- BCS (5/16) Oct 17 2010 If that's the case, then having a number in there seems silly: how about...
- Walter Bright (2/4) Oct 17 2010 Another keyword.
- Craig Black (4/8) Oct 18 2010 Not that it's a big deal but another idea align(pack), which could be
- Denis Koroskin (8/16) Oct 17 2010 On some platforms it is desired to align struct Vec { float[4] data; } o...
- BCS (6/11) Oct 17 2010 At a guess, that is aligning the structure in memory, not the members in...
- Denis Koroskin (3/12) Oct 17 2010 align(default) could also work.
- Walter Bright (3/9) Oct 17 2010 align is shorter than align(C).
- Jonathan M Davis (3/16) Oct 17 2010 It would be clearer though.
- Emil Madsen (5/25) Oct 18 2010 --
- Robert Jacques (18/26) Oct 17 2010 I mainly do GP-GPU work using NVIDIA's CUDA + D. Passing data into and o...
- Walter Bright (7/12) Oct 17 2010 I'm painfully aware that align(8)/(16) don't work on the 32 bit targets....
- Robert Jacques (7/19) Oct 21 2010 Cool. I understand the performance issue, but would it be possible for t...
- bearophile (9/13) Oct 18 2010 On the other hand stack-allocated structs and arrays that need to be 16-...
- Fawzi Mohamed (9/17) Oct 18 2010 I fully agree that those two are the only really useful alignment
- Trass3r (2/10) Oct 21 2010 Isn't the first case unnecessary if C ABI alignment is the default one
http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.
Oct 17 2010
Hello Walter,http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.If that's the case, then having a number in there seems silly: how about align and pack? -- ... <IXOYE><
Oct 17 2010
BCS wrote:If that's the case, then having a number in there seems silly: how about align and pack?Another keyword.
Oct 17 2010
Not that it's a big deal but another idea align(pack), which could be implemented without making pack a keyword. "Walter Bright" <newshound2 digitalmars.com> wrote in message news:i9gbr2$2c11$1 digitalmars.com...BCS wrote:If that's the case, then having a number in there seems silly: how about align and pack?Another keyword.
Oct 18 2010
On Mon, 18 Oct 2010 06:00:49 +0400, Walter Bright <newshound2 digitalmars.com> wrote:http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.On some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes. I don't recall why now, but I know for sure we do that for all platforms (other than PC maybe). I guess that has something to do with vector operations. Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?
Oct 17 2010
Hello Denis,On some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes.At a guess, that is aligning the structure in memory, not the members in the struct. I think the 2nd is the question here.Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?vote++; -- ... <IXOYE><
Oct 17 2010
On Mon, 18 Oct 2010 06:22:32 +0400, BCS <none anon.com> wrote:Hello Denis,Yes, I was talking about stack alignment.On some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes.At a guess, that is aligning the structure in memory, not the members in the struct. I think the 2nd is the question here.align(default) could also work.Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?vote++;
Oct 17 2010
Denis Koroskin wrote:On some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes. I don't recall why now, but I know for sure we do that for all platforms (other than PC maybe). I guess that has something to do with vector operations.I was thinking of special casing such arrays to be 16 byte aligned by default.Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?align is shorter than align(C).
Oct 17 2010
On Sunday 17 October 2010 20:07:26 Walter Bright wrote:Denis Koroskin wrote:It would be clearer though. - Jonathna M DavisOn some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes. I don't recall why now, but I know for sure we do that for all platforms (other than PC maybe). I guess that has something to do with vector operations.I was thinking of special casing such arrays to be 16 byte aligned by default.Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?align is shorter than align(C).
Oct 17 2010
About the 16byte alignment, of 4floats, its mostlikely for SSE :) On 18 October 2010 04:16, Denis Koroskin <2korden gmail.com> wrote:On Mon, 18 Oct 2010 06:00:49 +0400, Walter Bright < newshound2 digitalmars.com> wrote: http://www.digitalmars.com/d/2.0/attribute.html#align-- // Yours sincerely // Emil 'Skeen' MadsenOver time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.On some platforms it is desired to align struct Vec { float[4] data; } on 16 bytes. I don't recall why now, but I know for sure we do that for all platforms (other than PC maybe). I guess that has something to do with vector operations. Bikeshed note: how about align(C) to be consistent with both align(1) and extern(C)?
Oct 18 2010
On Sun, 17 Oct 2010 22:00:49 -0400, Walter Bright <newshound2 digitalmars.com> wrote:http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.I mainly do GP-GPU work using NVIDIA's CUDA + D. Passing data into and out of the GPU requires conforming to align(8) and align(16) structs as well as structs that contain align(8)/align(16) structs. At first, I tried using align(8)/align(16) + .alignof, only to find out that those weren't supported. Currently, I use an enum flag and a bunch of packing/unpacking routines in order to send/access data. Maintaining the correct alignment with composite structs manually is a difficult/brittle endeavor; its fine for interfacing with a stable code base, but not for development where things are constantly changing. Also, CUDA requires that the programmer maintain the proper function call alignment, adding another place where one has to remember to pad correctly. Although I have a solution that works well for me, the one thing I lament about not having a canonical D way of expression align(8)/align(16), even at only a meta-information level, is that if phobos gets a small vector library, I can't use it and conversely I'm not motivated to improve/submit my own small vector library to phobos.
Oct 17 2010
Robert Jacques wrote:Although I have a solution that works well for me, the one thing I lament about not having a canonical D way of expression align(8)/align(16), even at only a meta-information level, is that if phobos gets a small vector library, I can't use it and conversely I'm not motivated to improve/submit my own small vector library to phobos.I'm painfully aware that align(8)/(16) don't work on the 32 bit targets. I've been reluctant to fix that because it involves some performance degradation (keeping the stack so aligned requires the insertion of stack adjustment instructions here and there). With the 64 bit target, however, the C ABI will force the issue. It'll support those alignments.
Oct 17 2010
On Sun, 17 Oct 2010 23:38:34 -0400, Walter Bright <newshound2 digitalmars.com> wrote:Robert Jacques wrote:Cool. I understand the performance issue, but would it be possible for the internal alignment of a struct to be correct? i.e. struct A { float x; float2 point; } would be properly aligned internally: struct A { float x; int padding; float2 point; }. That way, 32-bit programs could read binary files from 64-bit programs and vice-versa.Although I have a solution that works well for me, the one thing I lament about not having a canonical D way of expression align(8)/align(16), even at only a meta-information level, is that if phobos gets a small vector library, I can't use it and conversely I'm not motivated to improve/submit my own small vector library to phobos.I'm painfully aware that align(8)/(16) don't work on the 32 bit targets. I've been reluctant to fix that because it involves some performance degradation (keeping the stack so aligned requires the insertion of stack adjustment instructions here and there). With the 64 bit target, however, the C ABI will force the issue. It'll support those alignments.
Oct 21 2010
Walter Bright:I'm painfully aware that align(8)/(16) don't work on the 32 bit targets. I've been reluctant to fix that because it involves some performance degradation (keeping the stack so aligned requires the insertion of stack adjustment instructions here and there).On the other hand stack-allocated structs and arrays that need to be 16-byte aligned may speed up the code if used with certain SSE+ instructions. If it is something that must be used for the whole program then you may add a compiler switch to activate stack alignment on request. If it may work for a single compilation unit then use something like: align(16) module foobar; If it may work for a single function, then then you may add the alignment for functions: align(16) int foo(int x) {... Bye, bearophile
Oct 18 2010
On 18-ott-10, at 04:00, Walter Bright wrote:http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.I fully agree that those two are the only really useful alignment properties for fields in a struct. Another thing that is needed is specifying the alignment of the struct itself, for example if it contains fields that one wants to use with SSE. So one should have the freedom to specify the wanted alignment for the structure Fawzi
Oct 18 2010
http://www.digitalmars.com/d/2.0/attribute.html#align Over time, it has become clear to me that there are only two useful alignments: align // set to whatever the C ABI alignment is align(1) // pack everything in, no alignment padding I think any other alignments should be deprecated. Note that with align(1), any alignment is achievable simply by adding in byte fields where desired.Isn't the first case unnecessary if C ABI alignment is the default one anyway?
Oct 21 2010