util/BitReverse: generate table with constexpr function

Get rid of the macro hell.
This commit is contained in:
Max Kellermann 2020-02-05 19:51:46 +01:00
parent 7b03f55cb4
commit 2d3b51665e
2 changed files with 28 additions and 11 deletions

View File

@ -19,13 +19,13 @@
#include "BitReverse.hxx" #include "BitReverse.hxx"
/** static constexpr BitReverseTable
* @see http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable GenerateBitReverseTable() noexcept
*/
const uint8_t bit_reverse_table[256] =
{ {
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64 BitReverseTable table{};
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16) for (unsigned i = 0; i < 256; ++i)
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 ) table.data[i] = BitReverseMultiplyModulus(i);
R6(0), R6(2), R6(1), R6(3) return table;
}; }
const BitReverseTable bit_reverse_table = GenerateBitReverseTable();

View File

@ -24,13 +24,30 @@
#include <stdint.h> #include <stdint.h>
extern const uint8_t bit_reverse_table[256]; /**
* @see http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv
*/
constexpr uint8_t
BitReverseMultiplyModulus(uint8_t _in) noexcept
{
uint64_t in = _in;
return uint8_t((in * 0x0202020202ULL & 0x010884422010ULL) % 1023);
}
/* in order to avoid including <array> in this header, this `struct`
is a workaround for GenerateBitReverseTable() being able to return
the plain array */
struct BitReverseTable {
uint8_t data[256];
};
extern const BitReverseTable bit_reverse_table;
gcc_const gcc_const
static inline uint8_t static inline uint8_t
bit_reverse(uint8_t x) noexcept bit_reverse(uint8_t x) noexcept
{ {
return bit_reverse_table[x]; return bit_reverse_table.data[x];
} }
#endif #endif