SapphireDensetsu / ypsilon

Automatically exported from code.google.com/p/ypsilon
Other
0 stars 0 forks source link

alignment is different than array stride #63

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I see that the alignment of platform's data types is determined with the
__alignof__ operator. Then these values are used as element strides in
arrays. I just want to signal that this usage may turn out to be incorrect.

IMHO the safe way to compute the array stride of a typedef is to run the
following test program:

THE_TYPEDEF x[3];

printf("%ld", (long int)((unsigned char *)&(x[2])-
                         (unsigned char *)&(x[1])));

As an example on a i686-pc-linux-gnu:

sizeof(double)         = 8
alignment of double    = 4
array stride of double = 8

where the alignment is computed by running the following test program
(adapted from the sources of GNU Autoconf):

#ifndef offsetof
# define offsetof(type, member) \
    ((char *) &((type *) 0)->member - (char *) 0)
#endif

typedef struct { char x; THE_TYPEDEF y; } ac__type_alignof_;
printf("%ld", (long int)offsetof(ac__type_alignof_,y));

Now, Ypsilon has no configuration script, so its up to you to decide about
the inclusion of the required (small) type declarations somewhere in the
Ypsilon sources.

Original issue reported on code.google.com by mrc....@gmail.com on 18 Dec 2008 at 10:58

GoogleCodeExporter commented 8 years ago
Sorry for the grammar error in the title ... :/

Original comment by mrc....@gmail.com on 18 Dec 2008 at 11:01

GoogleCodeExporter commented 8 years ago
Thank you for your valuable input.
Now I understand GNU __alignof__ returns *recommended* alignment of a type. :o

I have fixed it and trunk directory is updated to revision r326.
New version uses ANSI C offsetof macro as you suggested. :)

Please try. Thank you!
-- fujita

$ uname -a
Linux ubuntu-core2 2.6.24-22-generic #1 SMP Mon Nov 24 18:32:42 UTC 2008 i686 
GNU/Linux
$ ypsilon
Ypsilon 0.9.6-trunk/r326 Copyright (c) 2008 Y.Fujita, LittleWing Company 
Limited.
> (import (ffi))
> alignof:short
2
> alignof:int
4
> alignof:long
4
> alignof:void*
4
> alignof:float
4
> alignof:double
4
> alignof:int8_t
1
> alignof:int16_t
2
> alignof:int32_t
4
> alignof:int64_t
4

Original comment by y.fujita...@gmail.com on 19 Dec 2008 at 11:24

GoogleCodeExporter commented 8 years ago
New version 0.9.6-update3 has released and I close this issue. Thank you again!
-- fujita

Original comment by y.fujita...@gmail.com on 23 Dec 2008 at 10:24