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?
Conditionally-compiling based on endianness
Asked Answered
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).
Thanks! I'd sort of ruled that out for performance reasons, but I should look more closely. I think I could at least do that trick in some template haskell to get a compile-time constant. –
Tattler
© 2022 - 2024 — McMap. All rights reserved.