Thread overview
need help to get bitoffsetof bitwidth for clang
Jul 02
Dakota
Jul 03
Tim
Jul 05
Dakota
July 02

I want to get bitoffsetof and bitwidth in clang, to verify the d memory layout.

I try this but not work:

#define offsetof_bit(type, member) ((offsetof(type, member) * 8) + __builtin_ctz(((type *)0)->member))

get this error:

cannot compute offset of bit-field 'field'
July 03

On Tuesday, 2 July 2024 at 05:34:19 UTC, Dakota wrote:

>

I want to get bitoffsetof and bitwidth in clang, to verify the d memory layout.

I try this but not work:

#define offsetof_bit(type, member) ((offsetof(type, member) * 8) + __builtin_ctz(((type *)0)->member))

get this error:

cannot compute offset of bit-field 'field'

If you only want to see the layout, you can use the following command to dump it:

clang test.c -Xclang -fdump-record-layouts -c

File test.c could contain the following:

struct S
{
    int a : 3;
    int b : 29;
};
struct S dummy;

The above clang command would then output:

*** Dumping AST Record Layout
         0 | struct S
     0:0-2 |   int a
    0:3-31 |   int b
           | [sizeof=4, align=4]

Inside a C/C++ program you could initialize a struct with all 0xff, set one bitfield to zero and look at the bits. I have used this to compare the bitfield layout between D and C++ in a test added in https://github.com/dlang/dmd/pull/16590.

July 05

On Wednesday, 3 July 2024 at 21:47:32 UTC, Tim wrote:

>

If you only want to see the layout, you can use the following command to dump it:

clang test.c -Xclang -fdump-record-layouts -c

File test.c could contain the following:

struct S
{
    int a : 3;
    int b : 29;
};
struct S dummy;

The above clang command would then output:

*** Dumping AST Record Layout
         0 | struct S
     0:0-2 |   int a
    0:3-31 |   int b
           | [sizeof=4, align=4]

Inside a C/C++ program you could initialize a struct with all 0xff, set one bitfield to zero and look at the bits. I have used this to compare the bitfield layout between D and C++ in a test added in https://github.com/dlang/dmd/pull/16590.

Thanks for the tips.

Then I guess there is no easy way to get equivalent in c for bitoffsetof and bitwidth