interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / compat / bswap.h

175 lines · 4489 bytes Raw Blame History
1#ifndef COMPAT_BSWAP_H
2#define COMPAT_BSWAP_H
3
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 */
15static inline uint32_t default_swab32(uint32_t val)
16{
17 return (((val & 0xff000000) >> 24) |
18 ((val & 0x00ff0000) >> 8) |
19 ((val & 0x0000ff00) << 8) |
20 ((val & 0x000000ff) << 24));
21}
22
23static 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
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
43#undef bswap32
44#undef bswap64
45
46#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM64))
47
48#include <stdlib.h>
49
50#define bswap32(x) _byteswap_ulong(x)
51#define bswap64(x) _byteswap_uint64(x)
52
53#define GIT_LITTLE_ENDIAN 1234
54#define GIT_BIG_ENDIAN 4321
55#define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
56
57#elif __has_builtin(__builtin_bswap32) && __has_builtin(__builtin_bswap64)
58
59#define bswap32(x) __builtin_bswap32((x))
60#define bswap64(x) __builtin_bswap64((x))
61
62#endif
63
64#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
65
66# define GIT_BYTE_ORDER __BYTE_ORDER
67# define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN
68# define GIT_BIG_ENDIAN __BIG_ENDIAN
69
70#elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
71
72# define GIT_BYTE_ORDER BYTE_ORDER
73# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
74# define GIT_BIG_ENDIAN BIG_ENDIAN
75
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)
83
84# define GIT_BIG_ENDIAN 4321
85# define GIT_LITTLE_ENDIAN 1234
86
87# if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
88# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
89# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
90# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
91# elif defined(__THW_BIG_ENDIAN__) && !defined(__THW_LITTLE_ENDIAN__)
92# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
93# elif defined(__THW_LITTLE_ENDIAN__) && !defined(__THW_BIG_ENDIAN__)
94# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
95# else
96# error "Cannot determine endianness"
97# endif
98
99#endif
100
101#undef ntohl
102#undef htonl
103#undef ntohll
104#undef htonll
105
106#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
107# define ntohl(x) (x)
108# define htonl(x) (x)
109# define ntohll(x) (x)
110# define htonll(x) (x)
111#else
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
128#endif
129
130static inline uint16_t get_be16(const void *ptr)
131{
132 const unsigned char *p = ptr;
133 return (uint16_t)p[0] << 8 |
134 (uint16_t)p[1] << 0;
135}
136
137static inline uint32_t get_be32(const void *ptr)
138{
139 const unsigned char *p = ptr;
140 return (uint32_t)p[0] << 24 |
141 (uint32_t)p[1] << 16 |
142 (uint32_t)p[2] << 8 |
143 (uint32_t)p[3] << 0;
144}
145
146static inline uint64_t get_be64(const void *ptr)
147{
148 const unsigned char *p = ptr;
149 return (uint64_t)get_be32(&p[0]) << 32 |
150 (uint64_t)get_be32(&p[4]) << 0;
151}
152
153static inline void put_be32(void *ptr, uint32_t value)
154{
155 unsigned char *p = ptr;
156 p[0] = (value >> 24) & 0xff;
157 p[1] = (value >> 16) & 0xff;
158 p[2] = (value >> 8) & 0xff;
159 p[3] = (value >> 0) & 0xff;
160}
161
162static inline void put_be64(void *ptr, uint64_t value)
163{
164 unsigned char *p = ptr;
165 p[0] = (value >> 56) & 0xff;
166 p[1] = (value >> 48) & 0xff;
167 p[2] = (value >> 40) & 0xff;
168 p[3] = (value >> 32) & 0xff;
169 p[4] = (value >> 24) & 0xff;
170 p[5] = (value >> 16) & 0xff;
171 p[6] = (value >> 8) & 0xff;
172 p[7] = (value >> 0) & 0xff;
173}
174
175#endif /* COMPAT_BSWAP_H */