2
0

pg_bswap.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*-------------------------------------------------------------------------
  2. *
  3. * pg_bswap.h
  4. * Byte swapping.
  5. *
  6. * Macros for reversing the byte order of 16, 32 and 64-bit unsigned integers.
  7. * For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for
  8. * built-in functions provided by the compiler where support exists.
  9. *
  10. * Note that all of these functions accept unsigned integers as arguments and
  11. * return the same. Use caution when using these wrapper macros with signed
  12. * integers.
  13. *
  14. * Copyright (c) 2015-2022, PostgreSQL Global Development Group
  15. *
  16. * src/include/port/pg_bswap.h
  17. *
  18. *-------------------------------------------------------------------------
  19. */
  20. #ifndef PG_BSWAP_H
  21. #define PG_BSWAP_H
  22. /*
  23. * In all supported versions msvc provides _byteswap_* functions in stdlib.h,
  24. * already included by c.h.
  25. */
  26. /* implementation of uint16 pg_bswap16(uint16) */
  27. #if defined(HAVE__BUILTIN_BSWAP16)
  28. #define pg_bswap16(x) __builtin_bswap16(x)
  29. #elif defined(_MSC_VER)
  30. #define pg_bswap16(x) _byteswap_ushort(x)
  31. #else
  32. static inline uint16
  33. pg_bswap16(uint16 x)
  34. {
  35. return
  36. ((x << 8) & 0xff00) |
  37. ((x >> 8) & 0x00ff);
  38. }
  39. #endif /* HAVE__BUILTIN_BSWAP16 */
  40. /* implementation of uint32 pg_bswap32(uint32) */
  41. #if defined(HAVE__BUILTIN_BSWAP32)
  42. #define pg_bswap32(x) __builtin_bswap32(x)
  43. #elif defined(_MSC_VER)
  44. #define pg_bswap32(x) _byteswap_ulong(x)
  45. #else
  46. static inline uint32
  47. pg_bswap32(uint32 x)
  48. {
  49. return
  50. ((x << 24) & 0xff000000) |
  51. ((x << 8) & 0x00ff0000) |
  52. ((x >> 8) & 0x0000ff00) |
  53. ((x >> 24) & 0x000000ff);
  54. }
  55. #endif /* HAVE__BUILTIN_BSWAP32 */
  56. /* implementation of uint64 pg_bswap64(uint64) */
  57. #if defined(HAVE__BUILTIN_BSWAP64)
  58. #define pg_bswap64(x) __builtin_bswap64(x)
  59. #elif defined(_MSC_VER)
  60. #define pg_bswap64(x) _byteswap_uint64(x)
  61. #else
  62. static inline uint64
  63. pg_bswap64(uint64 x)
  64. {
  65. return
  66. ((x << 56) & UINT64CONST(0xff00000000000000)) |
  67. ((x << 40) & UINT64CONST(0x00ff000000000000)) |
  68. ((x << 24) & UINT64CONST(0x0000ff0000000000)) |
  69. ((x << 8) & UINT64CONST(0x000000ff00000000)) |
  70. ((x >> 8) & UINT64CONST(0x00000000ff000000)) |
  71. ((x >> 24) & UINT64CONST(0x0000000000ff0000)) |
  72. ((x >> 40) & UINT64CONST(0x000000000000ff00)) |
  73. ((x >> 56) & UINT64CONST(0x00000000000000ff));
  74. }
  75. #endif /* HAVE__BUILTIN_BSWAP64 */
  76. /*
  77. * Portable and fast equivalents for ntohs, ntohl, htons, htonl,
  78. * additionally extended to 64 bits.
  79. */
  80. #ifdef WORDS_BIGENDIAN
  81. #define pg_hton16(x) (x)
  82. #define pg_hton32(x) (x)
  83. #define pg_hton64(x) (x)
  84. #define pg_ntoh16(x) (x)
  85. #define pg_ntoh32(x) (x)
  86. #define pg_ntoh64(x) (x)
  87. #else
  88. #define pg_hton16(x) pg_bswap16(x)
  89. #define pg_hton32(x) pg_bswap32(x)
  90. #define pg_hton64(x) pg_bswap64(x)
  91. #define pg_ntoh16(x) pg_bswap16(x)
  92. #define pg_ntoh32(x) pg_bswap32(x)
  93. #define pg_ntoh64(x) pg_bswap64(x)
  94. #endif /* WORDS_BIGENDIAN */
  95. /*
  96. * Rearrange the bytes of a Datum from big-endian order into the native byte
  97. * order. On big-endian machines, this does nothing at all. Note that the C
  98. * type Datum is an unsigned integer type on all platforms.
  99. *
  100. * One possible application of the DatumBigEndianToNative() macro is to make
  101. * bitwise comparisons cheaper. A simple 3-way comparison of Datums
  102. * transformed by the macro (based on native, unsigned comparisons) will return
  103. * the same result as a memcmp() of the corresponding original Datums, but can
  104. * be much cheaper. It's generally safe to do this on big-endian systems
  105. * without any special transformation occurring first.
  106. *
  107. * If SIZEOF_DATUM is not defined, then postgres.h wasn't included and these
  108. * macros probably shouldn't be used, so we define nothing. Note that
  109. * SIZEOF_DATUM == 8 would evaluate as 0 == 8 in that case, potentially
  110. * leading to the wrong implementation being selected and confusing errors, so
  111. * defining nothing is safest.
  112. */
  113. #ifdef SIZEOF_DATUM
  114. #ifdef WORDS_BIGENDIAN
  115. #define DatumBigEndianToNative(x) (x)
  116. #else /* !WORDS_BIGENDIAN */
  117. #if SIZEOF_DATUM == 8
  118. #define DatumBigEndianToNative(x) pg_bswap64(x)
  119. #else /* SIZEOF_DATUM != 8 */
  120. #define DatumBigEndianToNative(x) pg_bswap32(x)
  121. #endif /* SIZEOF_DATUM == 8 */
  122. #endif /* WORDS_BIGENDIAN */
  123. #endif /* SIZEOF_DATUM */
  124. #endif /* PG_BSWAP_H */