3 #ifndef HED__UTIL_NUMBERS_H
4 #define HED__UTIL_NUMBERS_H
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
;
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. */
37 min(const bint a
, const bint b
)
43 max(const bint a
, const bint b
)
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; }
64 /* Returns 1 if running on a little-endian architecture.
65 * Hopefully, this will be always optimized to a constant expression.
68 arch_little_endian(void)