[lib] better byte ordering detection

* originally inspired by redis [src/config.h](https://github.com/antirez/redis/blob/unstable/src/config.h#L125) and
       llvm [include/llvm/Support/Host.h](https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Support/Host.h#L19)
* 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.