www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - need help to get bitoffsetof bitwidth for clang

reply Dakota <dakota gmail.com> writes:
I want to get `bitoffsetof` and `bitwidth` in clang, to verify 
the d memory layout.

I try this but not work:

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


get this error:

```sh
cannot compute offset of bit-field 'field'
```
Jul 01
parent reply Tim <tim.dlang t-online.de> writes:
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:

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


 get this error:

 ```sh
 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.
Jul 03
parent Dakota <dakota gmail.com> writes:
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`
Jul 04