April 14, 2009
Stewart Gordon wrote:
> Frits van Bommel wrote:
>> Stewart Gordon wrote:
> <snip>
>>> But the offset of a union member is always zero.  So what would this do?
>>
>> It should make sure the union is aligned appropriately in a containing struct, meaning U.alignof >= M.alignof for all members M. Specifying per-member alignment allows you to change that member's effect on the union's alignment.
> 
> But alignment of a member within a structure and alignment of the whole structure relative to an outer one are two distinct concepts.  What sense is there in being able to use one to control the other?
> 
>>>> Most obviously, a union U consisting of a single member x should have
>>>> U.alignof == x.alignof.
>>> <snip>
>>>
>>> Yes, by propagating the union's alignment (relative to the containing struct) to the member.
>>
>> But the union's alignment needs to be sufficient for all members, so it depends on the maximum alignment of all members.
> 
> That's only because you want to be able to attach alignments to individual members of a union.  And I still don't know why.

I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.

> 
> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?

In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
April 14, 2009
Don wrote:
> Stewart Gordon wrote:
<snip>
>> That's only because you want to be able to attach alignments to individual members of a union.  And I still don't know why.
> 
> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.

I still don't know what you mean.

>> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?
> 
> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.

If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires.  The person who creates the struct, OTOH, does know.  So why are you trying to do that person's job?

Stewart.
April 14, 2009
Frits van Bommel wrote:
> Stewart Gordon wrote:
>> Don wrote:
>>> Stewart Gordon wrote:
>> <snip>
>>>> Surely, align isn't applicable to unions at all.  IINM the members of a union, by design, start at the same offset.
>>>
>>> Not so, the alignment of each member should be respected. 
>>
>> But the offset of a union member is always zero.  So what would this do?
> 
> It should make sure the union is aligned appropriately in a containing struct, meaning U.alignof >= M.alignof for all members M. Specifying per-member alignment allows you to change that member's effect on the union's alignment.
> 
>>> Most obviously, a union U consisting of a single member x should have
>>> U.alignof == x.alignof.
>> <snip>
>>
>> Yes, by propagating the union's alignment (relative to the containing struct) to the member.
> 
> But the union's alignment needs to be sufficient for all members, so it depends on the maximum alignment of all members.

Or the least common multiple, assuming that align accepts arguments that are not powers of 2.
April 14, 2009
Christopher Wright wrote:
> Frits van Bommel wrote:
>> But the union's alignment needs to be sufficient for all members, so it depends on the maximum alignment of all members.
> 
> Or the least common multiple, assuming that align accepts arguments that are not powers of 2.

I have never seen "alignment" used to describe addresses at a multiple of anything but a power of two.

In fact, common object file formats (That I know of: ELF and LLVM bitcode) enforce this by only allowing alignments to be specified as powers of two. ELF does this by storing the log of the alignment instead of the alignment itself; LLVM currently encodes the full value for backward compatibility but asserts if this isn't a power of two (or zero) and IIRC they plan to switch to log-storage for LLVM 3.0 if and when it comes out.
April 14, 2009
Stewart Gordon wrote:
> Don wrote:
>> Stewart Gordon wrote:
> <snip>
>>> That's only because you want to be able to attach alignments to individual members of a union.  And I still don't know why.
>>
>> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.
> 
> I still don't know what you mean.
> 
>>> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?
>>
>> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
> 
> If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires.  The person who creates the struct, OTOH, does know.  So why are you trying to do that person's job?
> 
> Stewart.

You know ahead of time that you're going to use this union everywhere and that it should have proper alignment. If you still want to put the align attribute everywhere it's used instead, allow me to shoot you.

Granted, you can replace your named union with a struct of the same name containing an anonymous union, and put the align attribute on that. That wouldn't be too odious (as long as the language specification mentioned the workaround), but since you already have align for unions, why bother changing it?
April 15, 2009
Stewart Gordon wrote:
> Don wrote:
>> Stewart Gordon wrote:
> <snip>
>>> That's only because you want to be able to attach alignments to individual members of a union.  And I still don't know why.
>>
>> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.
> 
> I still don't know what you mean.

You're acting as if there's a big difference between unions and structs, and there isn't. Read the spec, the only differences are at construction, and struct literals.

>>> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?
>>
>> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
> 
> If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires. 

Yes you do. The union doesn't control the alignment of the surrounding struct. It only controls the alignment of itself inside that struct.

 The person who creates
> the struct, OTOH, does know.  

They do NOT know. Not without probing every member in the union.

So why are you trying to do that person's
> job?

No, you know what alignment the member requires.

By the way, there doesn't even need to be a surrounding struct!
An instance of the union on the stack may itself need to be aligned.
And you can even create a bare union on the heap.
April 15, 2009
Don wrote:
> Stewart Gordon wrote:
>> Don wrote:
<snip>
>>> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.
>>
>> I still don't know what you mean.
> 
> You're acting as if there's a big difference between unions and structs, and there isn't. Read the spec, the only differences are at construction, and struct literals.

No, you have to add to that the differences inherited from C.  Moreover, what difference could possibly be bigger than that between the basic essence of structs and the basic essence of unions?

http://www.digitalmars.com/d/1.0/struct.html
"They work like they do in C, with the following exceptions:"

It's true that "alignment can be explicitly specified" is in there.  But I'm not sure that Walter really meant it to apply to unions as well as structs.  Especially given that it makes no comment (that I've found) on what align is meant to do in a union.  Moreover,

http://www.digitalmars.com/d/1.0/attribute.html#align
"Specifies the alignment of struct members."

>>>> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?
>>>
>>> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
>>
>> If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires. 
> 
> Yes you do. The union doesn't control the alignment of the surrounding struct. It only controls the alignment of itself inside that struct.

I thought that was meant to be controlled by an align applied to the union instance as a member of the struct, rather than by an align applied to the union itself.  Only in the case of anonymous unions are they one and the same.

>> The person who creates the struct, OTOH, does know.
> 
> They do NOT know. Not without probing every member in the union.

?? ISTM all they really need to know is the overall size of the union, which is equal to the size of the union's largest member (which is easy to find out).

> So why are you trying to do that person's
>> job?
> 
> No, you know what alignment the member requires.
<snip>

What are the cases in which a member _requires_ a certain alignment?

OK, so there's one case: pointers in order to make sure the GC works properly.  But this is absolute alignment.  Maybe it's just me, but given that talk of alignment is nearly always in relation to structs, I'd made it out to be talking about relative alignment.  Maybe the spec just needs to be clearer....

Stewart.
April 16, 2009
Stewart Gordon wrote:
> Don wrote:
>> Stewart Gordon wrote:
>>> Don wrote:
> <snip>
>>>> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.
>>>
>>> I still don't know what you mean.
>>
>> You're acting as if there's a big difference between unions and structs, and there isn't. Read the spec, the only differences are at construction, and struct literals.
> 
> No, you have to add to that the differences inherited from C.  Moreover,
> what difference could possibly be bigger than that between the basic essence of structs and the basic essence of unions?

In the compiler, struct and union share almost all of their code.
Basically, a union is just a struct where all the elements are on top of each other. Which is a tiny difference compared (say) to the difference between structs and classes. For example, this works:

union foo(T) {
public:
   static int bar(int x) { return x*2; }
}

void main()
{
  assert (foo!(int).bar(7)==14);
}


> 
> http://www.digitalmars.com/d/1.0/struct.html
> "They work like they do in C, with the following exceptions:"
> 
> It's true that "alignment can be explicitly specified" is in there.  But I'm not sure that Walter really meant it to apply to unions as well as structs.  Especially given that it makes no comment (that I've found) on what align is meant to do in a union. 

In a struct, align(4) int X; means, pad with zero bytes until the address you're up to is a multiple of 4, then put the int at that address. The address for the next element is the first byte past the int.
In a union, align(4) int X; means, pad with zero bytes until the address you're up to is a multiple of 4, then put the int at that address.  The address for the next element is the first byte of the union.

 Moreover,
> 
> http://www.digitalmars.com/d/1.0/attribute.html#align
> "Specifies the alignment of struct members."
> 
>>>>> If you want a union to have a certain alignment relative to a struct in which it's contained, in what cases is it not sufficient to put the align attribute on the union's instance in the struct?
>>>>
>>>> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
>>>
>>> If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires. 
>>
>> Yes you do. The union doesn't control the alignment of the surrounding struct. It only controls the alignment of itself inside that struct.
> 
> I thought that was meant to be controlled by an align applied to the union instance as a member of the struct, rather than by an align applied to the union itself.  Only in the case of anonymous unions are they one and the same.
> 
>>> The person who creates the struct, OTOH, does know.
>>
>> They do NOT know. Not without probing every member in the union.
> 
> ?? ISTM all they really need to know is the overall size of the union, which is equal to the size of the union's largest member (which is easy to find out).
> 
>> So why are you trying to do that person's
>>> job?
>>
>> No, you know what alignment the member requires.
> <snip>
> 
> What are the cases in which a member _requires_ a certain alignment?

Static arrays, eg float[4]. Must be aligned if you want to use the movaps instruction (which is 4X faster than the unaligned instruction).

Alignment requirements are rare, even for structs.

> 
> OK, so there's one case: pointers in order to make sure the GC works properly.  But this is absolute alignment.  Maybe it's just me, but given that talk of alignment is nearly always in relation to structs, I'd made it out to be talking about relative alignment.  Maybe the spec just needs to be clearer....
> 
> Stewart.

Probably. Unions tend to get forgotten.
April 16, 2009
On Thu, Apr 16, 2009 at 12:34 PM, Don <nospam@nospam.com> wrote:
> Stewart Gordon wrote:
>>
>> Don wrote:
>>>
>>> Stewart Gordon wrote:
>>>>
>>>> Don wrote:
>>
>> <snip>
>>>>>
>>>>> I'm not sure why you think unions are so different to structs. They are identical in most respects -- including requirements for alignment of members.
>>>>
>>>> I still don't know what you mean.
>>>
>>> You're acting as if there's a big difference between unions and structs, and there isn't. Read the spec, the only differences are at construction, and struct literals.
>>
>> No, you have to add to that the differences inherited from C.  Moreover, what difference could possibly be bigger than that between the basic essence of structs and the basic essence of unions?
>
> In the compiler, struct and union share almost all of their code. Basically, a union is just a struct where all the elements are on top of each other. Which is a tiny difference compared (say) to the difference between structs and classes. For example, this works:
>
> union foo(T) {
> public:
>   static int bar(int x) { return x*2; }
> }
>
> void main()
> {
>  assert (foo!(int).bar(7)==14);
> }
>
>
>>
>> http://www.digitalmars.com/d/1.0/struct.html
>> "They work like they do in C, with the following exceptions:"
>>
>> It's true that "alignment can be explicitly specified" is in there.  But I'm not sure that Walter really meant it to apply to unions as well as structs.  Especially given that it makes no comment (that I've found) on what align is meant to do in a union.
>
> In a struct, align(4) int X; means, pad with zero bytes until the address
> you're up to is a multiple of 4, then put the int at that address. The
> address for the next element is the first byte past the int.
> In a union, align(4) int X; means, pad with zero bytes until the address
> you're up to is a multiple of 4, then put the int at that address.  The
> address for the next element is the first byte of the union.
>
>  Moreover,
>>
>> http://www.digitalmars.com/d/1.0/attribute.html#align "Specifies the alignment of struct members."
>>
>>>>>> If you want a union to have a certain alignment relative to a struct
>>>>>> in which it's contained, in what cases is it not sufficient to put the align
>>>>>> attribute on the union's instance in the struct?
>>>>>
>>>>> In cases where you don't know what the containing struct is. The union may just be a type returned from a template, for example.
>>>>
>>>> If you don't know what the containing struct is, you probably also don't know what member alignment that struct requires.
>>>
>>> Yes you do. The union doesn't control the alignment of the surrounding struct. It only controls the alignment of itself inside that struct.
>>
>> I thought that was meant to be controlled by an align applied to the union instance as a member of the struct, rather than by an align applied to the union itself.  Only in the case of anonymous unions are they one and the same.
>>
>>>> The person who creates the struct, OTOH, does know.
>>>
>>> They do NOT know. Not without probing every member in the union.
>>
>> ?? ISTM all they really need to know is the overall size of the union, which is equal to the size of the union's largest member (which is easy to find out).
>>
>>> So why are you trying to do that person's
>>>>
>>>> job?
>>>
>>> No, you know what alignment the member requires.
>>
>> <snip>
>>
>> What are the cases in which a member _requires_ a certain alignment?
>
> Static arrays, eg float[4]. Must be aligned if you want to use the movaps instruction (which is 4X faster than the unaligned instruction).

So now we just need align(N) for the storage as well as field offsets,
to actually make that useful.

>
> Alignment requirements are rare, even for structs.
>
>>
>> OK, so there's one case: pointers in order to make sure the GC works properly.  But this is absolute alignment.  Maybe it's just me, but given that talk of alignment is nearly always in relation to structs, I'd made it out to be talking about relative alignment.  Maybe the spec just needs to be clearer....
>>
>> Stewart.
>
> Probably. Unions tend to get forgotten.
>
1 2 3
Next ›   Last »