Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 07, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
07.12.2010 11:58, Jonathan M Davis пишет:
> Okay. I'm trying to get some C code to be properly callable from some D code,
> which naturally involves some extern(C) blocks. One of the types that I have to
> deal with looks something like this:
>
> typedef struct
> {
> unsigned i:1;
> } my_struct;
>
> I have no idea how to do such a bitfield in D. Does a std.bitmanip.bitfield work?
> I wouldn't have thought so, but I don't know. What would be the proper way to
> create a properly compatible struct in D?
>
> - Jonathan M Davis
>
I'm under the impression that
struct my_struct
{
mixin(bitfields!(
uint, "i", 1,
uint, "", 31));
}
should do the trick.
|
December 07, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
I haven't tried but maybe htod can help, try copypasting the code to a header file and run it through htod. There's also some flags htod can take to simplify the output, iirc. But maybe htod isn't clever enough to do bitfields..
On 12/7/10, Stanislav Blinov <blinov@loniir.ru> wrote:
> 07.12.2010 11:58, Jonathan M Davis пишет:
>> Okay. I'm trying to get some C code to be properly callable from some D
>> code,
>> which naturally involves some extern(C) blocks. One of the types that I
>> have to
>> deal with looks something like this:
>>
>> typedef struct
>> {
>> unsigned i:1;
>> } my_struct;
>>
>> I have no idea how to do such a bitfield in D. Does a
>> std.bitmanip.bitfield work?
>> I wouldn't have thought so, but I don't know. What would be the proper way
>> to
>> create a properly compatible struct in D?
>>
>> - Jonathan M Davis
>>
> I'm under the impression that
>
> struct my_struct
> {
> mixin(bitfields!(
> uint, "i", 1,
> uint, "", 31));
> }
>
> should do the trick.
>
|
December 07, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | Stanislav Blinov:
> I'm under the impression that
>
> struct my_struct
> {
> mixin(bitfields!(
> uint, "i", 1,
> uint, "", 31));
> }
>
> should do the trick.
But bitfields in C may not use the same alignments used by bitfields!() on DMD so I think you have to test the sanity of the whole thing for each combination of D compiler, operating system and C compiler.
Bye,
bearophile
|
December 07, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | bearophile wrote:
> Stanislav Blinov:
>
>> I'm under the impression that
>>
>> struct my_struct
>> {
>> mixin(bitfields!(
>> uint, "i", 1,
>> uint, "", 31));
>> }
>>
>> should do the trick.
>
> But bitfields in C may not use the same alignments used by bitfields!() on DMD so I think you have to test the sanity of the whole thing for each combination of D compiler, operating system and C compiler.
>
> Bye,
> bearophile
hm... what alignments are you talking about? bitfields template uses the smallest suitable type for all fields, which is uint for the above struct. The trouble may come from bit order, mayhaps. bitfields positions fields in LSb order.
|
December 07, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | Stanislav Blinov wrote: >> But bitfields in C may not use the same alignments used by >> bitfields!() on DMD so I think you have to test the sanity of the >> whole thing for each combination of D compiler, operating system and C >> compiler. > > hm... what alignments are you talking about? bitfields template uses the > smallest suitable type for all fields, which is uint for the above > struct. The trouble may come from bit order, mayhaps. bitfields > positions fields in LSb order. There is almost no guarantee in C (and C++) on how the bitfields are layed out. I had implemented a C++ template solution once just to overcome that lack of layout. We needed to have our bits layed out exactly in certain ways. Ali |
December 08, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic wrote:
> I haven't tried but maybe htod can help, try copypasting the code to a
> header file and run it through htod. There's also some flags htod can
> take to simplify the output, iirc. But maybe htod isn't clever enough
> to do bitfields..
htod does bitfields. Give it a try!
|
December 08, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On 12/8/10, Walter Bright <newshound2@digitalmars.com> wrote:
> Andrej Mitrovic wrote:
>> I haven't tried but maybe htod can help, try copypasting the code to a header file and run it through htod. There's also some flags htod can take to simplify the output, iirc. But maybe htod isn't clever enough to do bitfields..
>
> htod does bitfields. Give it a try!
>
Cool! But, when is "ctod.exe" coming out? :p
|
December 08, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
On 12/8/10, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 12/8/10, Walter Bright <newshound2@digitalmars.com> wrote:
>> Andrej Mitrovic wrote:
>>> I haven't tried but maybe htod can help, try copypasting the code to a header file and run it through htod. There's also some flags htod can take to simplify the output, iirc. But maybe htod isn't clever enough to do bitfields..
>>
>> htod does bitfields. Give it a try!
>>
>
> Cool! But, when is "ctod.exe" coming out? :p
>
That should have been "cpptod". ;)
|
December 08, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote: >> Cool! But, when is "ctod.exe" coming out? :p >> > > That should have been "cpptod". ;) As soon as you write it. -- Simen |
December 08, 2010 Re: C Bitfields in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On 08/12/2010 02:23, Simen kjaeraas wrote:
> Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>
>>> Cool! But, when is "ctod.exe" coming out? :p
>>>
>>
>> That should have been "cpptod". ;)
>
> As soon as you write it.
>
See d.announce SWIG for D :)
|
Copyright © 1999-2021 by the D Language Foundation