views:

1640

answers:

7

Hi

I need to convert a bit-field structure from little-endian to big-endia architecture. What is the best way to do that, as there will be issues in byte boundaries, if I simply swap the structure elements.

Ex Structure is:

struct {
    unsigned int    b1:1;
    unsigned int    b2:8;
    unsigned int    b3:7;
    unsigned int    b4:8;
    unsigned int    b5:7;
    unsigned int    b6:1;
};
A: 

You have two 16 bit sections there (the first three fields and the last three fields are 16 bits).

That's only 65536 entries. So have a lookup table that holds the bit-reversed version of the fields. Wrap the struct in a union with another struct that has two 16 bit fields to make this easier?

Something like (untested, I'm not near a C compiler):

union u {
    struct {
        unsigned int    b1:1;
        unsigned int    b2:8;
        unsigned int    b3:7;
        unsigned int    b4:8;
        unsigned int    b5:7;
        unsigned int    b6:1;
     } bits;
     struct {
        uint16 first;
        uint16 second;
     } words
} ;

unit16 lookup[65536];

/* swap architectures */

void swapbits ( union u *p)
{
   p->words.first = lookup[p->words.first];
   p->words.second = lookup[p->words.second];
}

Population of the lookup table left as an exercise for the reader :)

However, read your compiler doc carefully. I'm not sure if the C standard requires that struct to fit in a word (although I'd expect most compilers to do that).

Paul
Unless the performance is absolutely critical, this code wastes 128k of memory. No wonder 4Gb are no longer considered enough for productive work ;-)
MaxVT
Well, maybe it is critical, we've not been told.
Paul
+7  A: 

You could use a 32 bit integer, and extract information out of it using and- and bitshift operators. With that in place, you could simply use htonl (host-to-network, long). Network byte order is big endian.

This won't be as elegant as a bit-field, but at least you'll know what you have and won't have to worry about the compiler padding your structures.

roe
+1 For me, htonl() or htons() combined with bit masks and bit shifts is the most maintainable approach for this kind of stuff.
mouviciel
Yes you are correct, though the epatel's method as given below also works, I just need to see where all it will not work :)
foo
The method given by epatel is very common also (and I upvoted it as well). But it can be tricky when bit fields overlap a byte boundary.
mouviciel
+1  A: 

In a project porting code from MIPS to Linux/x86 we did like this.

struct {

#ifdef __ONE_ENDIANESS__
    unsigned int    b1:1;
    unsigned int    b2:8;
    unsigned int    b3:7;
    unsigned int    b4:8;
    unsigned int    b5:7;
    unsigned int    b6:1;
#define _STRUCT_FILLED
#endif /* __ONE_ENDIANESS__ */

#ifdef __OTHER_ENDIANESS__
    unsigned int    b6:1;
    unsigned int    b5:7;
    unsigned int    b4:8;
    unsigned int    b3:7;
    unsigned int    b2:8;
    unsigned int    b1:1;
#define _STRUCT_FILLED
#endif /* __OTHER_ENDIANESS__ */

};

#ifndef _STRUCT_FILLED
#  error Endianess uncertain for struct
#else
#  undef _STRUCT_FILLED
#endif /* _STRUCT_FILLED */

The macros __ONE_ENDIANESS__ and __OTHER_ENDIANESS__ was the appropriate for the compiler we used so you might need to look into which is appropriate for you...

epatel
Notice that fields b2 and b5 span more than one byte in the first example, so it isn't likely that they can be re-written to match in the second case. Otherwise, this trick can save a lot of hair pulling.
RBerteig
Not? I think if sizeof(int) it worked fine for us...
epatel
ah ;) saw now...I was just editing without looking...fixing!
epatel
@epatel: surely sizeof (int) is 4, or some small value like that? The unit for sizeof is chars, so for a 32-bit int on a machine where CHAR_BIT is 8, it will give 4.
unwind
sizeof(int)== 32 bit I meant
epatel
if a field was longer than 8 bits it also should be swapped for one of the endianess archs
epatel
@epatel This seems to be the best way out for me !
foo
A: 

It should be enough to swap the bytes. Bit position within a byte is the same in big and little endian.
e.g. :

char* dest = (char*)&yourstruct;
unsigned int orig = yourstruct;
char* origbytes = (char*)&orig;
dest[0] = origbytes[3];
dest[1] = origbytes[2];
dest[2] = origbytes[1];
dest[3] = origbytes[0];
qwerty
As far as I can tell, the ANSI C standard does not specify the order in which bitfields are allocated inside a byte (or word) so swapping bytes may not be enough.
Paul
yes, won't be portable. but i *guess* most compilers should put the bits in the "natural" place (e.g. struct {unsigned char a:1,b:6,c:1} ---> a bit 0, b bit 1-6, c bit 7.)...if portability is at prime, use roe's advice.
qwerty
+1  A: 

You want to do this between the channel (file, or network) and your structure. My preferred practice is to isolate file I/O from structures by write code that builds the file buffers in a known representation, and matching read code that reverses that transformation.

Your specific example is particularly difficult to guess at because the bitfields are defined to be unsigned int and sizeof(unsigned int) is particularly non-portable.

Assuming as a SWAG that sizeof(int)==4 then getting a pointer to the struct and reording the individual bytes probably gets you the answer you want.

The trick of defining the struct differently for different platforms might work, but in the example you cite there isn't a clean break at byte boundaries, so it is not likely to be possible to produce an equivalent of one platform in the other without splitting one or more of the fields into two pieces.

RBerteig
+1  A: 

You should not use bit-fields when the physical layout is important because it is implementation-defined in which order the larger word is populated.

zvrba
A: 

To get this going I finally got a solution (some what derived from epatel's solution above). This is if I convert from x86 to Solaris SPARC.

We need to first swap the incoming sturcture and then read the elements in reverse order. Basically after looking at how the structures are alligned I saw that the endianess changed both in byte ordering and bit ordering. Here is a pseudo code.

struct orig
{    
    unsigned int    b1:1;
    unsigned int    b2:8;
    unsigned int    b3:7;
    unsigned int    b4:8;
    unsigned int    b5:7;
    unsigned int    b6:1;
};

struct temp
{    
    unsigned int    b6:1;
    unsigned int    b5:7;
    unsigned int    b4:8;
    unsigned int    b3:7;
    unsigned int    b2:8;
    unsigned int    b1:1;
}temp;


func (struct orig *toconvert)
{
    struct temp temp_val;
    //Swap the bytes
    swap32byte((u32*)toconvert);
    //Now read the structure in reverse order - bytes have been swapped
    (u32*)&temp_val = (u32 *)toconvert;
    //Write it back to orignal structure
    toconvert->b6=temp_val.b6;
    toconvert->b5=temp_val.b5;
    toconvert->b4=temp_val.b4;
    toconvert->b3=temp_val.b3;
    toconvert->b2=temp_val.b2;
    toconvert->b1=temp_val.b1;

}

After some experimenting I found that this approach is only valid if the elements completely fill the structure, i.e. there are no unused bits.

foo