aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_bithelpers.h
blob: 8f7058a836c550cfa6dd7cee1cec49a88a3eebfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
  This file is part of libmicrohttpd
  Copyright (C) 2019-2021 Karlson2k (Evgeny Grin)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library.
  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file microhttpd/mhd_bithelpers.h
 * @brief  macros for bits manipulations
 * @author Karlson2k (Evgeny Grin)
 */

#ifndef MHD_BITHELPERS_H
#define MHD_BITHELPERS_H 1

#include "mhd_options.h"
#include <stdint.h>
#if defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
  defined(__OPTIMIZE__)))
/* Declarations for VC & Clang/C2 built-ins */
#include <intrin.h>
#endif /* _MSC_FULL_VER  */
#include "mhd_assert.h"
#include "mhd_byteorder.h"
#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN || _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#include "mhd_align.h"
#endif /* _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN ||
          _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN */

#ifndef __has_builtin
/* Avoid precompiler errors with non-clang */
#  define __has_builtin(x) 0
#endif


#ifdef MHD_HAVE___BUILTIN_BSWAP32
#define _MHD_BYTES_SWAP32(value32)  \
  ((uint32_t) __builtin_bswap32 ((uint32_t) value32))
#elif defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
  defined(__OPTIMIZE__)))
/* Clang/C2 may not inline this function if optimizations are turned off. */
#ifndef __clang__
#pragma intrinsic(_byteswap_ulong)
#endif /* ! __clang__ */
#define _MHD_BYTES_SWAP32(value32)  \
  ((uint32_t) _byteswap_ulong ((uint32_t) value32))
#elif __has_builtin (__builtin_bswap32)
#define _MHD_BYTES_SWAP32(value32)  \
  ((uint32_t) __builtin_bswap32 ((uint32_t) value32))
#else  /* ! __has_builtin(__builtin_bswap32) */
#define _MHD_BYTES_SWAP32(value32)                                  \
  ( (((uint32_t) (value32)) << 24)                                  \
    | ((((uint32_t) (value32)) & ((uint32_t) 0x0000FF00)) << 8)     \
    | ((((uint32_t) (value32)) & ((uint32_t) 0x00FF0000)) >> 8)     \
    | (((uint32_t) (value32))                           >> 24) )
#endif /* ! __has_builtin(__builtin_bswap32) */

#ifdef MHD_HAVE___BUILTIN_BSWAP64
#define _MHD_BYTES_SWAP64(value64) \
  ((uint64_t) __builtin_bswap64 ((uint64_t) value64))
#elif defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
  defined(__OPTIMIZE__)))
/* Clang/C2 may not inline this function if optimizations are turned off. */
#ifndef __clang__
#pragma intrinsic(_byteswap_uint64)
#endif /* ! __clang__ */
#define _MHD_BYTES_SWAP64(value64)  \
  ((uint64_t) _byteswap_uint64 ((uint64_t) value64))
#elif __has_builtin (__builtin_bswap64)
#define _MHD_BYTES_SWAP64(value64) \
  ((uint64_t) __builtin_bswap64 ((uint64_t) value64))
#else  /* ! __has_builtin(__builtin_bswap64) */
#define _MHD_BYTES_SWAP64(value64)                                          \
  ( (((uint64_t) (value64)) << 56)                                          \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x000000000000FF00)) << 40)    \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x0000000000FF0000)) << 24)    \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x00000000FF000000)) << 8)     \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x000000FF00000000)) >> 8)     \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x0000FF0000000000)) >> 24)    \
    | ((((uint64_t) (value64)) & ((uint64_t) 0x00FF000000000000)) >> 40)    \
    | (((uint64_t) (value64))                                   >> 56) )
#endif /* ! __has_builtin(__builtin_bswap64) */


/* _MHD_PUT_64BIT_LE (addr, value64)
 * put native-endian 64-bit value64 to addr
 * in little-endian mode.
 */
/* Slow version that works with unaligned addr and with any bytes order */
#define _MHD_PUT_64BIT_LE_SLOW(addr, value64) do {                       \
    ((uint8_t*) (addr))[0] = (uint8_t) ((uint64_t) (value64));           \
    ((uint8_t*) (addr))[1] = (uint8_t) (((uint64_t) (value64)) >> 8);    \
    ((uint8_t*) (addr))[2] = (uint8_t) (((uint64_t) (value64)) >> 16);   \
    ((uint8_t*) (addr))[3] = (uint8_t) (((uint64_t) (value64)) >> 24);   \
    ((uint8_t*) (addr))[4] = (uint8_t) (((uint64_t) (value64)) >> 32);   \
    ((uint8_t*) (addr))[5] = (uint8_t) (((uint64_t) (value64)) >> 40);   \
    ((uint8_t*) (addr))[6] = (uint8_t) (((uint64_t) (value64)) >> 48);   \
    ((uint8_t*) (addr))[7] = (uint8_t) (((uint64_t) (value64)) >> 56);   \
} while (0)
#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_PUT_64BIT_LE(addr, value64)             \
  ((*(uint64_t*) (addr)) = (uint64_t) (value64))
#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_PUT_64BIT_LE(addr, value64)             \
  ((*(uint64_t*) (addr)) = _MHD_BYTES_SWAP64 (value64))
#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_PUT_64BIT_LE(addr, value64) do {                            \
    ((uint8_t*) (addr))[0] = (uint8_t) ((uint64_t) (value64));           \
    ((uint8_t*) (addr))[1] = (uint8_t) (((uint64_t) (value64)) >> 8);    \
    ((uint8_t*) (addr))[2] = (uint8_t) (((uint64_t) (value64)) >> 16);   \
    ((uint8_t*) (addr))[3] = (uint8_t) (((uint64_t) (value64)) >> 24);   \
    ((uint8_t*) (addr))[4] = (uint8_t) (((uint64_t) (value64)) >> 32);   \
    ((uint8_t*) (addr))[5] = (uint8_t) (((uint64_t) (value64)) >> 40);   \
    ((uint8_t*) (addr))[6] = (uint8_t) (((uint64_t) (value64)) >> 48);   \
    ((uint8_t*) (addr))[7] = (uint8_t) (((uint64_t) (value64)) >> 56);   \
} while (0)
/* Indicate that _MHD_PUT_64BIT_LE does not need aligned pointer */
#define _MHD_PUT_64BIT_LE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */

/* Put result safely to unaligned address */
_MHD_static_inline void
_MHD_PUT_64BIT_LE_SAFE (void *dst, uint64_t value)
{
#ifndef _MHD_PUT_64BIT_LE_UNALIGNED
  if (0 != ((uintptr_t) dst) % (_MHD_UINT64_ALIGN))
    _MHD_PUT_64BIT_LE_SLOW (dst, value);
  else
#endif /* ! _MHD_PUT_64BIT_BE_UNALIGNED */
  _MHD_PUT_64BIT_LE (dst, value);
}


/* _MHD_PUT_32BIT_LE (addr, value32)
 * put native-endian 32-bit value32 to addr
 * in little-endian mode.
 */
#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_PUT_32BIT_LE(addr,value32)             \
  ((*(uint32_t*) (addr)) = (uint32_t) (value32))
#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_PUT_32BIT_LE(addr, value32)            \
  ((*(uint32_t*) (addr)) = _MHD_BYTES_SWAP32 (value32))
#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_PUT_32BIT_LE(addr, value32) do {                            \
    ((uint8_t*) (addr))[0] = (uint8_t) ((uint32_t) (value32));           \
    ((uint8_t*) (addr))[1] = (uint8_t) (((uint32_t) (value32)) >> 8);    \
    ((uint8_t*) (addr))[2] = (uint8_t) (((uint32_t) (value32)) >> 16);   \
    ((uint8_t*) (addr))[3] = (uint8_t) (((uint32_t) (value32)) >> 24);   \
} while (0)
/* Indicate that _MHD_PUT_32BIT_LE does not need aligned pointer */
#define _MHD_PUT_32BIT_LE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */

/* _MHD_GET_32BIT_LE (addr)
 * get little-endian 32-bit value storied at addr
 * and return it in native-endian mode.
 */
#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_GET_32BIT_LE(addr)             \
  (*(const uint32_t*) (addr))
#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_GET_32BIT_LE(addr)             \
  _MHD_BYTES_SWAP32 (*(const uint32_t*) (addr))
#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_GET_32BIT_LE(addr)                           \
  ( ( (uint32_t) (((const uint8_t*) addr)[0]))            \
    | (((uint32_t) (((const uint8_t*) addr)[1])) << 8)    \
    | (((uint32_t) (((const uint8_t*) addr)[2])) << 16)   \
    | (((uint32_t) (((const uint8_t*) addr)[3])) << 24) )
/* Indicate that _MHD_GET_32BIT_LE does not need aligned pointer */
#define _MHD_GET_32BIT_LE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */


/* _MHD_PUT_64BIT_BE (addr, value64)
 * put native-endian 64-bit value64 to addr
 * in big-endian mode.
 */
/* Slow version that works with unaligned addr and with any bytes order */
#define _MHD_PUT_64BIT_BE_SLOW(addr, value64) do {                       \
    ((uint8_t*) (addr))[7] = (uint8_t) ((uint64_t) (value64));           \
    ((uint8_t*) (addr))[6] = (uint8_t) (((uint64_t) (value64)) >> 8);    \
    ((uint8_t*) (addr))[5] = (uint8_t) (((uint64_t) (value64)) >> 16);   \
    ((uint8_t*) (addr))[4] = (uint8_t) (((uint64_t) (value64)) >> 24);   \
    ((uint8_t*) (addr))[3] = (uint8_t) (((uint64_t) (value64)) >> 32);   \
    ((uint8_t*) (addr))[2] = (uint8_t) (((uint64_t) (value64)) >> 40);   \
    ((uint8_t*) (addr))[1] = (uint8_t) (((uint64_t) (value64)) >> 48);   \
    ((uint8_t*) (addr))[0] = (uint8_t) (((uint64_t) (value64)) >> 56);   \
} while (0)
#if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_PUT_64BIT_BE(addr, value64)             \
  ((*(uint64_t*) (addr)) = (uint64_t) (value64))
#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_PUT_64BIT_BE(addr, value64)             \
  ((*(uint64_t*) (addr)) = _MHD_BYTES_SWAP64 (value64))
#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_PUT_64BIT_BE(addr, value64) _MHD_PUT_64BIT_BE_SLOW(addr, value64)
/* Indicate that _MHD_PUT_64BIT_BE does not need aligned pointer */
#define _MHD_PUT_64BIT_BE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */

/* Put result safely to unaligned address */
_MHD_static_inline void
_MHD_PUT_64BIT_BE_SAFE (void *dst, uint64_t value)
{
#ifndef _MHD_PUT_64BIT_BE_UNALIGNED
  if (0 != ((uintptr_t) dst) % (_MHD_UINT64_ALIGN))
    _MHD_PUT_64BIT_BE_SLOW (dst, value);
  else
#endif /* ! _MHD_PUT_64BIT_BE_UNALIGNED */
  _MHD_PUT_64BIT_BE (dst, value);
}


/* _MHD_PUT_32BIT_BE (addr, value32)
 * put native-endian 32-bit value32 to addr
 * in big-endian mode.
 */
#if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_PUT_32BIT_BE(addr, value32)             \
  ((*(uint32_t*) (addr)) = (uint32_t) (value32))
#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_PUT_32BIT_BE(addr, value32)             \
  ((*(uint32_t*) (addr)) = _MHD_BYTES_SWAP32 (value32))
#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_PUT_32BIT_BE(addr, value32) do {                            \
    ((uint8_t*) (addr))[3] = (uint8_t) ((uint32_t) (value32));           \
    ((uint8_t*) (addr))[2] = (uint8_t) (((uint32_t) (value32)) >> 8);    \
    ((uint8_t*) (addr))[1] = (uint8_t) (((uint32_t) (value32)) >> 16);   \
    ((uint8_t*) (addr))[0] = (uint8_t) (((uint32_t) (value32)) >> 24);   \
} while (0)
/* Indicate that _MHD_PUT_32BIT_BE does not need aligned pointer */
#define _MHD_PUT_32BIT_BE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */

/* _MHD_GET_32BIT_BE (addr)
 * get big-endian 32-bit value storied at addr
 * and return it in native-endian mode.
 */
#if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
#define _MHD_GET_32BIT_BE(addr)             \
  (*(const uint32_t*) (addr))
#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
#define _MHD_GET_32BIT_BE(addr)             \
  _MHD_BYTES_SWAP32 (*(const uint32_t*) (addr))
#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
/* Endianness was not detected or non-standard like PDP-endian */
#define _MHD_GET_32BIT_BE(addr)                           \
  ( (((uint32_t) (((const uint8_t*) addr)[0])) << 24)     \
    | (((uint32_t) (((const uint8_t*) addr)[1])) << 16)   \
    | (((uint32_t) (((const uint8_t*) addr)[2])) << 8)    \
    | ((uint32_t) (((const uint8_t*) addr)[3])) )
/* Indicate that _MHD_GET_32BIT_BE does not need aligned pointer */
#define _MHD_GET_32BIT_BE_UNALIGNED 1
#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */


/**
 * Rotate right 32-bit value by number of bits.
 * bits parameter must be more than zero and must be less than 32.
 */
#if defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
  defined(__OPTIMIZE__)))
/* Clang/C2 do not inline this function if optimizations are turned off. */
#ifndef __clang__
#pragma intrinsic(_rotr)
#endif /* ! __clang__ */
#define _MHD_ROTR32(value32, bits) \
  ((uint32_t) _rotr ((uint32_t) (value32),(bits)))
#elif __has_builtin (__builtin_rotateright32)
#define _MHD_ROTR32(value32, bits) \
  ((uint32_t) __builtin_rotateright32 ((value32), (bits)))
#else  /* ! __builtin_rotateright32 */
_MHD_static_inline uint32_t
_MHD_ROTR32 (uint32_t value32, int bits)
{
  mhd_assert (bits < 32);
  /* Defined in form which modern compiler could optimize. */
  return (value32 >> bits) | (value32 << (32 - bits));
}


#endif /* ! __builtin_rotateright32 */


/**
 * Rotate left 32-bit value by number of bits.
 * bits parameter must be more than zero and must be less than 32.
 */
#if defined(_MSC_FULL_VER) && (! defined(__clang__) || (defined(__c2__) && \
  defined(__OPTIMIZE__)))
/* Clang/C2 do not inline this function if optimizations are turned off. */
#ifndef __clang__
#pragma intrinsic(_rotl)
#endif /* ! __clang__ */
#define _MHD_ROTL32(value32, bits) \
  ((uint32_t) _rotl ((uint32_t) (value32),(bits)))
#elif __has_builtin (__builtin_rotateleft32)
#define _MHD_ROTL32(value32, bits) \
  ((uint32_t) __builtin_rotateleft32 ((value32), (bits)))
#else  /* ! __builtin_rotateleft32 */
_MHD_static_inline uint32_t
_MHD_ROTL32 (uint32_t value32, int bits)
{
  mhd_assert (bits < 32);
  /* Defined in form which modern compiler could optimize. */
  return (value32 << bits) | (value32 >> (32 - bits));
}


#endif /* ! __builtin_rotateleft32 */


#endif /* ! MHD_BITHELPERS_H */