JIABI / xxhash

Automatically exported from code.google.com/p/xxhash
Other
0 stars 0 forks source link

Runtime detection of endianess #12

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Some bi-endian CPU can support both little-endian and big-endian execution.

The current method of detection uses a lot of tests on #define, to catch 
endianess. But the result is static, so a bi-endian CPU will be set to its most 
common setting (little-endian for ARM, big-endian for PPC), which, in some rare 
cases, might not be the mode set for the specific program.

Perhaps more importantly, the list of #define to check can only grow with 
different compilers and new CPU types.

Runtime check would provide a universal and reliable method, covering all 
issues mentioned here.

The attached file is an xxHash source modification, using runtime endian 
detection, instead of macro heuristics. It has been checked on x86, and 
introduces no speed penalty.

Original issue reported on code.google.com by yann.col...@gmail.com on 23 Jul 2013 at 9:46

Attachments:

GoogleCodeExporter commented 8 years ago
* Compile-time endianness configuration

Runtime detection is very good. But it would be nice if we could
configure the endianness in compile-time with XXH_CPU_LITTLE_ENDIAN.
The code will look like this:

  typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess;
+ #ifndef XXH_CPU_LITTLE_ENDIAN
  static const int one = 1;
  #define XXH_CPU_LITTLE_ENDIAN   (*(char*)(&one))
+ #endif

With this change, we can configure the endianness by compiler switch:

  gcc -DXXH_CPU_LITTLE_ENDIAN=1 ...

Original comment by takayuki...@gmail.com on 23 Jul 2013 at 4:27

GoogleCodeExporter commented 8 years ago
Thanks Takayuki,
Looks like a simple and great flexibility improvement.
Done.

Original comment by yann.col...@gmail.com on 23 Jul 2013 at 4:33

Attachments:

GoogleCodeExporter commented 8 years ago
included into r31

Original comment by yann.col...@gmail.com on 25 Jul 2013 at 8:41