alignof

View: New views
9 Messages — Rating Filter:   Alert me  

alignof

by pip88nl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello developers and users of Aldor,

I keep wondering what to do with this:

# define alignof(T)             (sizeof(struct {char a; T b;}) - sizeof(T))

as it's incorrect. When the compiler adds padding to the structure, this
macro may return a negative value, breaking everything.

--
Pippijn van Steenhoven


_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

signature.asc (196 bytes) Download Attachment

Re: alignof

by Stephen Watt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Pippijn,

Can you give an example of a compiler and input for which the macro is incorrect ?

-- Stephen


On Fri, Mar 21, 2008 at 10:43:44PM +0100, Pippijn van Steenhoven wrote:

> Hello developers and users of Aldor,
>
> I keep wondering what to do with this:
>
> # define alignof(T)             (sizeof(struct {char a; T b;}) - sizeof(T))
>
> as it's incorrect. When the compiler adds padding to the structure, this
> macro may return a negative value, breaking everything.
>
> --
> Pippijn van Steenhoven



> _______________________________________________
> Aldor-l mailing list
> Aldor-l@...
> http://aldor.org/mailman/listinfo/aldor-l_aldor.org


_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by Bill Page-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 3/21/08, Pippijn van Steenhoven wrote:
>
>  I keep wondering what to do with this:
>
>  # define alignof(T)             (sizeof(struct {char a; T b;}) - sizeof(T))
>
>  as it's incorrect. When the compiler adds padding to the structure,
> this macro may return a negative value, breaking everything.
>

Can you given an example where this situation occurs?

Is it desirable to include a check and error exit in this macro to
make sure that this is not happening? Can such a test be implemented
without severely impacting the performance of the compiler? If not,
perhaps a "debug" build option is desirable.

Regards,
Bill Page.

_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by pip88nl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

First of all, it is not important whether or not there is an example. The
compiler is free to add padding to a struct { char a; short b; }; and then the
code is broken. On x86, it would make sense to align the short to 4 bytes and
the struct itself, too, leading to an alignof (that struct) of 0. I can
probably live with it as long as no issues arise. The problem is really just,
the code is broken and it would be good if a better alternative was found.

--
Pippijn van Steenhoven


_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

signature.asc (196 bytes) Download Attachment

Re: alignof

by Gabriel Dos Reis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Mar 21, 2008 at 4:56 PM, Bill Page <bill.page@...> wrote:
> On 3/21/08, Pippijn van Steenhoven wrote:
>  >
>  >  I keep wondering what to do with this:
>  >
>  >  # define alignof(T)             (sizeof(struct {char a; T b;}) - sizeof(T))

I suspect that on most platformd of interest to Aldor, GCC is
running, therefore GCC's documented extension __alignof__
is a better option.

FWIW, the next version of C++ will have a similar standard
facility (inspired largely from existing experience with major
C and C++ compilers).

_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by Stephen Watt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Mar 21, 2008 at 11:22:36PM +0100, Pippijn van Steenhoven wrote:
> First of all, it is not important whether or not there is an example. The
> compiler is free to add padding to a struct { char a; short b; }; and then the
> code is broken. On x86, it would make sense to align the short to 4 bytes and
> the struct itself, too, leading to an alignof (that struct) of 0. I can
> probably live with it as long as no issues arise. The problem is really just,
> the code is broken and it would be good if a better alternative was found.
>
> --
> Pippijn van Steenhoven


On 3/21/08, Pippijn van Steenhoven wrote:
>
>  I keep wondering what to do with this:
>
>  # define alignof(T)             (sizeof(struct {char a; T b;}) - sizeof(T))
>                
>  as it's incorrect. When the compiler adds padding to the structure,
> this macro may return a negative value, breaking everything.

-----------------------------------------------------

Pippijn,

As Gabriel pointed out, we can have conditional implementation in terms of
__alignof__.  There will be some platforms without it, however, so this
discussion remains useful.

The reason I ask for an example where it is wrong is because I don't understand
your statement that the macro can return a negative value.  

Can you please explain?

I do not recall whether the C standard allows the compiler to re-order
fields of a structure.  

If the compiler does not re-order fields, then I believe
sizeof(struct {char a; T b; }) is strictly greater than sizeof(T)
so the value of the macro will always be positive.

If the compiler can re-order fields, then I believe
sizeof(struct {char a; T b; }) is greater than or equal to sizeof(T)
so the value of the macro will always be non-negative, but possibly zero.

So if this is what you are worried about, it could probably be fixed as
something along the lines of


if have __alignof__
  use that
else
  correct alignof = max(old alignof, 1)


-- Stephen






_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by Gabriel Dos Reis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stephen Watt <watt@...> writes:

| Pippijn,
|
| Can you give an example of a compiler and input for which the macro is incorrect ?

GCC, on x86.  Take T=double.  On my machine (x86), I get 8 with the macro.

The snag is that the alignment of double is not the same when it is a
member of a struct (alignment 4), or when it is the type of a
standalone datum (8).   So, whether you get the right answer depends
on the context of use.

I knew this thread was ringing a bell to me.

  http://gcc.gnu.org/ml/gcc/2003-04/msg01178.html


So, the exact value matters only if you're doing something very low-level
that it does matter whether the double is a member of a struct or not.
I do not know the details of Aldor source code, so only you and
Pippijn can conclude :-)

-- Gaby

_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by Stephen Watt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, we use alignof only for some low level things.  I don't recall off hand
which ones survive to this day, but they have included understanding pointer alingment
to determine how many passes to make on the stack for GC  and  some structure packing.

-- Stephen


On Fri, Mar 21, 2008 at 06:58:05PM -0500, Gabriel Dos Reis wrote:

> Stephen Watt <watt@...> writes:
>
> | Pippijn,
> |
> | Can you give an example of a compiler and input for which the macro is incorrect ?
>
> GCC, on x86.  Take T=double.  On my machine (x86), I get 8 with the macro.
>
> The snag is that the alignment of double is not the same when it is a
> member of a struct (alignment 4), or when it is the type of a
> standalone datum (8).   So, whether you get the right answer depends
> on the context of use.
>
> I knew this thread was ringing a bell to me.
>
>   http://gcc.gnu.org/ml/gcc/2003-04/msg01178.html
>
>
> So, the exact value matters only if you're doing something very low-level
> that it does matter whether the double is a member of a struct or not.
> I do not know the details of Aldor source code, so only you and
> Pippijn can conclude :-)
>
> -- Gaby

_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org

Re: alignof

by Gabriel Dos Reis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Pippijn van Steenhoven <pip88nl@...> writes:

| First of all, it is not important whether or not there is an example. The
| compiler is free to add padding to a struct { char a; short b; }; and then the
| code is broken. On x86, it would make sense to align the short to 4 bytes and
| the struct itself, too, leading to an alignof (that struct) of 0. I can
| probably live with it as long as no issues arise. The problem is really just,
| the code is broken and it would be good if a better alternative was found.
|

It matters that there is a real example, otherwise we would be
elaborating in the vacuum..
All useful C programs I know of rely on some form of implementation
defined or undefined behaviour of the C abstract machine.  That is just
real life.  Even the famous `hello world' program relies on
implementation defined aspects to produce anything useful.

-- Gaby

_______________________________________________
Aldor-l mailing list
Aldor-l@...
http://aldor.org/mailman/listinfo/aldor-l_aldor.org