@@ -32,78 +32,35 @@ static inline uint64_t default_bswap64(uint64_t val)
3232 ((val & (uint64_t )0xff00000000000000ULL ) >> 56 ));
3333}
3434
35+ /*
36+ * __has_builtin is available since Clang 10 and GCC 10.
37+ * Below is a fallback for older compilers.
38+ */
39+ #ifndef __has_builtin
40+ # define __has_builtin (x ) 0
41+ #endif
42+
3543#undef bswap32
3644#undef bswap64
3745
38- #if defined(__GNUC__ ) && (defined(__i386__ ) || defined(__x86_64__ ))
39-
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- }
50-
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-
77- #elif defined(_MSC_VER ) && (defined(_M_IX86 ) || defined(_M_X64 ) || defined(_M_ARM64 ))
46+ #if defined(_MSC_VER ) && (defined(_M_IX86 ) || defined(_M_X64 ) || defined(_M_ARM64 ))
7847
7948#include <stdlib.h>
8049
8150#define bswap32 (x ) _byteswap_ulong(x)
8251#define bswap64 (x ) _byteswap_uint64(x)
8352
84- #endif
53+ #define GIT_LITTLE_ENDIAN 1234
54+ #define GIT_BIG_ENDIAN 4321
55+ #define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
8556
86- #if defined( bswap32 )
57+ #elif __has_builtin ( __builtin_bswap32 ) && __has_builtin ( __builtin_bswap64 )
8758
88- #undef ntohl
89- #undef htonl
90- #define ntohl (x ) bswap32(x)
91- #define htonl (x ) bswap32(x)
59+ #define bswap32 (x ) __builtin_bswap32((x))
60+ #define bswap64 (x ) __builtin_bswap64((x))
9261
9362#endif
9463
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-
10764#if defined(__BYTE_ORDER ) && defined(__LITTLE_ENDIAN ) && defined(__BIG_ENDIAN )
10865
10966# define GIT_BYTE_ORDER __BYTE_ORDER
@@ -116,7 +73,13 @@ static inline uint64_t git_bswap64(uint64_t x)
11673# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
11774# define GIT_BIG_ENDIAN BIG_ENDIAN
11875
119- #else
76+ #elif defined(__BYTE_ORDER__ ) && defined(__ORDER_LITTLE_ENDIAN__ ) && defined(__ORDER_BIG_ENDIAN__ )
77+
78+ # define GIT_BYTE_ORDER __BYTE_ORDER__
79+ # define GIT_LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
80+ # define GIT_BIG_ENDIAN __ORDER_BIG_ENDIAN__
81+
82+ #elif !defined(GIT_BYTE_ORDER )
12083
12184# define GIT_BIG_ENDIAN 4321
12285# define GIT_LITTLE_ENDIAN 1234
@@ -135,14 +98,33 @@ static inline uint64_t git_bswap64(uint64_t x)
13598
13699#endif
137100
101+ #undef ntohl
102+ #undef htonl
103+ #undef ntohll
104+ #undef htonll
105+
138106#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
139- # define ntohll (n ) (n)
140- # define htonll (n ) (n)
107+ # define ntohl (x ) (x)
108+ # define htonl (x ) (x)
109+ # define ntohll (x ) (x)
110+ # define htonll (x ) (x)
141111#else
142- # define ntohll (n ) default_bswap64(n)
143- # define htonll (n ) default_bswap64(n)
144- #endif
145112
113+ # if defined(bswap32 )
114+ # define ntohl (x ) bswap32(x)
115+ # define htonl (x ) bswap32(x)
116+ # else
117+ # define ntohl (x ) default_swab32(x)
118+ # define htonl (x ) default_swab32(x)
119+ # endif
120+
121+ # if defined(bswap64 )
122+ # define ntohll (x ) bswap64(x)
123+ # define htonll (x ) bswap64(x)
124+ # else
125+ # define ntohll (x ) default_bswap64(x)
126+ # define htonll (x ) default_bswap64(x)
127+ # endif
146128#endif
147129
148130static inline uint16_t get_be16 (const void * ptr )
0 commit comments