Any Method to Determine Endianness at Compile Time ?

C

Craig Gullixson

How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.

#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN

No way or some way? TIA.


In general I agree that the best solution is to avoid endian
sensitive code, although that is not always possible. It is also
possible to look for pre-defined symbols, although the presence of
such symbols are pretty platform and compiler dependent. In
general, there is no happy solution to this problem.

However, (although I'm straying from a strict discussion of C) for
what it is worth, I use configuration scripts which adjust the
Makefiles and creates a configuration dependent include file for
compile time platform and/or compiler dependancies. The following
is out of one of my configuration scripts and does work on the few
Unixish systems I've tried it on. The border variable eventually
ends up as a #define in a configure time config.h file. I haven't
attempted to adapt this such that it can be directly put into a
Makefile.

YMMV, etc., etc., etc.

--------------------------------------------------------------------------

(this is for sh or equivalent)

##### determine the byte ordering

theborder=`echo "abcd" | od -tx4 | head -1 | awk '{print $2}'`

if [ $theborder -eq 61626364 ] ; then

border="BORDER_BIG_ENDIAN"

elif [ $theborder -eq 64636261 ] ; then

border="BORDER_LITTLE_ENDIAN"

elif [ $theborder -eq 62616463 ] ; then

border="BORDER_PDP_ENDIAN"

else

border="BORDER_UNKNOWN"

fi

--------------------------------------------------------------------------


---Craig

________________________________________________________________________
Craig A. Gullixson
Instrument Engineer INTERNET: (e-mail address removed)
National Solar Observatory/Sac. Peak PHONE: (505) 434-7065
Sunspot, NM 88349 USA FAX: (505) 434-7029
 
K

Keith Thompson

[...]

The above (starting with "How could I determine") was written by
Probably declaring the option in MAKEFILE as a switch will take care
to compile for BIG-ENDIAN or LITTLE-ENDIAN. CISC are LE type whereas
RISC are BE type, one has to choice the option and then build which
takes care at compile time.

I don't believe there any correlation between big-endian
vs. little-endian and RISC vs. CISC. As far as I know, all four
combinations exist.

The best approach, if it's practical, is to write your code so it
doesn't matter whether the platform is big-endian or little-endian.
(This may not always be practical.)

(I do not grant permission to quote this, or anything else I write
here, without attribution.)
 
M

Malcolm McLean

Keith Thompson said:
I don't believe there any correlation between big-endian
vs. little-endian and RISC vs. CISC. As far as I know, all four
combinations exist.
Correlation means that there is a statistically significant tendency one way
or the other, not that no contrary combinations exist.
E.g. if we say there is a correlation between sex and power, such that men
are more likely to hold positions of high influence, it might also be true
that the President of the most powerful country on Earth is a woman,
certainly there will be examples of individual women who are in more
powerful positions than individual men.

Another question is whether you sample is physical processors or designs.
Do you count a Pentium as one or as 600 million or however many have been
pushed out?
 
K

Keith Thompson

Malcolm McLean said:
Correlation means that there is a statistically significant tendency
one way or the other, not that no contrary combinations exist.

You're right, I could have phrased that better. I *think* the word
correlation is vague enough to cover what I meant, but perhaps it
isn't. What I meant was that knowing whether a processor is RISC or
CISC doesn't tell you whether it's big-endian or little-endian.

I was responding to a post by <[email protected]> in which it was
claimed that:
| Probably declaring the option in MAKEFILE as a switch will take care
| to compile for BIG-ENDIAN or LITTLE-ENDIAN. CISC are LE type whereas
| RISC are BE type, one has to choice the option and then build which
| takes care at compile time.

Note that there's no easy way to determine automatically whether a
given processor is RISC or CISC; the dividing line isn't even
completely clear.

[...]
Another question is whether you sample is physical processors or
designs. Do you count a Pentium as one or as 600 million or however
many have been pushed out?

I don't believe that's relevant to the question at hand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top