Jeff King | 33aa579 | 2019-03-06 14:05:10 -0500 | [diff] [blame] | 1 | #ifndef COMPAT_BSWAP_H |
| 2 | #define COMPAT_BSWAP_H |
| 3 | |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 4 | /* |
| 5 | * Let's make sure we always have a sane definition for ntohl()/htonl(). |
| 6 | * Some libraries define those as a function call, just to perform byte |
| 7 | * shifting, bringing significant overhead to what should be a simple |
| 8 | * operation. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Default version that the compiler ought to optimize properly with |
| 13 | * constant values. |
| 14 | */ |
Ramsay Jones | 5322ef2 | 2009-09-30 19:49:24 +0100 | [diff] [blame] | 15 | static inline uint32_t default_swab32(uint32_t val) |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 16 | { |
| 17 | return (((val & 0xff000000) >> 24) | |
| 18 | ((val & 0x00ff0000) >> 8) | |
| 19 | ((val & 0x0000ff00) << 8) | |
| 20 | ((val & 0x000000ff) << 24)); |
| 21 | } |
| 22 | |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 23 | static inline uint64_t default_bswap64(uint64_t val) |
| 24 | { |
| 25 | return (((val & (uint64_t)0x00000000000000ffULL) << 56) | |
| 26 | ((val & (uint64_t)0x000000000000ff00ULL) << 40) | |
| 27 | ((val & (uint64_t)0x0000000000ff0000ULL) << 24) | |
| 28 | ((val & (uint64_t)0x00000000ff000000ULL) << 8) | |
| 29 | ((val & (uint64_t)0x000000ff00000000ULL) >> 8) | |
| 30 | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24) | |
| 31 | ((val & (uint64_t)0x00ff000000000000ULL) >> 40) | |
| 32 | ((val & (uint64_t)0xff00000000000000ULL) >> 56)); |
| 33 | } |
| 34 | |
Holger Weiß | 21e403a | 2010-03-29 12:22:19 +0200 | [diff] [blame] | 35 | #undef bswap32 |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 36 | #undef bswap64 |
Holger Weiß | 21e403a | 2010-03-29 12:22:19 +0200 | [diff] [blame] | 37 | |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 38 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) |
| 39 | |
Jonathan Nieder | c6c8d0b | 2011-03-16 02:00:49 -0500 | [diff] [blame] | 40 | #define bswap32 git_bswap32 |
| 41 | static inline uint32_t git_bswap32(uint32_t x) |
| 42 | { |
| 43 | uint32_t result; |
| 44 | if (__builtin_constant_p(x)) |
| 45 | result = default_swab32(x); |
| 46 | else |
| 47 | __asm__("bswap %0" : "=r" (result) : "0" (x)); |
| 48 | return result; |
| 49 | } |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 50 | |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 51 | #define bswap64 git_bswap64 |
| 52 | #if defined(__x86_64__) |
| 53 | static inline uint64_t git_bswap64(uint64_t x) |
| 54 | { |
| 55 | uint64_t result; |
| 56 | if (__builtin_constant_p(x)) |
| 57 | result = default_bswap64(x); |
| 58 | else |
| 59 | __asm__("bswap %q0" : "=r" (result) : "0" (x)); |
| 60 | return result; |
| 61 | } |
| 62 | #else |
| 63 | static inline uint64_t git_bswap64(uint64_t x) |
| 64 | { |
| 65 | union { uint64_t i64; uint32_t i32[2]; } tmp, result; |
| 66 | if (__builtin_constant_p(x)) |
| 67 | result.i64 = default_bswap64(x); |
| 68 | else { |
| 69 | tmp.i64 = x; |
| 70 | result.i32[0] = git_bswap32(tmp.i32[1]); |
| 71 | result.i32[1] = git_bswap32(tmp.i32[0]); |
| 72 | } |
| 73 | return result.i64; |
| 74 | } |
| 75 | #endif |
| 76 | |
Daniel Gurney | 0c038fc | 2020-11-11 10:32:27 +0200 | [diff] [blame] | 77 | #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64)) |
Sebastian Schuberth | 0fcabde | 2009-10-19 18:37:05 +0200 | [diff] [blame] | 78 | |
| 79 | #include <stdlib.h> |
| 80 | |
| 81 | #define bswap32(x) _byteswap_ulong(x) |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 82 | #define bswap64(x) _byteswap_uint64(x) |
Sebastian Schuberth | 0fcabde | 2009-10-19 18:37:05 +0200 | [diff] [blame] | 83 | |
| 84 | #endif |
| 85 | |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 86 | #if defined(bswap32) |
Sebastian Schuberth | 0fcabde | 2009-10-19 18:37:05 +0200 | [diff] [blame] | 87 | |
Nicolas Pitre | 51ea551 | 2009-08-18 15:26:55 -0400 | [diff] [blame] | 88 | #undef ntohl |
| 89 | #undef htonl |
| 90 | #define ntohl(x) bswap32(x) |
| 91 | #define htonl(x) bswap32(x) |
| 92 | |
| 93 | #endif |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 94 | |
| 95 | #if defined(bswap64) |
| 96 | |
| 97 | #undef ntohll |
| 98 | #undef htonll |
| 99 | #define ntohll(x) bswap64(x) |
| 100 | #define htonll(x) bswap64(x) |
| 101 | |
| 102 | #else |
| 103 | |
| 104 | #undef ntohll |
| 105 | #undef htonll |
| 106 | |
Junio C Hamano | 839fa9c | 2014-05-02 12:36:10 -0700 | [diff] [blame] | 107 | #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 108 | |
| 109 | # define GIT_BYTE_ORDER __BYTE_ORDER |
| 110 | # define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN |
| 111 | # define GIT_BIG_ENDIAN __BIG_ENDIAN |
| 112 | |
Junio C Hamano | 839fa9c | 2014-05-02 12:36:10 -0700 | [diff] [blame] | 113 | #elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) |
| 114 | |
| 115 | # define GIT_BYTE_ORDER BYTE_ORDER |
| 116 | # define GIT_LITTLE_ENDIAN LITTLE_ENDIAN |
| 117 | # define GIT_BIG_ENDIAN BIG_ENDIAN |
| 118 | |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 119 | #else |
| 120 | |
| 121 | # define GIT_BIG_ENDIAN 4321 |
| 122 | # define GIT_LITTLE_ENDIAN 1234 |
| 123 | |
| 124 | # if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) |
| 125 | # define GIT_BYTE_ORDER GIT_BIG_ENDIAN |
Ben Walton | 9c65ee1 | 2014-05-30 16:22:40 +0100 | [diff] [blame] | 126 | # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 127 | # define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN |
David Michael | bfb0e6f | 2014-10-26 13:34:26 -0400 | [diff] [blame] | 128 | # elif defined(__THW_BIG_ENDIAN__) && !defined(__THW_LITTLE_ENDIAN__) |
| 129 | # define GIT_BYTE_ORDER GIT_BIG_ENDIAN |
| 130 | # elif defined(__THW_LITTLE_ENDIAN__) && !defined(__THW_BIG_ENDIAN__) |
| 131 | # define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 132 | # else |
| 133 | # error "Cannot determine endianness" |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 134 | # endif |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 135 | |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 136 | #endif |
| 137 | |
Charles Bailey | 3cf6bb3 | 2014-05-02 08:55:29 +0100 | [diff] [blame] | 138 | #if GIT_BYTE_ORDER == GIT_BIG_ENDIAN |
Vicent Marti | 7e3dae4 | 2013-11-14 07:43:36 -0500 | [diff] [blame] | 139 | # define ntohll(n) (n) |
| 140 | # define htonll(n) (n) |
| 141 | #else |
| 142 | # define ntohll(n) default_bswap64(n) |
| 143 | # define htonll(n) default_bswap64(n) |
| 144 | #endif |
| 145 | |
| 146 | #endif |
Jeff King | 802b123 | 2014-01-23 16:23:09 -0500 | [diff] [blame] | 147 | |
René Scharfe | 5b114f3 | 2017-07-15 21:22:50 +0200 | [diff] [blame] | 148 | static inline uint16_t get_be16(const void *ptr) |
| 149 | { |
| 150 | const unsigned char *p = ptr; |
| 151 | return (uint16_t)p[0] << 8 | |
| 152 | (uint16_t)p[1] << 0; |
| 153 | } |
| 154 | |
| 155 | static inline uint32_t get_be32(const void *ptr) |
| 156 | { |
| 157 | const unsigned char *p = ptr; |
| 158 | return (uint32_t)p[0] << 24 | |
| 159 | (uint32_t)p[1] << 16 | |
| 160 | (uint32_t)p[2] << 8 | |
| 161 | (uint32_t)p[3] << 0; |
| 162 | } |
| 163 | |
Ben Peart | b2e39d0 | 2017-09-22 12:35:37 -0400 | [diff] [blame] | 164 | static inline uint64_t get_be64(const void *ptr) |
| 165 | { |
| 166 | const unsigned char *p = ptr; |
| 167 | return (uint64_t)get_be32(&p[0]) << 32 | |
| 168 | (uint64_t)get_be32(&p[4]) << 0; |
| 169 | } |
| 170 | |
René Scharfe | 5b114f3 | 2017-07-15 21:22:50 +0200 | [diff] [blame] | 171 | static inline void put_be32(void *ptr, uint32_t value) |
| 172 | { |
| 173 | unsigned char *p = ptr; |
| 174 | p[0] = value >> 24; |
| 175 | p[1] = value >> 16; |
| 176 | p[2] = value >> 8; |
| 177 | p[3] = value >> 0; |
| 178 | } |
Jeff King | 802b123 | 2014-01-23 16:23:09 -0500 | [diff] [blame] | 179 | |
Ben Peart | b2e39d0 | 2017-09-22 12:35:37 -0400 | [diff] [blame] | 180 | static inline void put_be64(void *ptr, uint64_t value) |
| 181 | { |
| 182 | unsigned char *p = ptr; |
| 183 | p[0] = value >> 56; |
| 184 | p[1] = value >> 48; |
| 185 | p[2] = value >> 40; |
| 186 | p[3] = value >> 32; |
| 187 | p[4] = value >> 24; |
| 188 | p[5] = value >> 16; |
| 189 | p[6] = value >> 8; |
| 190 | p[7] = value >> 0; |
| 191 | } |
| 192 | |
Jeff King | 33aa579 | 2019-03-06 14:05:10 -0500 | [diff] [blame] | 193 | #endif /* COMPAT_BSWAP_H */ |