utils/bits.h: Refactor the load/store macros.
[mLib] / utils / bits.h
CommitLineData
6b033bc4 1/* -*-c-*-
2 *
6b033bc4 3 * Portable bit-level manipulation macros
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
6b033bc4 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.
d4efbcd9 16 *
6b033bc4 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.
d4efbcd9 21 *
6b033bc4 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
c6e0eaf0 28#ifndef MLIB_BITS_H
29#define MLIB_BITS_H
6b033bc4 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Header files ------------------------------------------------------*/
36
37#include <limits.h>
38#include <stddef.h>
a6f4a484 39#if __STDC_VERSION__ >= 199900l
40# include <stdint.h>
41#endif
6b033bc4 42
43/*----- Decide on some types ----------------------------------------------*/
44
1f175471 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
6b033bc4 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
a6f4a484 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
1f175471 79# define HAVE_UINT64
80 typedef unsigned int uint64;
a6f4a484 81#elif ULONG_MAX >> 31 > 0xffffffff
1f175471 82# define HAVE_UINT64
83 typedef unsigned long uint64;
a6f4a484 84#elif defined(ULLONG_MAX)
1f175471 85# define HAVE_UINT64
86 MLIB_BITS_EXTENSION typedef unsigned long long uint64;
a6f4a484 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
6a0129ea 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
6b033bc4 107/* --- Decide on 16-bit and 8-bit types --- *
108 *
109 * This is more for brevity than anything else.
110 */
111
112typedef unsigned short uint16;
9b5ac6ff 113typedef unsigned char octet, uint8;
6b033bc4 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
9b5ac6ff 127#define MASK16_L MASK16
128#define MASK16_B MASK16
6a0129ea 129#define MASK24 0xffffffu
9b5ac6ff 130#define MASK24_L MASK24
131#define MASK24_B MASK24
6b033bc4 132#define MASK32 0xffffffffu
9b5ac6ff 133#define MASK32_L MASK32
134#define MASK32_B MASK32
6b033bc4 135
a6f4a484 136#ifdef HAVE_UINT64
1f175471 137# define MASK64 MLIB_BITS_EXTENSION 0xffffffffffffffffu
9b5ac6ff 138# define MASK64_L MASK64
139# define MASK64_B MASK64
a6f4a484 140#endif
141
76a7638e 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
9b5ac6ff 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)
5ce3df29 189# define DOUINTSZ(_) _(8) _(16) _(24) _(32) _(64)
9b5ac6ff 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)
5ce3df29 196# define DOUINTSZ(_) _(8) _(16) _(24) _(32)
9b5ac6ff 197#endif
198
6b033bc4 199/* --- Type coercions --- */
200
201#define U8(x) ((octet)((x) & MASK8))
202#define U16(x) ((uint16)((x) & MASK16))
6a0129ea 203#define U24(x) ((uint24)((x) & MASK24))
6b033bc4 204#define U32(x) ((uint32)((x) & MASK32))
205
a6f4a484 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
6b033bc4 213/* --- Safe shifting macros --- */
214
bc9f60b6 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))
6a0129ea 219#define LSL24(v, s) U24(U24(v) << ((s) % 24u))
220#define LSR24(v, s) U24(U24(v) >> ((s) % 24u))
bc9f60b6 221#define LSL32(v, s) U32(U32(v) << ((s) & 31u))
222#define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
6b033bc4 223
a6f4a484 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
6b033bc4 262/* --- Rotation macros --- */
263
bc9f60b6 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))))
6a0129ea 268#define ROL24(v, s) (LSL24((v), (s)) | (LSR24((v), 24u - (s))))
269#define ROR24(v, s) (LSR24((v), (s)) | (LSL24((v), 24u - (s))))
bc9f60b6 270#define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
271#define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
6b033bc4 272
a6f4a484 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); \
5892fd39 283 if (_s > 32) { \
a6f4a484 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; \
5892fd39 289 } else if (_s == 32) { \
290 _d->lo = _h; \
291 _d->hi = _l; \
a6f4a484 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); \
5892fd39 301 if (_s > 32) { \
a6f4a484 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; \
5892fd39 307 } else if (_s == 32) { \
308 _d->lo = _h; \
309 _d->hi = _l; \
a6f4a484 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
6b033bc4 317/* --- Storage and retrieval --- */
318
319#define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
320#define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
321
322#define LOAD8(p) (GETBYTE((p), 0))
323#define STORE8(p, v) (PUTBYTE((p), 0, (v)))
324
897c58ad
MW
325#ifndef LOAD16_B
326# define LOAD16_B(p)
327 (((uint16)GETBYTE((p), 0) << 8) | \
328 ((uint16)GETBYTE((p), 1) << 0))
329#endif
330#ifndef LOAD16_L
331# define LOAD16_L(p) \
332 (((uint16)GETBYTE((p), 0) << 0) | \
333 ((uint16)GETBYTE((p), 1) << 8))
334#endif
6b033bc4 335#define LOAD16(p) LOAD16_B((p))
336
897c58ad
MW
337#ifndef STORE16_B
338# define STORE16_B(p, v) \
339 (PUTBYTE((p), 0, (uint16)(v) >> 8), \
340 PUTBYTE((p), 1, (uint16)(v) >> 0))
341#endif
342#ifndef STORE16_L
343# define STORE16_L(p, v) \
344 (PUTBYTE((p), 0, (uint16)(v) >> 0), \
345 PUTBYTE((p), 1, (uint16)(v) >> 8))
346#endif
6b033bc4 347#define STORE16(p, v) STORE16_B((p), (v))
348
897c58ad
MW
349#ifndef LOAD24_B
350# define LOAD24_B(p) \
351 (((uint24)GETBYTE((p), 0) << 16) | \
352 ((uint24)LOAD16_B((octet *)(p) + 1) << 0))
353#endif
354#ifndef LOAD24_L
355# define LOAD24_L(p) \
356 (((uint24)LOAD16_L((octet *)(p) + 0) << 0) | \
357 ((uint24)GETBYTE((p), 2) << 16))
358#endif
6a0129ea 359#define LOAD24(p) LOAD24_B((p))
360
897c58ad
MW
361#ifndef STORE24_B
362# define STORE24_B(p, v) \
363 (PUTBYTE((p), 0, (uint24)(v) >> 16), \
364 STORE16_B((octet *)(p) + 1, (uint24)(v) >> 0))
365#endif
366#ifndef STORE24_L
367# define STORE24_L(p, v) \
368 (STORE16_L((octet *)(p) + 0, (uint24)(v) >> 0), \
369 PUTBYTE((p), 2, (uint24)(v) >> 16))
370#endif
6a0129ea 371#define STORE24(p, v) STORE24_B((p), (v))
372
897c58ad
MW
373#ifndef LOAD32_B
374# define LOAD32_B(p) \
375 (((uint32)LOAD16_B((octet *)(p) + 0) << 16) | \
376 ((uint32)LOAD16_B((octet *)(p) + 2) << 0))
377#endif
378#ifndef LOAD32_L
379# define LOAD32_L(p) \
380 (((uint32)LOAD16_L((octet *)(p) + 0) << 0) | \
381 ((uint32)LOAD16_L((octet *)(p) + 2) << 16))
382#endif
6b033bc4 383#define LOAD32(p) LOAD32_B((p))
384
897c58ad
MW
385#ifndef STORE32_B
386# define STORE32_B(p, v) \
387 (STORE16_B((octet *)(p) + 0, (uint32)(v) >> 16), \
388 STORE16_B((octet *)(p) + 2, (uint32)(v) >> 0))
389#endif
390#ifndef STORE32_L
391# define STORE32_L(p, v) \
392 (STORE16_L((octet *)(p) + 0, (uint32)(v) >> 0), \
393 STORE16_L((octet *)(p) + 2, (uint32)(v) >> 16))
394#endif
6b033bc4 395#define STORE32(p, v) STORE32_B((p), (v))
396
a6f4a484 397#ifdef HAVE_UINT64
398
897c58ad
MW
399# ifndef LOAD64_B
400# define LOAD64_B(p) \
401 (((uint64)LOAD32_B((octet *)(p) + 0) << 32) | \
402 ((uint64)LOAD32_B((octet *)(p) + 4) << 0))
403# endif
404# ifndef LOAD64_L
405# define LOAD64_L(p) \
406 (((uint64)LOAD32_L((octet *)(p) + 0) << 0) | \
407 ((uint64)LOAD32_L((octet *)(p) + 4) << 32))
408# endif
a6f4a484 409# define LOAD64(p) LOAD64_B((p))
410# define LOAD64_B_(d, p) ((d).i = LOAD64_B((p)))
411# define LOAD64_L_(d, p) ((d).i = LOAD64_L((p)))
412# define LOAD64_(d, p) LOAD64_B_((d), (p))
413
897c58ad
MW
414# ifndef STORE64_B
415# define STORE64_B(p, v) \
416 (STORE32_B((octet *)(p) + 0, (uint64)(v) >> 32), \
417 STORE32_B((octet *)(p) + 4, (uint64)(v) >> 0))
418# endif
419# ifndef STORE64_L
420# define STORE64_L(p, v) \
421 (STORE32_L((octet *)(p) + 0, (uint64)(v) >> 0), \
422 STORE32_L((octet *)(p) + 4, (uint64)(v) >> 32))
423# endif
a6f4a484 424# define STORE64(p, v) STORE64_B((p), (v))
425# define STORE64_B_(p, v) STORE64_B((p), (v).i)
426# define STORE64_L_(p, v) STORE64_L((p), (v).i)
427# define STORE64_(p, v) STORE64_B_((p), (v))
428
429#else
430
431# define LOAD64_B_(d, p) \
432 ((d).hi = LOAD32_B((octet *)(p) + 0), \
433 (d).lo = LOAD32_B((octet *)(p) + 4))
434# define LOAD64_L_(d, p) \
435 ((d).lo = LOAD32_L((octet *)(p) + 0), \
436 (d).hi = LOAD32_L((octet *)(p) + 4))
437# define LOAD64_(d, p) LOAD64_B_((d), (p))
438
439# define STORE64_B_(p, v) \
440 (STORE32_B((octet *)(p) + 0, (v).hi), \
441 STORE32_B((octet *)(p) + 4, (v).lo))
442# define STORE64_L_(p, v) \
443 (STORE32_L((octet *)(p) + 0, (v).lo), \
444 STORE32_L((octet *)(p) + 4, (v).hi))
445# define STORE64_(p, v) STORE64_B_((p), (v))
446
447#endif
448
449/* --- Other operations on 64-bit integers --- */
450
451#ifdef HAVE_UINT64
452# define SET64(d, h, l) ((d).i = (U64((h)) << 32) | U64((l)))
453# define ASSIGN64(d, x) ((d).i = U64((x)))
454# define HI64(x) U32((x).i >> 32)
455# define LO64(x) U32((x).i)
3b7cea93 456# define GET64(t, x) ((t)(x).i)
a6f4a484 457#else
458# define SET64(d, h, l) ((d).hi = U32(h), (d).lo = U32(l))
459# define ASSIGN64(d, x) \
460 ((d).hi = ((x & ~MASK32) >> 16) >> 16, (d).lo = U32(x))
461# define HI64(x) U32((x).hi)
462# define LO64(x) U32((x).lo)
3b7cea93 463# define GET64(t, x) (((((t)HI64(x) << 16) << 16) & ~MASK32) | (t)LO64(x))
a6f4a484 464#endif
465
466#ifdef HAVE_UINT64
467# define AND64(d, x, y) ((d).i = (x).i & (y).i)
468# define OR64(d, x, y) ((d).i = (x).i | (y).i)
469# define XOR64(d, x, y) ((d).i = (x).i ^ (y).i)
470# define CPL64(d, x) ((d).i = ~(x).i)
471# define ADD64(d, x, y) ((d).i = (x).i + (y).i)
472# define SUB64(d, x, y) ((d).i = (x).i - (y).i)
473# define CMP64(x, op, y) ((x).i op (y).i)
474# define ZERO64(x) ((x) == 0)
475#else
476# define AND64(d, x, y) ((d).lo = (x).lo & (y).lo, (d).hi = (x).hi & (y).hi)
477# define OR64(d, x, y) ((d).lo = (x).lo | (y).lo, (d).hi = (x).hi | (y).hi)
478# define XOR64(d, x, y) ((d).lo = (x).lo ^ (y).lo, (d).hi = (x).hi ^ (y).hi)
479# define CPL64(d, x) ((d).lo = ~(x).lo, (d).hi = ~(x).hi)
480# define ADD64(d, x, y) do { \
481 uint32 _x = U32((x).lo + (y).lo); \
482 (d).hi = (x).hi + (y).hi + (_x < (x).lo); \
483 (d).lo = _x; \
484 } while (0)
485# define SUB64(d, x, y) do { \
486 uint32 _x = U32((x).lo - (y).lo); \
487 (d).hi = (x).hi - (y).hi - (_x > (x).lo); \
488 (d).lo = _x; \
489 } while (0)
490# define CMP64(x, op, y) \
491 ((x).hi == (y).hi ? (x).lo op (y).lo : (x).hi op (y).hi)
492# define ZERO64(x) ((x).lo == 0 && (x).hi == 0)
493#endif
494
91928c79 495/* --- Storing integers in tables --- */
496
497#ifdef HAVE_UINT64
498# define X64(x, y) { 0x##x##y }
499#else
500# define X64(x, y) { 0x##x, 0x##y }
501#endif
d4efbcd9 502
6b033bc4 503/*----- That's all, folks -------------------------------------------------*/
504
505#ifdef __cplusplus
506 }
507#endif
508
509#endif