Add lg_ceil to bit_util.

Also, add the bit_util test back to the Makefile.
This commit is contained in:
David Goldblatt 2017-12-22 15:14:44 -08:00 committed by David Goldblatt
parent 07b89c7673
commit 2f07e92adb
3 changed files with 76 additions and 68 deletions

View File

@ -167,6 +167,7 @@ TESTS_UNIT := \
$(srcroot)test/unit/background_thread_enable.c \ $(srcroot)test/unit/background_thread_enable.c \
$(srcroot)test/unit/base.c \ $(srcroot)test/unit/base.c \
$(srcroot)test/unit/bitmap.c \ $(srcroot)test/unit/bitmap.c \
$(srcroot)test/unit/bit_util.c \
$(srcroot)test/unit/ckh.c \ $(srcroot)test/unit/ckh.c \
$(srcroot)test/unit/decay.c \ $(srcroot)test/unit/decay.c \
$(srcroot)test/unit/div.c \ $(srcroot)test/unit/div.c \

View File

@ -160,74 +160,27 @@ lg_floor(size_t x) {
} }
#endif #endif
BIT_UTIL_INLINE unsigned
lg_ceil(size_t x) {
return lg_floor(x) + ((x & (x - 1)) == 0 ? 0 : 1);
}
#undef BIT_UTIL_INLINE #undef BIT_UTIL_INLINE
/* A compile-time version of lg_ceil */ /* A compile-time version of lg_floor and lg_ceil. */
#define LG_CEIL(x) ( \ #define LG_FLOOR_1(x) 0
(x) <= (1ULL << 0ULL) ? 0 : \ #define LG_FLOOR_2(x) (x < (1ULL << 1) ? LG_FLOOR_1(x) : 1 + LG_FLOOR_1(x >> 1))
(x) <= (1ULL << 1ULL) ? 1 : \ #define LG_FLOOR_4(x) (x < (1ULL << 2) ? LG_FLOOR_2(x) : 2 + LG_FLOOR_2(x >> 2))
(x) <= (1ULL << 2ULL) ? 2 : \ #define LG_FLOOR_8(x) (x < (1ULL << 4) ? LG_FLOOR_4(x) : 4 + LG_FLOOR_4(x >> 4))
(x) <= (1ULL << 3ULL) ? 3 : \ #define LG_FLOOR_16(x) (x < (1ULL << 8) ? LG_FLOOR_8(x) : 8 + LG_FLOOR_8(x >> 8))
(x) <= (1ULL << 4ULL) ? 4 : \ #define LG_FLOOR_32(x) (x < (1ULL << 16) ? LG_FLOOR_16(x) : 16 + LG_FLOOR_16(x >> 16))
(x) <= (1ULL << 5ULL) ? 5 : \ #define LG_FLOOR_64(x) (x < (1ULL << 32) ? LG_FLOOR_32(x) : 32 + LG_FLOOR_32(x >> 32))
(x) <= (1ULL << 6ULL) ? 6 : \ #if LG_SIZEOF_PTR == 2
(x) <= (1ULL << 7ULL) ? 7 : \ # define LG_FLOOR(x) LG_FLOOR_32((x))
(x) <= (1ULL << 8ULL) ? 8 : \ #else
(x) <= (1ULL << 9ULL) ? 9 : \ # define LG_FLOOR(x) LG_FLOOR_64((x))
(x) <= (1ULL << 10ULL) ? 10 : \ #endif
(x) <= (1ULL << 11ULL) ? 11 : \
(x) <= (1ULL << 12ULL) ? 12 : \ #define LG_CEIL(x) (LG_FLOOR(x) + (((x) & ((x) - 1)) == 0 ? 0 : 1))
(x) <= (1ULL << 13ULL) ? 13 : \
(x) <= (1ULL << 14ULL) ? 14 : \
(x) <= (1ULL << 15ULL) ? 15 : \
(x) <= (1ULL << 16ULL) ? 16 : \
(x) <= (1ULL << 17ULL) ? 17 : \
(x) <= (1ULL << 18ULL) ? 18 : \
(x) <= (1ULL << 19ULL) ? 19 : \
(x) <= (1ULL << 20ULL) ? 20 : \
(x) <= (1ULL << 21ULL) ? 21 : \
(x) <= (1ULL << 22ULL) ? 22 : \
(x) <= (1ULL << 23ULL) ? 23 : \
(x) <= (1ULL << 24ULL) ? 24 : \
(x) <= (1ULL << 25ULL) ? 25 : \
(x) <= (1ULL << 26ULL) ? 26 : \
(x) <= (1ULL << 27ULL) ? 27 : \
(x) <= (1ULL << 28ULL) ? 28 : \
(x) <= (1ULL << 29ULL) ? 29 : \
(x) <= (1ULL << 30ULL) ? 30 : \
(x) <= (1ULL << 31ULL) ? 31 : \
(x) <= (1ULL << 32ULL) ? 32 : \
(x) <= (1ULL << 33ULL) ? 33 : \
(x) <= (1ULL << 34ULL) ? 34 : \
(x) <= (1ULL << 35ULL) ? 35 : \
(x) <= (1ULL << 36ULL) ? 36 : \
(x) <= (1ULL << 37ULL) ? 37 : \
(x) <= (1ULL << 38ULL) ? 38 : \
(x) <= (1ULL << 39ULL) ? 39 : \
(x) <= (1ULL << 40ULL) ? 40 : \
(x) <= (1ULL << 41ULL) ? 41 : \
(x) <= (1ULL << 42ULL) ? 42 : \
(x) <= (1ULL << 43ULL) ? 43 : \
(x) <= (1ULL << 44ULL) ? 44 : \
(x) <= (1ULL << 45ULL) ? 45 : \
(x) <= (1ULL << 46ULL) ? 46 : \
(x) <= (1ULL << 47ULL) ? 47 : \
(x) <= (1ULL << 48ULL) ? 48 : \
(x) <= (1ULL << 49ULL) ? 49 : \
(x) <= (1ULL << 50ULL) ? 50 : \
(x) <= (1ULL << 51ULL) ? 51 : \
(x) <= (1ULL << 52ULL) ? 52 : \
(x) <= (1ULL << 53ULL) ? 53 : \
(x) <= (1ULL << 54ULL) ? 54 : \
(x) <= (1ULL << 55ULL) ? 55 : \
(x) <= (1ULL << 56ULL) ? 56 : \
(x) <= (1ULL << 57ULL) ? 57 : \
(x) <= (1ULL << 58ULL) ? 58 : \
(x) <= (1ULL << 59ULL) ? 59 : \
(x) <= (1ULL << 60ULL) ? 60 : \
(x) <= (1ULL << 61ULL) ? 61 : \
(x) <= (1ULL << 62ULL) ? 62 : \
(x) <= (1ULL << 63ULL) ? 63 : \
64)
#endif /* JEMALLOC_INTERNAL_BIT_UTIL_H */ #endif /* JEMALLOC_INTERNAL_BIT_UTIL_H */

View File

@ -48,10 +48,64 @@ TEST_BEGIN(test_pow2_ceil_zu) {
} }
TEST_END TEST_END
void
assert_lg_ceil_range(size_t input, unsigned answer) {
if (input == 1) {
assert_u_eq(0, answer, "Got %u as lg_ceil of 1", answer);
return;
}
assert_zu_le(input, (ZU(1) << answer),
"Got %u as lg_ceil of %zu", answer, input);
assert_zu_gt(input, (ZU(1) << (answer - 1)),
"Got %u as lg_ceil of %zu", answer, input);
}
void
assert_lg_floor_range(size_t input, unsigned answer) {
if (input == 1) {
assert_u_eq(0, answer, "Got %u as lg_floor of 1", answer);
return;
}
assert_zu_ge(input, (ZU(1) << answer),
"Got %u as lg_floor of %zu", answer, input);
assert_zu_lt(input, (ZU(1) << (answer + 1)),
"Got %u as lg_floor of %zu", answer, input);
}
TEST_BEGIN(test_lg_ceil_floor) {
for (size_t i = 1; i < 10 * 1000 * 1000; i++) {
assert_lg_ceil_range(i, lg_ceil(i));
assert_lg_ceil_range(i, LG_CEIL(i));
assert_lg_floor_range(i, lg_floor(i));
assert_lg_floor_range(i, LG_FLOOR(i));
}
for (int i = 10; i < 8 * (1 << LG_SIZEOF_PTR) - 5; i++) {
for (size_t j = 0; j < (1 << 4); j++) {
size_t num1 = ((size_t)1 << i)
- j * ((size_t)1 << (i - 4));
size_t num2 = ((size_t)1 << i)
+ j * ((size_t)1 << (i - 4));
assert_zu_ne(num1, 0, "Invalid lg argument");
assert_zu_ne(num2, 0, "Invalid lg argument");
assert_lg_ceil_range(num1, lg_ceil(num1));
assert_lg_ceil_range(num1, LG_CEIL(num1));
assert_lg_ceil_range(num2, lg_ceil(num2));
assert_lg_ceil_range(num2, LG_CEIL(num2));
assert_lg_floor_range(num1, lg_floor(num1));
assert_lg_floor_range(num1, LG_FLOOR(num1));
assert_lg_floor_range(num2, lg_floor(num2));
assert_lg_floor_range(num2, LG_FLOOR(num2));
}
}
}
TEST_END
int int
main(void) { main(void) {
return test( return test(
test_pow2_ceil_u64, test_pow2_ceil_u64,
test_pow2_ceil_u32, test_pow2_ceil_u32,
test_pow2_ceil_zu); test_pow2_ceil_zu,
test_lg_ceil_floor);
} }