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