[lib] better byte ordering detection

     - originally inspired by redis (src/config.h) and
       llvm (include/llvm/Support/Host.h)
     - BSDs detection inspired by this platform detection manual
       http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system#BSD
     - Apple's OSX and iOS is based on a fork of FreeBSD, including
       machine/endian.h as what we do to other BSDs should be enough.