Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally-compiling based on endianness

I'm reading Words from byte arrays and need to make parts of my code endian-aware. Does GHC expose anything (like MachDeps.h) which I can use to make code conditional on processor endianness? If not can I deduce endianness reliably from HOST_ARCH (from ghcplatform.h accessible in .cabal file with the arch() conditional)? Or other ideas?

like image 363
jberryman Avatar asked Mar 30 '15 15:03

jberryman


1 Answers

Are you looking for something like this: https://hackage.haskell.org/package/cpu-0.1.0/docs/System-Endian.html

If you don't want/cannot use these packages, looking at the source code for the above, you can see how to check endianness on any platform with (almost) any lower-level programming language like C or similar. Fill a part of your stack (for a machine with 4 bits: e.g. 1000) and then read back the LSB or MSB of said stack. The Endianness will determine how it is stored (you'll read back 1000 or 0001).

like image 152
Guru Evi Avatar answered Oct 31 '22 10:10

Guru Evi