From 622cda3802be7b54075bae25839d55936a1c370a Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 6 Dec 2020 14:37:18 +0100 Subject: hash/log2: Add generic implementations of fls() and fls64() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When importing the hashtable code in I8ef73a62fe9846ce45058eb21cf999dd3eed5741 I didn't import actual implementations of the fls() and fls64() implementations, as at least gcc-10 was smart enough to detect we only use it on constant types and hence the computation can happen at build time via const_ilog2() However, in our jenkins build verification' this doesn't appear to happen, as we get below errors: /build/deps/install/stow/libosmocore/include/osmocom/core/log2.h: In function ‘__ilog2_u32’: /build/deps/install/stow/libosmocore/include/osmocom/core/log2.h:20:9: error: implicit declaration of function ‘fls’ [-Werror=implicit-function-declaration] return fls(n) - 1; ^~~ /build/deps/install/stow/libosmocore/include/osmocom/core/log2.h: In function ‘__ilog2_u64’: /build/deps/install/stow/libosmocore/include/osmocom/core/log2.h:28:9: error: implicit declaration of function ‘fls64’ [-Werror=implicit-function-declaration] return fls64(n) - 1; ^~~~~ Let's provide some generic implementations for this case. If needed one could also introduce architecture-specific assembly implementations like in the Linux kernel, but so far we managed to keep libosmocore free of any assembly tweaks. Change-Id: Ifa4898eb66c8d949618edd47961b7a0330ed35b5 --- include/osmocom/core/log2.h | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/osmocom/core/log2.h b/include/osmocom/core/log2.h index 06b20f8c..dfe9d37a 100644 --- a/include/osmocom/core/log2.h +++ b/include/osmocom/core/log2.h @@ -6,6 +6,65 @@ */ #pragma once +#define __always_inline inline __attribute__((always_inline)) + +/* from linux/asm-generic/bitops/{fls,fls64}.h - could later be enhanced + * with architecture specific optimized versions */ + +/** + * fls - find last (most-significant) bit set + * @x: the word to search + * + * This is defined the same way as ffs. + * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. + */ +static __always_inline int fls(unsigned int x) +{ + int r = 32; + + if (!x) + return 0; + if (!(x & 0xffff0000u)) { + x <<= 16; + r -= 16; + } + if (!(x & 0xff000000u)) { + x <<= 8; + r -= 8; + } + if (!(x & 0xf0000000u)) { + x <<= 4; + r -= 4; + } + if (!(x & 0xc0000000u)) { + x <<= 2; + r -= 2; + } + if (!(x & 0x80000000u)) { + x <<= 1; + r -= 1; + } + return r; +} + +/** + * fls64 - find last set bit in a 64-bit word + * @x: the word to search + * + * This is defined in a similar way as the libc and compiler builtin + * ffsll, but returns the position of the most significant set bit. + * + * fls64(value) returns 0 if value is 0 or the position of the last + * set bit if value is nonzero. The last (most significant) bit is + * at position 64. + */ +static __always_inline int fls64(__u64 x) +{ + __u32 h = x >> 32; + if (h) + return fls(h) + 32; + return fls(x); +} /* * non-constant log of base 2 calculators -- cgit v1.2.3