bit fields

Johan j at j.nl
Mon Aug 30 15:58:50 UTC 2021


On Monday, 30 August 2021 at 11:07:31 UTC, Iain Buclaw wrote:
> On Monday, 30 August 2021 at 10:43:33 UTC, Steven Schveighoffer 
> wrote:
>> On 8/30/21 6:40 AM, Johan wrote:
>>> 
>>> I think it's worthwhile to create a random testcase generator 
>>> and let it run (+ execute the test) for a day or two...
>>> 
>>
>> Max had said he was going to try and cobble something like 
>> this together during beerconf.
>>
>
> I had raised a bug report about doing such a thing a couple 
> months back too.
>
> https://issues.dlang.org/show_bug.cgi?id=22074

Something like that, but then specific for bit fields that 
includes execution of C/D code, something like this:

1. Randomly generate C file with a struct+bitfields type 
definition:
```c
struct Test {
   {<empty>|some __alignment...} {<empty>|signed|unsigned} 
{int|char|long|size_t|float|...} member1 {<empty>|:<some number 
0..bitwidth of the storage type};
   ... repeat for member2, member3, random number of members
}

void resetMember1_C(Test* t)
{
   t->member1 = 0;
}
...repeat for how many members there are

size_t sizeofTest_C()
{
    return sizeof(Test);
}
```

2. While generating the C file, you also generate a D file:
```d
extern(C)
struct Test {
// ofcourse corresponding to the generated C type!
   {<empty>|align(...)} 
{int|uint|bool|byte|char|long|size_t|float|...} member1 
{<empty>|:<some number 0..bitwidth of the storage type};
   ... repeat for member2, member3, random number of members
}

extern(C) void resetMember1_C(Test* t);
void resetMember1_D(Test* t)
{
   t.member1 = 0;
}
...repeat for how many members there are

extern(C) size_t sizeofTest_C();
size_t sizeofTest_D()
{
    return Test.sizeof();
}

void main()
{
   assert(sizeofTest_C() == sizeofTest_D());

   foreach(i; number of members in Test) {
      Test s_C;
      Test s_D;
      memset(&s_C, 0xFF, Test.sizeof);
      memset(&s_D, 0xFF, Test.sizeof);
      resetMemberi_C(&s_C);
      resetMemberi_D(&s_D);
      assert(memcmp(&s_C, &s_D, Test.sizeof));
   }
}
```

3. Compile and run at -O0 and -O3 on every arch supported. Make 
sure that asserts are not discarded at high optimization level.

And then loop&repeat these steps for a long time.

cheers,
   Johan



More information about the Digitalmars-d mailing list