9#ifndef FORTRAN_COMMON_LEADING_ZERO_BIT_COUNT_H_
10#define FORTRAN_COMMON_LEADING_ZERO_BIT_COUNT_H_
38static constexpr std::uint64_t deBruijn{0x07edd5e59a4e28c2};
39static constexpr std::uint8_t mapping[64]{63, 0, 58, 1, 59, 47, 53, 2, 60, 39,
40 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43,
41 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
42 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
45inline constexpr int LeadingZeroBitCount(std::uint64_t x) {
61 int base2Log{mapping[(x * deBruijn) >> 58]};
66inline constexpr int LeadingZeroBitCount(std::uint32_t x) {
67 return LeadingZeroBitCount(
static_cast<std::uint64_t
>(x)) - 32;
70inline constexpr int LeadingZeroBitCount(std::uint16_t x) {
71 return LeadingZeroBitCount(
static_cast<std::uint64_t
>(x)) - 48;
75static constexpr std::uint8_t eightBitLeadingZeroBitCount[256]{8, 7, 6, 6, 5, 5,
76 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
77 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
78 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
88inline constexpr int LeadingZeroBitCount(std::uint8_t x) {
89 return eightBitLeadingZeroBitCount[x];
92template <
typename A>
inline constexpr int BitsNeededFor(A x) {
93 return 8 *
sizeof x - LeadingZeroBitCount(x);
Definition: bit-population-count.h:20