I'm reading Word
s 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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With