From fddfb4261b41579f5d816374b3cdeddcbaa9cc46 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Sun, 31 Jul 2022 21:17:39 +0100 Subject: [PATCH] add fast floor_log2 --- src/floor_log2.h | 19 ++----------------- src/tans_symbol_tbl.c | 2 +- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/floor_log2.h b/src/floor_log2.h index 64e1e0b..a1311b2 100644 --- a/src/floor_log2.h +++ b/src/floor_log2.h @@ -2,22 +2,7 @@ #include -static uint16_t floor_log2(uint16_t x) +static inline uint32_t floor_log2(uint32_t x) { - if (x < 2) return 0; - if (x < 4) return 1; - if (x < 8) return 2; - if (x < 16) return 3; - if (x < 32) return 4; - if (x < 64) return 5; - if (x < 128) return 6; - if (x < 256) return 7; - if (x < 512) return 8; - if (x < 1024) return 9; - if (x < 2048) return 10; - if (x < 4096) return 11; - if (x < 8192) return 12; - if (x < 16384) return 13; - if (x < 32768) return 14; - /*if (x < 65536)*/ return 15; + return 31 - (uint32_t) __builtin_clz(x); } diff --git a/src/tans_symbol_tbl.c b/src/tans_symbol_tbl.c index fe105fd..46a9356 100644 --- a/src/tans_symbol_tbl.c +++ b/src/tans_symbol_tbl.c @@ -18,7 +18,7 @@ int tans_symbol_tbl_init(struct tans_symbol_tbl *self, const struct tans_freq_tb freq = freq_tbl->freq[s]; self->entries[s].start = start - freq; self->entries[s].next = freq; - k = self->log2_tblsz - floor_log2(freq); + k = (uint16_t) (self->log2_tblsz - floor_log2(freq)); self->entries[s].nb = (uint32_t) ((k << (self->log2_tblsz + 1)) - (freq << k)); start += freq; for (i = 0; i < freq; ++i) { -- 2.34.1