Fix endian detection on non-linux systems

f975f736408f32e7384cf38270a7806e68d5e112 introduced inclusion of endian.h,
which is not a standard header. Only include it on Linux machines and
add similar includes for Apple and FreeBSD.