Endianness macros

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
User avatar
TexZK
Posts: 57
Joined: Sat Sep 22, 2012 7:06 pm

Endianness macros

Post by TexZK »

Hello Giovanni,
I am developing some register mapping for cross-platform code. You chose C99 as the standard in ChibiOS 3, and that's a wise choice IMHO. C99 provides the stdint.h header with portable integer types, but the standard still lacks any portable way to determine the architecture endianness at compile time. There are some nice tricks, but they don't work in the preprocessor.

So, what I ask to you is: are you willing to put a preprocessor symbol to tell the endianness of the ported architecture? I think it will be a very useful addition to ChibiOS, because that way it is possible to generate register type mappings with portable unions, by declaring bit fields in both orders, and compiling only the target one.

For example, in ch.h (or elsewere):

Code: Select all

#define CH_ENDIANNESS_LITTLE 0x1234
#define CH_ENDIANNESS_BIG    0x4321

In the port header:

Code: Select all

#define CH_ENDIANNESS CH_ENDIANNESS_LITTLE

In user library code:

Code: Select all

union mybitfield {
#if CH_ENDIANNESS == CH_ENDIANNESS_LITTLE
  uint8_t lsb : 1;
  uint8_t msb : 1;
#elif CH_ENDIANNESS == CH_ENDIANNESS_BIG
  uint8_t msb : 1;
  uint8_t lsb : 1;
#else
#error "Unknown CH_ENDIANNESS value"
#endif
};


Do you think this will be useful? I would also write a library for endianness conversion into the community repository if needed (something like the htons() and related stuff).
Since protocols and low-level access to data structures is common in the embedded field, such features are very appreciated and easy to use.
If there is already a way to do that, my fault ;)
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Endianness macros

Post by Giovanni »

Apparently GCC already defines endianness macros, see: http://stackoverflow.com/questions/1001 ... -c-program

I will look into adding it in chtypes.h in a compiler-independent way.

Giovanni
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Endianness macros

Post by Giovanni »

This is also interesting: http://esr.ibiblio.org/?p=5095

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Endianness macros

Post by barthess »

I am pretty sure that endiannes can not be detected at compile time.
Because compiler does not care about this. Moreover Cortex-M3 has ability
to switch endiannes at any time. So as a minimal evil I have defined
endiannes in separated header and check it at a top of main() function
using union trick.
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Endianness macros

Post by steved »

The LWIP interface already has a symbol BYTE_ORDER (= BIG_ENDIAN or LITTLE_ENDIAN) (defined in various/lwip_bindings/cc.h

As one of the references said, you don't normally swap endianness in mid-execution - must say I can't quickly think of a use case ATM, and its a piece of complexity that Chibi could well do without supporting, IMHO. So a preprocessor symbol seems a good idea to me.
Post Reply