@@ -32,78 +32,35 @@ static inline uint64_t default_bswap64(uint64_t val)
32
32
((val & (uint64_t )0xff00000000000000ULL ) >> 56 ));
33
33
}
34
34
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
+
35
43
#undef bswap32
36
44
#undef bswap64
37
45
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 ))
78
47
79
48
#include <stdlib.h>
80
49
81
50
#define bswap32 (x ) _byteswap_ulong(x)
82
51
#define bswap64 (x ) _byteswap_uint64(x)
83
52
84
- #endif
53
+ #define GIT_LITTLE_ENDIAN 1234
54
+ #define GIT_BIG_ENDIAN 4321
55
+ #define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
85
56
86
- #if defined( bswap32 )
57
+ #elif __has_builtin ( __builtin_bswap32 ) && __has_builtin ( __builtin_bswap64 )
87
58
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))
92
61
93
62
#endif
94
63
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
-
107
64
#if defined(__BYTE_ORDER ) && defined(__LITTLE_ENDIAN ) && defined(__BIG_ENDIAN )
108
65
109
66
# define GIT_BYTE_ORDER __BYTE_ORDER
@@ -116,7 +73,13 @@ static inline uint64_t git_bswap64(uint64_t x)
116
73
# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
117
74
# define GIT_BIG_ENDIAN BIG_ENDIAN
118
75
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 )
120
83
121
84
# define GIT_BIG_ENDIAN 4321
122
85
# define GIT_LITTLE_ENDIAN 1234
@@ -135,14 +98,33 @@ static inline uint64_t git_bswap64(uint64_t x)
135
98
136
99
#endif
137
100
101
+ #undef ntohl
102
+ #undef htonl
103
+ #undef ntohll
104
+ #undef htonll
105
+
138
106
#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)
141
111
#else
142
- # define ntohll (n ) default_bswap64(n)
143
- # define htonll (n ) default_bswap64(n)
144
- #endif
145
112
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
146
128
#endif
147
129
148
130
static inline uint16_t get_be16 (const void * ptr )
0 commit comments