endian_inl.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Endian related functions.
  11. #ifndef WEBP_UTILS_ENDIAN_INL_H_
  12. #define WEBP_UTILS_ENDIAN_INL_H_
  13. #ifdef HAVE_CONFIG_H
  14. #include "../webp/config.h"
  15. #endif
  16. #include "../dsp/dsp.h"
  17. #include "../webp/types.h"
  18. // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
  19. #if !defined(WORDS_BIGENDIAN) && \
  20. (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
  21. (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
  22. #define WORDS_BIGENDIAN
  23. #endif
  24. #if defined(WORDS_BIGENDIAN)
  25. #define HToLE32 BSwap32
  26. #define HToLE16 BSwap16
  27. #else
  28. #define HToLE32(x) (x)
  29. #define HToLE16(x) (x)
  30. #endif
  31. #if !defined(HAVE_CONFIG_H)
  32. // clang-3.3 and gcc-4.3 have builtin functions for swap32/swap64
  33. #if LOCAL_GCC_PREREQ(4,3) || LOCAL_CLANG_PREREQ(3,3)
  34. #define HAVE_BUILTIN_BSWAP32
  35. #define HAVE_BUILTIN_BSWAP64
  36. #endif
  37. // clang-3.3 and gcc-4.8 have a builtin function for swap16
  38. #if LOCAL_GCC_PREREQ(4,8) || LOCAL_CLANG_PREREQ(3,3)
  39. #define HAVE_BUILTIN_BSWAP16
  40. #endif
  41. #endif // !HAVE_CONFIG_H
  42. static WEBP_INLINE uint16_t BSwap16(uint16_t x) {
  43. #if defined(HAVE_BUILTIN_BSWAP16)
  44. return __builtin_bswap16(x);
  45. #elif defined(_MSC_VER)
  46. return _byteswap_ushort(x);
  47. #else
  48. // gcc will recognize a 'rorw $8, ...' here:
  49. return (x >> 8) | ((x & 0xff) << 8);
  50. #endif // HAVE_BUILTIN_BSWAP16
  51. }
  52. static WEBP_INLINE uint32_t BSwap32(uint32_t x) {
  53. #if defined(WEBP_USE_MIPS32_R2)
  54. uint32_t ret;
  55. __asm__ volatile (
  56. "wsbh %[ret], %[x] \n\t"
  57. "rotr %[ret], %[ret], 16 \n\t"
  58. : [ret]"=r"(ret)
  59. : [x]"r"(x)
  60. );
  61. return ret;
  62. #elif defined(HAVE_BUILTIN_BSWAP32)
  63. return __builtin_bswap32(x);
  64. #elif defined(__i386__) || defined(__x86_64__)
  65. uint32_t swapped_bytes;
  66. __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
  67. return swapped_bytes;
  68. #elif defined(_MSC_VER)
  69. return (uint32_t)_byteswap_ulong(x);
  70. #else
  71. return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
  72. #endif // HAVE_BUILTIN_BSWAP32
  73. }
  74. static WEBP_INLINE uint64_t BSwap64(uint64_t x) {
  75. #if defined(HAVE_BUILTIN_BSWAP64)
  76. return __builtin_bswap64(x);
  77. #elif defined(__x86_64__)
  78. uint64_t swapped_bytes;
  79. __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
  80. return swapped_bytes;
  81. #elif defined(_MSC_VER)
  82. return (uint64_t)_byteswap_uint64(x);
  83. #else // generic code for swapping 64-bit values (suggested by bdb@)
  84. x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
  85. x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
  86. x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
  87. return x;
  88. #endif // HAVE_BUILTIN_BSWAP64
  89. }
  90. #endif // WEBP_UTILS_ENDIAN_INL_H_