Hello all,

In my investigations of whether I can pass structs through shared memory (after
performing endianness swaps), I looked at the precise endianness of the two
processors and struct packing. This may be of use to someone, so I thought I'd
summarise it here.

I'm not sure if struct packing may be done differently on the ARM depending on
the -O level, probably though.

Anyway, this is what one sees:

Using the following struct on the DSP-side:

typedef struct endian_test_struct {
    unsigned long iamulong1;
    unsigned int iamuint1;
    unsigned int iamuint2;
    unsigned long iamulong2;
    unsigned int iamuint3;
    unsigned long iamulong3;
} endian_test_struct;

Then giving it values and copying it into the shared memory region (all on the
DSP side):

    ets.iamulong1 = 0x12345678; // 32 bits
    ets.iamuint1 = 0x1234; // 16 bits
    ets.iamuint2 = 0x1234; // 16 bits
    ets.iamulong2 = 0x12345678; // 32 bits
    ets.iamuint3 = 0x1234; // 16 bits
    ets.iamulong3 = 0x12345678; // 32 bits

One obtains the following when reading the data on the ARM side:

Byte #: Hex 
Byte 0: 34
Byte 1: 12
Byte 2: 78
Byte 3: 56
Byte 4: 34
Byte 5: 12
Byte 6: 34
Byte 7: 12
Byte 8: 34
Byte 9: 12
Byte 10: 78
Byte 11: 56
Byte 12: 34
Byte 13: 12 
Byte 14: 0 
Byte 15: 0 
Byte 16: 34 
Byte 17: 12 
Byte 18: 78 
Byte 19: 56 

So the data on the DSP are bigendian, but as the 16bit char (=int) is the
smallest type, one doesn't see that within this type the data are stored in
little-endian form. In addition, data are packed into 32bit chunks (i.e. one
32bit value or 2x 16bit values), but if they spread across a boundary there is
padding inserted (see the gap between the last (16bit) int and the last (32bit)
long). I have looked at a similar struct with a (16bit) int on the end and the
struct was then padded to make it a multiple of 32bits long and the padding data
were not 0s.

For comparison here's the same struct filled and viewed on the ARM (with no
optimisation flags):

Byte #: Hex 
Byte 0: 78 
Byte 1: 56 
Byte 2: 34 
Byte 3: 12 
Byte 4: 34 
Byte 5: 12 
Byte 6: 0 
Byte 7: 0 
Byte 8: 34 
Byte 9: 12 
Byte 10: 0 
Byte 11: 0 
Byte 12: 78 
Byte 13: 56 
Byte 14: 34 
Byte 15: 12 
Byte 16: 34 
Byte 17: 12 
Byte 18: 0 
Byte 19: 0 
Byte 20: 78 
Byte 21: 56 
Byte 22: 34 
Byte 23: 12 

In this case we see that the data are truly little-endian, and that all struct
members are placed into a 32bit space, with packing if they are too short.

To summarise, don't try passing structs from ARM to DSP via shared memory unless
all the values are 32bits or you are careful with the order of the struct
members. Either way bit swapping is obviously required.

Cheers,


Simon

_______________________________________________
maemo-developers mailing list
maemo-developers@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-developers

Reply via email to