Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reverse Bitfields

2,739 views
Skip to first unread message

average

unread,
Mar 18, 2008, 8:38:13 PM3/18/08
to
I am trying to figure out the best way to correct a problem I am
having by converting source code written for PowerPC to work on Intel
x86_64. There are hundreds, if not thousands of bitfields defined in
the source code and they all need to have their ordering reversed.

For instance, this structure in PowerPC land:
struct
{
unsigned msb: 4;
unsigned a: 4;
unsigned b: 4;
unsigned lsb: 4;
};

Needs to become this structure in Intel land:
struct
{
unsigned lsb: 4;
unsigned b: 4;
unsigned a: 4;
unsigned msb: 4;
};

I have read that there exists a pragma for reversing bitfields with
GCC:
#pragma reverse_bitfields on | off | reset

However, according to some strange release notes for GCC 4.0: [http://
developer.apple.com/releasenotes/DeveloperTools/RN-GCC4/index.html]:
"This pragma has no effect when generating code for x86
processors."

Does GCC allow reversing of bitfields in C on the x86_64 architecture?

I have also read about people theorizing that you can change the
BITS_BIG_ENDIAN flag in the GCC source code to be defined as 1 and
recompile it. Then use that version of GCC to compile your code.
However, after reading this post, I believe that this would not work
at all:
[http://gcc.gnu.org/ml/gcc/2004-09/msg00635.html]

Does anyone have any suggestions on how to reverse the bitfield order
for structures in C?

Ulrich Eckhardt

unread,
Mar 22, 2008, 6:07:22 AM3/22/08
to
average wrote:
> I am trying to figure out the best way to correct a problem I am
> having by converting source code written for PowerPC to work on Intel
> x86_64. There are hundreds, if not thousands of bitfields defined in
> the source code and they all need to have their ordering reversed.
>
> For instance, this structure in PowerPC land:
> struct
> {
> unsigned msb: 4;
> unsigned a: 4;
> unsigned b: 4;
> unsigned lsb: 4;
> };
>
> Needs to become this structure in Intel land:
> struct
> {
> unsigned lsb: 4;
> unsigned b: 4;
> unsigned a: 4;
> unsigned msb: 4;
> };

Well, now you now why you should avoid depending on the layout of bitfields.
Anyway, take a look at Linux for example, there are lots of examples that
run like this:

#if __BIG_ENDIAN


struct
{
unsigned msb: 4;
unsigned a: 4;
unsigned b: 4;
unsigned lsb: 4;
};

#else


struct
{
unsigned lsb: 4;
unsigned b: 4;
unsigned a: 4;
unsigned msb: 4;
};

#endif

You could possibly use a programmable editor to make the conversion for you.

Uli

average

unread,
Mar 24, 2008, 9:52:35 AM3/24/08
to

belive my, i know! i would never favor the use of bitfields, but now
i have all of this legacy code that is infected with it. i could
definitely go through it manually and invert the bitfields, but i was
just hoping that there would be an easier, more automated way to do
it. there are thousands of these structures.

average

unread,
Mar 30, 2008, 12:55:12 PM3/30/08
to
I found an excellent patch to gcc that implements an option to reverse
bitfield layout in structures and unions:
http://www.busybox.net/cgi-bin/viewcvs.cgi/trunk/buildroot/toolchain/gcc/3.4.6/900-nios2.patch?rev=20492

the patch includes other things as well, so i had to sift through it
and extract just the bit field reverse code. this patch gives gcc a
command line option to reverse bitfields (-mreverse-bitfields),
provides support for a #pragma reverse_bitfields, and provides support
for an __attribute__((reverse_bitfields)). from what i have tested it
all works great.

0 new messages