Skip to content

Commit

Permalink
Fixed wildly incorrect result from log2 function in 32-bit builds. (&…
Browse files Browse the repository at this point in the history
… added test to prove it)
  • Loading branch information
pmj committed Feb 21, 2013
1 parent 0e8f139 commit d821185
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/chaining_hash_table.c
Expand Up @@ -187,7 +187,7 @@ void genc_cht_destroy(struct genc_chaining_hash_table* table)

static GENC_INLINE int genc_log2_size(size_t val)
{
return val == 0 ? -1 : (int)(sizeof(size_t) * 8 - 1 - __builtin_clzll(val));
return val == 0 ? -1 : (int)(sizeof(size_t) * 8 - 1 - __builtin_clzl(val));
}

static GENC_INLINE int genc_log2_size_roundup(size_t val)
Expand Down
13 changes: 13 additions & 0 deletions test/chaining_hash_table/cht_inline_test.c
@@ -0,0 +1,13 @@
#include "../../src/chaining_hash_table.c"
#include <assert.h>

int main()
{
assert(-1 == genc_log2_size(0));
assert(sizeof(size_t) * 8 - 1 == genc_log2_size(SIZE_MAX));
assert(0 == genc_log2_size(1));
assert(1 == genc_log2_size(2));
assert(1 == genc_log2_size(3));
assert(2 == genc_log2_size(4));
return 0;
}

0 comments on commit d821185

Please sign in to comment.