This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more information.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Endian Macros - are there any defaults


Stan Katz wrote:
I know that gcc defines some preprocessor macros that let the
preprocessor generate different code for different architectures (I'm
using some of them) but I can't seem to find any that tell me about
either data format (big or little endian) or invocation options. Does
anyone know of anything I can use.

#include <endian.h>


#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#elif __BYTE_ORDER == __BIG_ENDIAN
...
#endif

bits/endian.h is where __BYTE_ORDER is ultimately defined.


I can't find any endian.h file on my system, that may be because
unfortunately I'm using newlib

Oh, oops, endian.h is part of glibc!


http://www.unixpapa.com/incnote/byteorder.html suggests that <sys/param.h> is a more
portable place to get that info.

In fact, newlib itself gets it from <sys/param.h> when building iconv!
<machine/endian.h> is where BYTE_ORDER is ultimately defined on newlib,
but you should not include that directly.

Here's an old autoconf macro that uses that approach:
http://www.gnu.org/software/ac-archive/htmldoc/ac_c_bigendian_cross.html
I quote it so you can see how it used <sys/param.h> to test for
endianness.  I'm not suggesting you use autoconf (though that would
be cool if you could).

I suspect something like the following, or maybe without underscores, would
work on both newlib and glibc:

#include <sys/types.h>
#include <sys/param.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#elif __BYTE_ORDER == __BIG_ENDIAN
...
#endif

Let us know what you find out.  I've never used newlib myself.
- Dan

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sources.redhat.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]