From bdbde497530e5c81ec710eef651ddc5f11f9f2de Mon Sep 17 00:00:00 2001 From: chrysn Date: Sun, 29 May 2022 13:39:03 +0200 Subject: [PATCH] bitarithm: Move `extern const` out of static inline functions Workaround-For: https://github.com/immunant/c2rust/issues/423 --- core/lib/bitarithm.c | 2 +- core/lib/include/bitarithm.h | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/core/lib/bitarithm.c b/core/lib/bitarithm.c index 93ffc763d7..26f234383b 100644 --- a/core/lib/bitarithm.c +++ b/core/lib/bitarithm.c @@ -62,7 +62,7 @@ uint8_t bitarithm_bits_set_u32(uint32_t v) } #endif -const uint8_t MultiplyDeBruijnBitPosition[32] = +const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 diff --git a/core/lib/include/bitarithm.h b/core/lib/include/bitarithm.h index 41e9460141..fece5ae8c2 100644 --- a/core/lib/include/bitarithm.h +++ b/core/lib/include/bitarithm.h @@ -163,6 +163,20 @@ static inline unsigned bitarithm_msb(unsigned v) #endif } +/** + * @private + * + * @brief Lookup table for a fast CLS / LSB implementations. + * + * This is not supposed to be public, and should be declared `extern const` in + * the two functions which use it -- but that causes [transpiler issues], so it + * is declared here as a workaround. (Once that issue is resolved and part of + * CI, this line will be removed again). + * + * [transpiler issues]: https://github.com/immunant/c2rust/issues/423 + */ +extern const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32]; + static inline unsigned bitarithm_lsb(unsigned v) #if defined(BITARITHM_LSB_BUILTIN) { @@ -171,12 +185,11 @@ static inline unsigned bitarithm_lsb(unsigned v) #elif defined(BITARITHM_LSB_LOOKUP) { /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */ - extern const uint8_t MultiplyDeBruijnBitPosition[32]; /* cppcheck-suppress oppositeExpression * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the * check treats opposite arguments as indicator for poor copy-pasting * as e.g. `x + -x` or `x & ~x` don't make sense. ) */ - return MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> + return bitarithm_MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27]; } #else @@ -219,13 +232,12 @@ static inline unsigned bitarithm_test_and_clear(unsigned state, uint8_t *index) return state & ~(1 << *index); #elif defined(BITARITHM_LSB_LOOKUP) /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */ - extern const uint8_t MultiplyDeBruijnBitPosition[32]; /* cppcheck-suppress oppositeExpression * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the * check treats opposite arguments as indicator for poor copy-pasting * as e.g. `x + -x` or `x & ~x` don't make sense. ) */ uint32_t least_bit = state & -state; - *index = MultiplyDeBruijnBitPosition[(least_bit * 0x077CB531U) >> 27]; + *index = bitarithm_MultiplyDeBruijnBitPosition[(least_bit * 0x077CB531U) >> 27]; return state & ~least_bit; #else while ((state & 1) == 0) {