Move all block size reductions to shrink_block()
[hed.git] / util / numbers.h
blob67086c081fe20a90c695a230efcf67d8de3e937c
1 /* $Id$ */
3 #ifndef HED__UTIL_NUMBERS_H
4 #define HED__UTIL_NUMBERS_H
6 /*
7 * Bilbo was going to be eleventy-one, 111, a rather curious number and a very
8 * respectable age for a hobbit (the Old Took himself had only reached 130);
9 * and Frodo was going to be thirty-three, 33, an important number: the date
10 * of his 'coming of age'.
13 /* Maximal values of off_t and uoff_t.
14 * uoff_t is unsigned, while off_t is signed; in 2's complement
15 * arithmetic, this means that UOFF_MAX is all 1's, and OFF_MAX
16 * has only the highest bit clear.
18 #define UOFF_MAX (~(hed_uoff_t)(0))
19 #define OFF_MAX (hed_off_t)(UOFF_MAX >> 1)
21 /* The biggest possible integer type. */
22 typedef BINT_TYPE bint;
23 typedef unsigned BINT_TYPE ubint;
25 /* Minimax. */
26 static bint min(const bint a, const bint b);
27 static bint max(const bint a, const bint b);
29 /* Find the highest bit. */
30 static int bitsize(ubint x);
34 /**** Inline functions and such. */
36 static inline bint
37 min(const bint a, const bint b)
39 return a < b ? a : b;
42 static inline bint
43 max(const bint a, const bint b)
45 return a > b ? a : b;
48 static inline int
49 bitsize(ubint x)
51 int r = 0;
52 /* FIXME: only up to 64bits */
53 if (x & 0xffffffff00000000ULL) { x >>= 32; r += 32; }
54 if (x & 0x00000000ffff0000ULL) { x >>= 16; r += 16; }
55 if (x & 0x000000000000ff00ULL) { x >>= 8; r += 8; }
56 if (x & 0x00000000000000f0ULL) { x >>= 4; r += 4; }
57 if (x & 0x000000000000000cULL) { x >>= 2; r += 2; }
58 if (x & 0x0000000000000002ULL) { r += 1; }
59 return r;
62 /**** Endianity. */
64 /* Returns 1 if running on a little-endian architecture.
65 * Hopefully, this will be always optimized to a constant expression.
67 static inline int
68 arch_little_endian(void)
70 union {
71 long x;
72 char c;
73 } u = { 1 };
74 return u.c;
77 #endif