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?
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
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.
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.