Got rid of floating-point division in table init.

Ideally we would get rid of all floating-point operations
in table.c, but that's a job for another day.
pull/13171/head
Joshua Haberman 4 years ago
parent 723cd8ffc1
commit f2d826b9f3
  1. 10
      tests/test_table.cc
  2. 8
      upb/table.c
  3. 11
      upb/upb.h

@ -618,6 +618,16 @@ void test_delete() {
upb_inttable_uninit(&t);
}
void test_init() {
for (int i = 0; i < 2048; i++) {
/* Tests that the size calculations in init() (lg2 size for target load)
* work for all expected sizes. */
upb_strtable t;
upb_strtable_init2(&t, UPB_CTYPE_BOOL, i, &upb_alloc_global);
upb_strtable_uninit(&t);
}
}
extern "C" {
int run_tests(int argc, char *argv[]) {

@ -294,11 +294,9 @@ static bool streql(upb_tabkey k1, lookupkey_t k2) {
bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype,
size_t expected_size, upb_alloc *a) {
UPB_UNUSED(ctype); /* TODO(haberman): rm */
size_t need_entries = (expected_size + 1) / MAX_LOAD;
int size_lg2 = 2;
while (1 << size_lg2 < need_entries) {
size_lg2++;
}
size_t need_entries = (expected_size + 1) * 1204 / 1024;
UPB_ASSERT(need_entries >= expected_size * 0.85);
int size_lg2 = _upb_lg2ceil(need_entries);
return init(&t->t, size_lg2, a);
}

@ -313,6 +313,17 @@ UPB_INLINE uint64_t _upb_be_swap64(uint64_t val) {
}
}
UPB_INLINE int _upb_lg2ceil(int x) {
if (x == 0) return 0;
#ifdef __GNUC__
return 32 - __builtin_clz(x - 1);
#else
int lg2 = 0;
while (1 << lg2 < x) lg2++;
return lg2;
#endif
}
#include "upb/port_undef.inc"
#ifdef __cplusplus

Loading…
Cancel
Save