endian_inl_utils.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_UTILS_H_
  12. #define WEBP_UTILS_ENDIAN_INL_UTILS_H_
  13. #ifdef HAVE_CONFIG_H
  14. #include "../webp/config.h"
  15. #endif
  16. #include "../dsp/dsp.h"
  17. #include "../webp/types.h"
  18. #if defined(WORDS_BIGENDIAN)
  19. #define HToLE32 BSwap32
  20. #define HToLE16 BSwap16
  21. #else
  22. #define HToLE32(x) (x)
  23. #define HToLE16(x) (x)
  24. #endif
  25. #if !defined(HAVE_CONFIG_H)
  26. #if LOCAL_GCC_PREREQ(4,8) || __has_builtin(__builtin_bswap16)
  27. #define HAVE_BUILTIN_BSWAP16
  28. #endif
  29. #if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap32)
  30. #define HAVE_BUILTIN_BSWAP32
  31. #endif
  32. #if LOCAL_GCC_PREREQ(4,3) || __has_builtin(__builtin_bswap64)
  33. #define HAVE_BUILTIN_BSWAP64
  34. #endif
  35. #endif // !HAVE_CONFIG_H
  36. static WEBP_INLINE uint16_t BSwap16(uint16_t x) {
  37. #if defined(HAVE_BUILTIN_BSWAP16)
  38. return __builtin_bswap16(x);
  39. #elif defined(_MSC_VER)
  40. return _byteswap_ushort(x);
  41. #else
  42. // gcc will recognize a 'rorw $8, ...' here:
  43. return (x >> 8) | ((x & 0xff) << 8);
  44. #endif // HAVE_BUILTIN_BSWAP16
  45. }
  46. static WEBP_INLINE uint32_t BSwap32(uint32_t x) {
  47. #if defined(WEBP_USE_MIPS32_R2)
  48. uint32_t ret;
  49. __asm__ volatile (
  50. "wsbh %[ret], %[x] \n\t"
  51. "rotr %[ret], %[ret], 16 \n\t"
  52. : [ret]"=r"(ret)
  53. : [x]"r"(x)
  54. );
  55. return ret;
  56. #elif defined(HAVE_BUILTIN_BSWAP32)
  57. return __builtin_bswap32(x);
  58. #elif defined(__i386__) || defined(__x86_64__)
  59. uint32_t swapped_bytes;
  60. __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
  61. return swapped_bytes;
  62. #elif defined(_MSC_VER)
  63. return (uint32_t)_byteswap_ulong(x);
  64. #else
  65. return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
  66. #endif // HAVE_BUILTIN_BSWAP32
  67. }
  68. static WEBP_INLINE uint64_t BSwap64(uint64_t x) {
  69. #if defined(HAVE_BUILTIN_BSWAP64)
  70. return __builtin_bswap64(x);
  71. #elif defined(__x86_64__)
  72. uint64_t swapped_bytes;
  73. __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
  74. return swapped_bytes;
  75. #elif defined(_MSC_VER)
  76. return (uint64_t)_byteswap_uint64(x);
  77. #else // generic code for swapping 64-bit values (suggested by bdb@)
  78. x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
  79. x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
  80. x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
  81. return x;
  82. #endif // HAVE_BUILTIN_BSWAP64
  83. }
  84. #endif // WEBP_UTILS_ENDIAN_INL_UTILS_H_