byteorder.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* <MIT License>
  2. Copyright (c) 2013-2014 Marek Majkowski <marek@popcount.org>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. </MIT License>
  19. Original location:
  20. https://github.com/majek/csiphash/
  21. Solution inspired by code from:
  22. Samuel Neves (supercop/crypto_auth/siphash24/little)
  23. djb (supercop/crypto_auth/siphash24/little2)
  24. Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
  25. */
  26. /* This code is extracted from csiphash.h */
  27. #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  28. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  29. # define _le64toh(x) ((uint64_t)(x))
  30. #elif defined(_WIN32)
  31. /* Windows is always little endian, unless you're on xbox360
  32. http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
  33. # define _le64toh(x) ((uint64_t)(x))
  34. #elif defined(__APPLE__)
  35. # include <libkern/OSByteOrder.h>
  36. # define _le64toh(x) OSSwapLittleToHostInt64(x)
  37. #elif defined(sun) || defined(__sun)
  38. # include <sys/byteorder.h>
  39. # define _le64toh(x) LE_64(x)
  40. #else
  41. /* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
  42. # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(OpenBSD)
  43. # include <sys/endian.h>
  44. # else
  45. # include <endian.h>
  46. # endif
  47. # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
  48. __BYTE_ORDER == __LITTLE_ENDIAN
  49. # define _le64toh(x) ((uint64_t)(x))
  50. # else
  51. # if defined(OpenBSD)
  52. # define _le64toh(x) letoh64(x)
  53. # else
  54. # define _le64toh(x) le64toh(x)
  55. # endif
  56. # endif
  57. #endif