]> git.ipfire.org Git - thirdparty/git.git/blame - compat/bswap.h
Merge branch 'jc/fetch-refspec-doc-update' into maint
[thirdparty/git.git] / compat / bswap.h
CommitLineData
51ea5519
NP
1/*
2 * Let's make sure we always have a sane definition for ntohl()/htonl().
3 * Some libraries define those as a function call, just to perform byte
4 * shifting, bringing significant overhead to what should be a simple
5 * operation.
6 */
7
8/*
9 * Default version that the compiler ought to optimize properly with
10 * constant values.
11 */
5322ef20 12static inline uint32_t default_swab32(uint32_t val)
51ea5519
NP
13{
14 return (((val & 0xff000000) >> 24) |
15 ((val & 0x00ff0000) >> 8) |
16 ((val & 0x0000ff00) << 8) |
17 ((val & 0x000000ff) << 24));
18}
19
7e3dae49
VM
20static inline uint64_t default_bswap64(uint64_t val)
21{
22 return (((val & (uint64_t)0x00000000000000ffULL) << 56) |
23 ((val & (uint64_t)0x000000000000ff00ULL) << 40) |
24 ((val & (uint64_t)0x0000000000ff0000ULL) << 24) |
25 ((val & (uint64_t)0x00000000ff000000ULL) << 8) |
26 ((val & (uint64_t)0x000000ff00000000ULL) >> 8) |
27 ((val & (uint64_t)0x0000ff0000000000ULL) >> 24) |
28 ((val & (uint64_t)0x00ff000000000000ULL) >> 40) |
29 ((val & (uint64_t)0xff00000000000000ULL) >> 56));
30}
31
21e403a7 32#undef bswap32
7e3dae49 33#undef bswap64
21e403a7 34
51ea5519
NP
35#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
36
c6c8d0b7
JN
37#define bswap32 git_bswap32
38static inline uint32_t git_bswap32(uint32_t x)
39{
40 uint32_t result;
41 if (__builtin_constant_p(x))
42 result = default_swab32(x);
43 else
44 __asm__("bswap %0" : "=r" (result) : "0" (x));
45 return result;
46}
51ea5519 47
7e3dae49
VM
48#define bswap64 git_bswap64
49#if defined(__x86_64__)
50static inline uint64_t git_bswap64(uint64_t x)
51{
52 uint64_t result;
53 if (__builtin_constant_p(x))
54 result = default_bswap64(x);
55 else
56 __asm__("bswap %q0" : "=r" (result) : "0" (x));
57 return result;
58}
59#else
60static inline uint64_t git_bswap64(uint64_t x)
61{
62 union { uint64_t i64; uint32_t i32[2]; } tmp, result;
63 if (__builtin_constant_p(x))
64 result.i64 = default_bswap64(x);
65 else {
66 tmp.i64 = x;
67 result.i32[0] = git_bswap32(tmp.i32[1]);
68 result.i32[1] = git_bswap32(tmp.i32[0]);
69 }
70 return result.i64;
71}
72#endif
73
0fcabdeb
SS
74#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
75
76#include <stdlib.h>
77
78#define bswap32(x) _byteswap_ulong(x)
7e3dae49 79#define bswap64(x) _byteswap_uint64(x)
0fcabdeb
SS
80
81#endif
82
7e3dae49 83#if defined(bswap32)
0fcabdeb 84
51ea5519
NP
85#undef ntohl
86#undef htonl
87#define ntohl(x) bswap32(x)
88#define htonl(x) bswap32(x)
89
90#endif
7e3dae49
VM
91
92#if defined(bswap64)
93
94#undef ntohll
95#undef htonll
96#define ntohll(x) bswap64(x)
97#define htonll(x) bswap64(x)
98
99#else
100
101#undef ntohll
102#undef htonll
103
839fa9c5 104#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
3cf6bb34
CB
105
106# define GIT_BYTE_ORDER __BYTE_ORDER
107# define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN
108# define GIT_BIG_ENDIAN __BIG_ENDIAN
109
839fa9c5
JH
110#elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
111
112# define GIT_BYTE_ORDER BYTE_ORDER
113# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
114# define GIT_BIG_ENDIAN BIG_ENDIAN
115
3cf6bb34
CB
116#else
117
118# define GIT_BIG_ENDIAN 4321
119# define GIT_LITTLE_ENDIAN 1234
120
121# if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
122# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
9c65ee15 123# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
3cf6bb34 124# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
bfb0e6fc
DM
125# elif defined(__THW_BIG_ENDIAN__) && !defined(__THW_LITTLE_ENDIAN__)
126# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
127# elif defined(__THW_LITTLE_ENDIAN__) && !defined(__THW_BIG_ENDIAN__)
128# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
3cf6bb34
CB
129# else
130# error "Cannot determine endianness"
7e3dae49 131# endif
7e3dae49 132
7e3dae49
VM
133#endif
134
3cf6bb34 135#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
7e3dae49
VM
136# define ntohll(n) (n)
137# define htonll(n) (n)
138#else
139# define ntohll(n) default_bswap64(n)
140# define htonll(n) default_bswap64(n)
141#endif
142
143#endif
802b1233
JK
144
145/*
146 * Performance might be improved if the CPU architecture is OK with
147 * unaligned 32-bit loads and a fast ntohl() is available.
148 * Otherwise fall back to byte loads and shifts which is portable,
149 * and is faster on architectures with memory alignment issues.
150 */
151
a0df2e5a
JK
152#if !defined(NO_UNALIGNED_LOADS) && ( \
153 defined(__i386__) || defined(__x86_64__) || \
802b1233
JK
154 defined(_M_IX86) || defined(_M_X64) || \
155 defined(__ppc__) || defined(__ppc64__) || \
156 defined(__powerpc__) || defined(__powerpc64__) || \
a0df2e5a 157 defined(__s390__) || defined(__s390x__))
802b1233 158
c3d8da57 159#define get_be16(p) ntohs(*(unsigned short *)(p))
802b1233
JK
160#define get_be32(p) ntohl(*(unsigned int *)(p))
161#define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0)
162
163#else
164
5b114f3b
RS
165static inline uint16_t get_be16(const void *ptr)
166{
167 const unsigned char *p = ptr;
168 return (uint16_t)p[0] << 8 |
169 (uint16_t)p[1] << 0;
170}
171
172static inline uint32_t get_be32(const void *ptr)
173{
174 const unsigned char *p = ptr;
175 return (uint32_t)p[0] << 24 |
176 (uint32_t)p[1] << 16 |
177 (uint32_t)p[2] << 8 |
178 (uint32_t)p[3] << 0;
179}
180
181static inline void put_be32(void *ptr, uint32_t value)
182{
183 unsigned char *p = ptr;
184 p[0] = value >> 24;
185 p[1] = value >> 16;
186 p[2] = value >> 8;
187 p[3] = value >> 0;
188}
802b1233
JK
189
190#endif