chiark / gitweb /
Merge branch '2.4.x'
[mLib] / utils / bits.h
1 /* -*-c-*-
2  *
3  * Portable bit-level manipulation macros
4  *
5  * (c) 1998 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef MLIB_BITS_H
29 #define MLIB_BITS_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <limits.h>
38 #include <stddef.h>
39 #if __STDC_VERSION__ >= 199900l
40 #  include <stdint.h>
41 #endif
42
43 #ifndef MLIB_COMPILER_H
44 #  include "compiler.h"
45 #endif
46
47 /*----- Decide on some types ----------------------------------------------*/
48
49 /* --- Make GNU C shut up --- */
50
51 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 91)
52 #  define MLIB_BITS_EXTENSION __extension__
53 #else
54 #  define MLIB_BITS_EXTENSION
55 #endif
56
57 /* --- Decide on a 32-bit type --- *
58  *
59  * I want a type which is capable of expressing 32-bit numbers.  Because some
60  * implementations have 64-bit @long@s (infinitely preferable to the abortion
61  * that is @long long@), using @unsigned long@ regardless is wasteful.  So,
62  * if @int@ appears to be good enough, then I'll go with that.
63  */
64
65 #if UINT_MAX >= 0xffffffffu
66   typedef unsigned int uint32;
67 #else
68   typedef unsigned long uint32;
69 #endif
70
71 /* --- Decide on a 64-bit type --- *
72  *
73  * The test is quite subtle.  Think about it.  Note that (at least on my
74  * machine), the 32-bit macros are *much* faster than GCC's @long long@
75  * support.
76  */
77
78 #if defined(ULONG_LONG_MAX) && !defined(ULLONG_MAX)
79 #  define ULLONG_MAX ULONG_LONG_MAX
80 #endif
81
82 #if UINT_MAX >> 31 > 0xffffffff
83 #  define HAVE_UINT64
84    typedef unsigned int uint64;
85 #elif ULONG_MAX >> 31 > 0xffffffff
86 #  define HAVE_UINT64
87    typedef unsigned long uint64;
88 #elif defined(ULLONG_MAX)
89 #  define HAVE_UINT64
90    MLIB_BITS_EXTENSION typedef unsigned long long uint64;
91 #endif
92
93 #ifdef DEBUG64
94 #  undef HAVE_UINT64
95 #endif
96
97 #ifdef HAVE_UINT64
98   typedef struct { uint64 i; } kludge64;
99 #else
100   typedef struct { uint32 hi, lo; } kludge64;
101 #endif
102
103 /* --- Decide on a 24-bit type --- */
104
105 #if UINT_MAX >= 0x00ffffffu
106   typedef unsigned int uint24;
107 #else
108   typedef unsigned long uint24;
109 #endif
110
111 /* --- Decide on 16-bit and 8-bit types --- *
112  *
113  * This is more for brevity than anything else.
114  */
115
116 typedef unsigned short uint16;
117 typedef unsigned char octet, uint8;
118
119 /* --- WARNING! --- *
120  *
121  * Never lose sight of the fact that the above types may be wider than the
122  * names suggest.  Some architectures have 32-bit @short@s for example.
123  */
124
125 /*----- Macros ------------------------------------------------------------*/
126
127 /* --- Useful masks --- */
128
129 #define MASK8 0xffu
130 #define MASK16 0xffffu
131 #define MASK16_L MASK16
132 #define MASK16_B MASK16
133 #define MASK24 0xffffffu
134 #define MASK24_L MASK24
135 #define MASK24_B MASK24
136 #define MASK32 0xffffffffu
137 #define MASK32_L MASK32
138 #define MASK32_B MASK32
139
140 #ifdef HAVE_UINT64
141 #  define MASK64 MLIB_BITS_EXTENSION 0xffffffffffffffffu
142 #  define MASK64_L MASK64
143 #  define MASK64_B MASK64
144 #endif
145
146 /* --- Sizes --- */
147
148 #define SZ_8 1
149 #define SZ_16 2
150 #define SZ_16_L 2
151 #define SZ_16_B 2
152 #define SZ_24 3
153 #define SZ_24_L 3
154 #define SZ_24_B 3
155 #define SZ_32 4
156 #define SZ_32_L 4
157 #define SZ_32_B 4
158
159 #ifdef HAVE_UINT64
160 #  define SZ_64 8
161 #  define SZ_64_L 8
162 #  define SZ_64_B 8
163 #endif
164
165 /* --- Type aliases --- */
166
167 #define TY_U8 octet
168 #define TY_U16 uint16
169 #define TY_U16_L uint16
170 #define TY_U16_B uint16
171 #define TY_U24 uint24
172 #define TY_U24_L uint24
173 #define TY_U24_B uint24
174 #define TY_U32 uint32
175 #define TY_U32_L uint32
176 #define TY_U32_B uint32
177
178 #ifdef HAVE_UINT64
179 #  define TY_U64 uint64
180 #  define TY_U64_L uint64
181 #  define TY_U64_B uint64
182 #endif
183
184 /* --- List macros --- */
185
186 #ifdef HAVE_UINT64
187 #  define  DOUINTCONV(_)                                                \
188      _(8, 8, 8)                                                         \
189      _(16, 16, 16) _(16, 16_L, 16l) _(16, 16_B, 16b)                    \
190      _(24, 24, 24) _(24, 24_L, 24l) _(24, 24_B, 24b)                    \
191      _(32, 32, 32) _(32, 32_L, 32l) _(32, 32_B, 32b)                    \
192      _(64, 64, 64) _(64, 64_L, 64l) _(64, 64_B, 64b)
193 #  define DOUINTSZ(_) _(8) _(16) _(24) _(32) _(64)
194 #else
195 #  define  DOUINTCONV(_)                                                \
196      _(8, 8, 8)                                                         \
197      _(16, 16, 16) _(16, 16_L, 16l) _(16, 16_B, 16b)                    \
198      _(24, 24, 24) _(24, 24_L, 24l) _(24, 24_B, 24b)                    \
199      _(32, 32, 32) _(32, 32_L, 32l) _(32, 32_B, 32b)
200 #  define DOUINTSZ(_) _(8) _(16) _(24) _(32)
201 #endif
202
203 /* --- Type coercions --- */
204
205 #define U8(x) ((octet)((x) & MASK8))
206 #define U16(x) ((uint16)((x) & MASK16))
207 #define U24(x) ((uint24)((x) & MASK24))
208 #define U32(x) ((uint32)((x) & MASK32))
209
210 #ifdef HAVE_UINT64
211 #  define U64(x) ((uint64)(x) & MASK64)
212 #  define U64_(d, x) ((d).i = U64(x).i)
213 #else
214 #  define U64_(d, x) ((d).hi = U32((x).hi), (d).lo = U32((x).lo))
215 #endif
216
217 /* --- Safe shifting macros --- */
218
219 #define LSL8(v, s) U8(U8(v) << ((s) & 7u))
220 #define LSR8(v, s) U8(U8(v) >> ((s) & 7u))
221 #define LSL16(v, s) U16(U16(v) << ((s) & 15u))
222 #define LSR16(v, s) U16(U16(v) >> ((s) & 15u))
223 #define LSL24(v, s) U24(U24(v) << ((s) % 24u))
224 #define LSR24(v, s) U24(U24(v) >> ((s) % 24u))
225 #define LSL32(v, s) U32(U32(v) << ((s) & 31u))
226 #define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
227
228 #ifdef HAVE_UINT64
229 #  define LSL64(v, s) U64(U64(v) << ((s) & 63u))
230 #  define LSR64(v, s) U64(U64(v) >> ((s) & 63u))
231 #  define LSL64_(d, v, s) ((d).i = LSL64((v).i, (s)))
232 #  define LSR64_(d, v, s) ((d).i = LSR64((v).i, (s)))
233 #else
234 #  define LSL64_(d, v, s) do {                                          \
235      unsigned _s = (s) & 63u;                                           \
236      uint32 _l = (v).lo, _h = (v).hi;                                   \
237      kludge64 *_d = &(d);                                               \
238      if (_s >= 32) {                                                    \
239        _d->hi = LSL32(_l, _s - 32u);                                    \
240        _d->lo = 0;                                                      \
241      } else if (!_s) {                                                  \
242        _d->lo = _l;                                                     \
243        _d->hi = _h;                                                     \
244      } else {                                                           \
245        _d->hi = LSL32(_h, _s) | LSR32(_l, 32u - _s);                    \
246        _d->lo = LSL32(_l, _s);                                          \
247      }                                                                  \
248    } while (0)
249 #  define LSR64_(d, v, s) do {                                          \
250      unsigned _s = (s) & 63u;                                           \
251      uint32 _l = (v).lo, _h = (v).hi;                                   \
252      kludge64 *_d = &(d);                                               \
253      if (_s >= 32) {                                                    \
254        _d->lo = LSR32(_h, _s - 32u);                                    \
255        _d->hi = 0;                                                      \
256      } else if (!_s) {                                                  \
257        _d->lo = _l;                                                     \
258        _d->hi = _h;                                                     \
259      } else {                                                           \
260        _d->lo = LSR32(_l, _s) | LSL32(_h, 32u - _s);                    \
261        _d->hi = LSR32(_h, _s);                                          \
262      }                                                                  \
263    } while (0)
264 #endif
265
266 /* --- Rotation macros --- */
267
268 #define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s))))
269 #define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s))))
270 #define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s))))
271 #define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s))))
272 #define ROL24(v, s) (LSL24((v), (s)) | (LSR24((v), 24u - (s))))
273 #define ROR24(v, s) (LSR24((v), (s)) | (LSL24((v), 24u - (s))))
274 #define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
275 #define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
276
277 #ifdef HAVE_UINT64
278 #  define ROL64(v, s) (LSL64((v), (s)) | (LSR64((v), 64u - (s))))
279 #  define ROR64(v, s) (LSR64((v), (s)) | (LSL64((v), 64u - (s))))
280 #  define ROL64_(d, v, s) ((d).i = ROL64((v).i, (s)))
281 #  define ROR64_(d, v, s) ((d).i = ROR64((v).i, (s)))
282 #else
283 #  define ROL64_(d, v, s) do {                                          \
284      unsigned _s = (s) & 63u;                                           \
285      uint32 _l = (v).lo, _h = (v).hi;                                   \
286      kludge64 *_d = &(d);                                               \
287      if (_s > 32) {                                                     \
288        _d->hi = LSL32(_l, _s - 32u) | LSR32(_h, 64u - _s);              \
289        _d->lo = LSL32(_h, _s - 32u) | LSR32(_l, 64u - _s);              \
290      } else if (!_s) {                                                  \
291        _d->lo = _l;                                                     \
292        _d->hi = _h;                                                     \
293      } else if (_s == 32) {                                             \
294        _d->lo = _h;                                                     \
295        _d->hi = _l;                                                     \
296      } else {                                                           \
297        _d->hi = LSL32(_h, _s) | LSR32(_l, 32u - _s);                    \
298        _d->lo = LSL32(_l, _s) | LSR32(_h, 32u - _s);                    \
299      }                                                                  \
300    } while (0)
301 #  define ROR64_(d, v, s) do {                                          \
302      unsigned _s = (s) & 63u;                                           \
303      uint32 _l = (v).lo, _h = (v).hi;                                   \
304      kludge64 *_d = &(d);                                               \
305      if (_s > 32) {                                                     \
306        _d->hi = LSR32(_l, _s - 32u) | LSL32(_h, 64u - _s);              \
307        _d->lo = LSR32(_h, _s - 32u) | LSL32(_l, 64u - _s);              \
308      } else if (!_s) {                                                  \
309        _d->lo = _l;                                                     \
310        _d->hi = _h;                                                     \
311      } else if (_s == 32) {                                             \
312        _d->lo = _h;                                                     \
313        _d->hi = _l;                                                     \
314      } else {                                                           \
315        _d->hi = LSR32(_h, _s) | LSL32(_l, 32u - _s);                    \
316        _d->lo = LSR32(_l, _s) | LSL32(_h, 32u - _s);                    \
317      }                                                                  \
318    } while (0)
319 #endif
320
321 /* --- Endianness swapping --- */
322
323 #if GCC_VERSION_P(4, 8)
324 #  define ENDSWAP16(x) ((uint16)__builtin_bswap16(x))
325 #endif
326 #if GCC_VERSION_P(4, 3)
327 #  define ENDSWAP32(x) ((uint32)__builtin_bswap32(x))
328 #endif
329 #if GCC_VERSION_P(4, 3) && defined(HAVE_UINT64)
330 #  define ENDSWAP64(x) ((uint64)__builtin_bswap64(x))
331 #endif
332
333 #ifndef ENDSWAP8
334 #  define ENDSWAP8(x) U8(x)
335 #endif
336 #ifndef ENDSWAP16
337 #  define ENDSWAP16(x)                                                  \
338         ((((uint16)(x) >> 8)&0xff) |                                    \
339          (((uint16)(x)&0xff) << 8))
340 #endif
341 #ifndef ENDSWAP24
342 #  define ENDSWAP24(x)                                                  \
343         ((((uint24)(x) >> 16)&0xff) |                                   \
344          ((uint24)(x)&0xff00) |                                         \
345          ((uint24)((x)&0xff) << 16))
346 #endif
347 #ifndef ENDSWAP32
348 #  define ENDSWAP32(x)                                                  \
349         (ENDSWAP16(((uint32)(x) >> 16)&0xffff) |                        \
350          ((uint32)ENDSWAP16((x)&0xffff) << 16))
351 #endif
352 #if defined(HAVE_UINT64) && !defined(ENDSWAP64)
353 #  define ENDSWAP64(x)                                                  \
354         (ENDSWAP32(((uint64)(x) >> 32)&0xffffffff) |                    \
355          ((uint64)ENDSWAP32((x)&0xffffffff) << 32))
356 #endif
357 #ifdef HAVE_UINT64
358 #  define ENDSWAP64_(z, x)                                              \
359         ((z).i = ENDSWAP64((x).i))
360 #else
361 #  define ENDSWAP64_(z, x)                                              \
362         ((z).lo = ENDSWAP32((x).hi),                                    \
363          (z).hi = ENDSWAP32((x).lo))
364 #endif
365
366 #define MLIB_LITTLE_ENDIAN 1234
367 #define MLIB_BIG_ENDIAN 4321
368 #if defined(__ORDER_LITTLE_ENDIAN__) &&                                 \
369     __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
370 #  define MLIB_BYTE_ORDER MLIB_LITTLE_ENDIAN
371 #elif defined(__ORDER_BIG_ENDIAN__) &&                                  \
372       __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
373 #  define MLIB_BYTE_ORDER MLIB_BIG_ENDIAN
374 #endif
375
376 #if MLIB_BYTE_ORDER == MLIB_LITTLE_ENDIAN
377 #  define HTOL16(x) (x)
378 #  define LTOH16(x) (x)
379 #  define HTOB16(x) ENDSWAP16(x)
380 #  define BTOH16(x) ENDSWAP16(x)
381 #  define HTOL24(x) (x)
382 #  define LTOH24(x) (x)
383 #  define HTOB24(x) ENDSWAP24(x)
384 #  define BTOH24(x) ENDSWAP24(x)
385 #  define HTOL32(x) (x)
386 #  define LTOH32(x) (x)
387 #  define HTOB32(x) ENDSWAP32(x)
388 #  define BTOH32(x) ENDSWAP32(x)
389 #  ifdef HAVE_UINT64
390 #    define HTOL64(x) (x)
391 #    define LTOH64(x) (x)
392 #    define HTOB64(x) ENDSWAP64(x)
393 #    define BTOH64(x) ENDSWAP64(x)
394 #  endif
395 #  define HTOL64_(z, x) ASSIGN64(z, x)
396 #  define LTOH64_(z, x) ASSIGN64(z, x)
397 #  define HTOB64_(z, x) ENDSWAP64_(z, x)
398 #  define BTOH64_(z, x) ENDSWAP64_(z, x)
399 #elif MLIB_BYTE_ORDER == MLIB_BIG_ENDIAN
400 #  define HTOL16(x) ENDSWAP16(x)
401 #  define LTOH16(x) ENDSWAP16(x)
402 #  define HTOB16(x) (x)
403 #  define BTOH16(x) (x)
404 #  define HTOL24(x) ENDSWAP24(x)
405 #  define LTOH24(x) ENDSWAP24(x)
406 #  define HTOB24(x) (x)
407 #  define BTOH24(x) (x)
408 #  define HTOL32(x) ENDSWAP32(x)
409 #  define LTOH32(x) ENDSWAP32(x)
410 #  define HTOB32(x) (x)
411 #  define BTOH32(x) (x)
412 #  ifdef HAVE_UINT64
413 #    define HTOL64(x) ENDSWAP64(x)
414 #    define LTOH64(x) ENDSWAP64(x)
415 #    define HTOB64(x) (x)
416 #    define BTOH64(x) (x)
417 #    define HTOL64_(z, x) ENDSWAP64_(z, x)
418 #    define LTOH64_(z, x) ENDSWAP64_(z, x)
419 #    define HTOB64_(z, x) ((z).i = (x).i)
420 #    define BTOH64_(z, x) ((z).i = (x).i)
421 #  endif
422 #  define HTOL64_(z, x) ENDSWAP64_(z, x)
423 #  define LTOH64_(z, x) ENDSWAP64_(z, x)
424 #  define HTOB64_(z, x) ASSIGN64(z, x)
425 #  define BTOH64_(z, x) ASSIGN64(z, x)
426 #endif
427
428 /* --- Unaligned access (GCC-specific) --- */
429
430 #if GCC_VERSION_P(3, 3) && CHAR_BIT == 8
431 #  define MLIB_MISALIGNED __attribute__((aligned(1), may_alias))
432 #  if __SIZEOF_SHORT__ == 2
433      typedef MLIB_MISALIGNED unsigned short misaligned_uint16;
434 #    define RAW16(p) (*(misaligned_uint16 *)(p))
435 #  endif
436 #  if __SIZEOF_INT__ == 4
437      typedef MLIB_MISALIGNED unsigned int misaligned_uint32;
438 #    define RAW32(p) (*(misaligned_uint32 *)(p))
439 #  elif __SIZEOF_LONG__ == 4
440      typedef MLIB_MISALIGNED unsigned long misaligned_uint32;
441 #    define RAW32(p) (*(misaligned_uint32 *)(p))
442 #  endif
443 #  if __SIZEOF_LONG__ == 8
444      typedef MLIB_MISALIGNED unsigned long misaligned_uint64;
445 #    define RAW64(p) (*(misaligned_uint64 *)(p))
446 #  elif __SIZEOF_LONG_LONG__ == 8
447      typedef MLIB_MISALIGNED unsigned long long misaligned_uint64;
448 #    define RAW64(p) (*(misaligned_uint64 *)(p))
449 #  endif
450 #endif
451
452 /* --- Storage and retrieval --- */
453
454 #if defined(RAW16) && defined(LTOH16)
455 #  define LOAD16_L(p) LTOH16(RAW16(p))
456 #endif
457 #if defined(RAW16) && defined(HTOL16)
458 #  define STORE16_L(p, x) (RAW16(p) = HTOL16(x))
459 #endif
460 #if defined(RAW16) && defined(BTOH16)
461 #  define LOAD16_B(p) BTOH16(RAW16(p))
462 #endif
463 #if defined(RAW16) && defined(HTOB16)
464 #  define STORE16_B(p, x) (RAW16(p) = HTOB16(x))
465 #endif
466
467 #if defined(RAW32) && defined(LTOH32)
468 #  define LOAD32_L(p) LTOH32(RAW32(p))
469 #endif
470 #if defined(RAW32) && defined(HTOL32)
471 #  define STORE32_L(p, x) (RAW32(p) = HTOL32(x))
472 #endif
473 #if defined(RAW32) && defined(BTOH32)
474 #  define LOAD32_B(p) BTOH32(RAW32(p))
475 #endif
476 #if defined(RAW32) && defined(HTOB32)
477 #  define STORE32_B(p, x) (RAW32(p) = HTOB32(x))
478 #endif
479
480 #if defined(RAW64) && defined(LTOH64)
481 #  define LOAD64_L(p) LTOH64(RAW64(p))
482 #endif
483 #if defined(RAW64) && defined(HTOL64)
484 #  define STORE64_L(p, x) (RAW64(p) = HTOL64(x))
485 #endif
486 #if defined(RAW64) && defined(BTOH64)
487 #  define LOAD64_B(p) BTOH64(RAW64(p))
488 #endif
489 #if defined(RAW64) && defined(HTOB64)
490 #  define STORE64_B(p, x) (RAW64(p) = HTOB64(x))
491 #endif
492
493 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
494 #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
495
496 #define LOAD8(p) (GETBYTE((p), 0))
497 #define STORE8(p, v) (PUTBYTE((p), 0, (v)))
498
499 #ifndef LOAD16_B
500 #  define LOAD16_B(p)
501         (((uint16)GETBYTE((p), 0) << 8) |                               \
502          ((uint16)GETBYTE((p), 1) << 0))
503 #endif
504 #ifndef LOAD16_L
505 #  define LOAD16_L(p)                                                   \
506         (((uint16)GETBYTE((p), 0) << 0) |                               \
507          ((uint16)GETBYTE((p), 1) << 8))
508 #endif
509 #define LOAD16(p) LOAD16_B((p))
510
511 #ifndef STORE16_B
512 #  define STORE16_B(p, v)                                               \
513         (PUTBYTE((p), 0, (uint16)(v) >> 8),                             \
514          PUTBYTE((p), 1, (uint16)(v) >> 0))
515 #endif
516 #ifndef STORE16_L
517 #  define STORE16_L(p, v)                                               \
518         (PUTBYTE((p), 0, (uint16)(v) >> 0),                             \
519          PUTBYTE((p), 1, (uint16)(v) >> 8))
520 #endif
521 #define STORE16(p, v) STORE16_B((p), (v))
522
523 #ifndef LOAD24_B
524 #  define LOAD24_B(p)                                                   \
525         (((uint24)GETBYTE((p), 0)            << 16) |                   \
526          ((uint24)LOAD16_B((octet *)(p) + 1) <<  0))
527 #endif
528 #ifndef LOAD24_L
529 #  define LOAD24_L(p)                                                   \
530         (((uint24)LOAD16_L((octet *)(p) + 0) <<  0) |                   \
531          ((uint24)GETBYTE((p), 2)            << 16))
532 #endif
533 #define LOAD24(p) LOAD24_B((p))
534
535 #ifndef STORE24_B
536 #  define STORE24_B(p, v)                                               \
537         (PUTBYTE((p), 0,             (uint24)(v) >> 16),                \
538          STORE16_B((octet *)(p) + 1, (uint24)(v) >>  0))
539 #endif
540 #ifndef STORE24_L
541 #  define STORE24_L(p, v)                                               \
542         (STORE16_L((octet *)(p) + 0, (uint24)(v) >>  0),                \
543          PUTBYTE((p), 2,             (uint24)(v) >> 16))
544 #endif
545 #define STORE24(p, v) STORE24_B((p), (v))
546
547 #ifndef LOAD32_B
548 #  define LOAD32_B(p)                                                   \
549         (((uint32)LOAD16_B((octet *)(p) + 0) << 16) |                   \
550          ((uint32)LOAD16_B((octet *)(p) + 2) <<  0))
551 #endif
552 #ifndef LOAD32_L
553 #  define LOAD32_L(p)                                                   \
554         (((uint32)LOAD16_L((octet *)(p) + 0) <<  0) |                   \
555          ((uint32)LOAD16_L((octet *)(p) + 2) << 16))
556 #endif
557 #define LOAD32(p) LOAD32_B((p))
558
559 #ifndef STORE32_B
560 #  define STORE32_B(p, v)                                               \
561         (STORE16_B((octet *)(p) + 0, (uint32)(v) >> 16),                \
562          STORE16_B((octet *)(p) + 2, (uint32)(v) >>  0))
563 #endif
564 #ifndef STORE32_L
565 #  define STORE32_L(p, v)                                               \
566         (STORE16_L((octet *)(p) + 0, (uint32)(v) >>  0),                \
567          STORE16_L((octet *)(p) + 2, (uint32)(v) >> 16))
568 #endif
569 #define STORE32(p, v) STORE32_B((p), (v))
570
571 #ifdef HAVE_UINT64
572
573 #  ifndef LOAD64_B
574 #    define LOAD64_B(p)                                                 \
575         (((uint64)LOAD32_B((octet *)(p) + 0) << 32) |                   \
576          ((uint64)LOAD32_B((octet *)(p) + 4) <<  0))
577 #  endif
578 #  ifndef LOAD64_L
579 #    define LOAD64_L(p)                                                 \
580         (((uint64)LOAD32_L((octet *)(p) + 0) <<  0) |                   \
581          ((uint64)LOAD32_L((octet *)(p) + 4) << 32))
582 #  endif
583 #  define LOAD64(p) LOAD64_B((p))
584 #  define LOAD64_B_(d, p) ((d).i = LOAD64_B((p)))
585 #  define LOAD64_L_(d, p) ((d).i = LOAD64_L((p)))
586 #  define LOAD64_(d, p) LOAD64_B_((d), (p))
587
588 #  ifndef STORE64_B
589 #    define STORE64_B(p, v)                                             \
590         (STORE32_B((octet *)(p) + 0, (uint64)(v) >> 32),                \
591          STORE32_B((octet *)(p) + 4, (uint64)(v) >>  0))
592 #  endif
593 #  ifndef STORE64_L
594 #    define STORE64_L(p, v)                                             \
595         (STORE32_L((octet *)(p) + 0, (uint64)(v) >>  0),                \
596          STORE32_L((octet *)(p) + 4, (uint64)(v) >> 32))
597 #  endif
598 #  define STORE64(p, v) STORE64_B((p), (v))
599 #  define STORE64_B_(p, v) STORE64_B((p), (v).i)
600 #  define STORE64_L_(p, v) STORE64_L((p), (v).i)
601 #  define STORE64_(p, v) STORE64_B_((p), (v))
602
603 #else
604
605 #  define LOAD64_B_(d, p)                                               \
606         ((d).hi = LOAD32_B((octet *)(p) + 0),                           \
607          (d).lo = LOAD32_B((octet *)(p) + 4))
608 #  define LOAD64_L_(d, p)                                               \
609         ((d).lo = LOAD32_L((octet *)(p) + 0),                           \
610          (d).hi = LOAD32_L((octet *)(p) + 4))
611 #  define LOAD64_(d, p) LOAD64_B_((d), (p))
612
613 #  define STORE64_B_(p, v)                                              \
614         (STORE32_B((octet *)(p) + 0, (v).hi),                           \
615          STORE32_B((octet *)(p) + 4, (v).lo))
616 #  define STORE64_L_(p, v)                                              \
617         (STORE32_L((octet *)(p) + 0, (v).lo),                           \
618          STORE32_L((octet *)(p) + 4, (v).hi))
619 #  define STORE64_(p, v) STORE64_B_((p), (v))
620
621 #endif
622
623 /* --- Other operations on 64-bit integers --- */
624
625 #ifdef HAVE_UINT64
626 #  define SET64(d, h, l) ((d).i = (U64((h)) << 32) | U64((l)))
627 #  define ASSIGN64(d, x) ((d).i = U64((x)))
628 #  define HI64(x) U32((x).i >> 32)
629 #  define LO64(x) U32((x).i)
630 #  define GET64(t, x) ((t)(x).i)
631 #else
632 #  define SET64(d, h, l) ((d).hi = U32(h), (d).lo = U32(l))
633 #  define ASSIGN64(d, x)                                                \
634     ((d).hi = ((x & ~MASK32) >> 16) >> 16, (d).lo = U32(x))
635 #  define HI64(x) U32((x).hi)
636 #  define LO64(x) U32((x).lo)
637 #  define GET64(t, x) (((((t)HI64(x) << 16) << 16) & ~MASK32) | (t)LO64(x))
638 #endif
639
640 #ifdef HAVE_UINT64
641 #  define AND64(d, x, y) ((d).i = (x).i & (y).i)
642 #  define OR64(d, x, y) ((d).i = (x).i | (y).i)
643 #  define XOR64(d, x, y) ((d).i = (x).i ^ (y).i)
644 #  define CPL64(d, x) ((d).i = ~(x).i)
645 #  define ADD64(d, x, y) ((d).i = (x).i + (y).i)
646 #  define SUB64(d, x, y) ((d).i = (x).i - (y).i)
647 #  define CMP64(x, op, y) ((x).i op (y).i)
648 #  define ZERO64(x) ((x) == 0)
649 #else
650 #  define AND64(d, x, y) ((d).lo = (x).lo & (y).lo, (d).hi = (x).hi & (y).hi)
651 #  define OR64(d, x, y) ((d).lo = (x).lo | (y).lo, (d).hi = (x).hi | (y).hi)
652 #  define XOR64(d, x, y) ((d).lo = (x).lo ^ (y).lo, (d).hi = (x).hi ^ (y).hi)
653 #  define CPL64(d, x) ((d).lo = ~(x).lo, (d).hi = ~(x).hi)
654 #  define ADD64(d, x, y) do {                                           \
655      uint32 _x = U32((x).lo + (y).lo);                                  \
656      (d).hi = (x).hi + (y).hi + (_x < (x).lo);                          \
657      (d).lo = _x;                                                       \
658    } while (0)
659 #  define SUB64(d, x, y) do {                                           \
660      uint32 _x = U32((x).lo - (y).lo);                                  \
661      (d).hi = (x).hi - (y).hi - (_x > (x).lo);                          \
662      (d).lo = _x;                                                       \
663    } while (0)
664 #  define CMP64(x, op, y)                                               \
665     ((x).hi == (y).hi ? (x).lo op (y).lo : (x).hi op (y).hi)
666 #  define ZERO64(x) ((x).lo == 0 && (x).hi == 0)
667 #endif
668
669 /* --- Storing integers in tables --- */
670
671 #ifdef HAVE_UINT64
672 #  define X64(x, y) { 0x##x##y }
673 #else
674 #  define X64(x, y) { 0x##x, 0x##y }
675 #endif
676
677 /*----- That's all, folks -------------------------------------------------*/
678
679 #ifdef __cplusplus
680   }
681 #endif
682
683 #endif