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, 0, 1, 0, 2, 0, 1, 0,
50 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
51 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
52 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
53 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
54 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
55 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
56 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
57 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
58 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
59 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
60 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
61 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
62 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
65 static const uint64_t FCLZ_MASK_56 = 0x00ffffffffffffff;
66 static const uint64_t FCLZ_MASK_48 = 0x0000ffffffffffff;
67 static const uint64_t FCLZ_MASK_40 = 0x000000ffffffffff;
68 static const uint64_t FCLZ_MASK_32 = 0x00000000ffffffff;
69 static const uint64_t FCLZ_MASK_24 = 0x0000000000ffffff;
70 static const uint64_t FCLZ_MASK_16 = 0x000000000000ffff;
71 static const uint64_t FCLZ_MASK_08 = 0x00000000000000ff;
73 static inline uint8_t count_leading_zeros_in_u64(uint64_t input) {
74 if (input > FCLZ_MASK_56)
75 return byte_leading_zeros_table[(input >> 56) & FCLZ_MASK_08];
76 if (input > FCLZ_MASK_48)
77 return 8 + byte_leading_zeros_table[(input >> 48) & FCLZ_MASK_08];
78 if (input > FCLZ_MASK_40)
79 return 16 + byte_leading_zeros_table[(input >> 40) & FCLZ_MASK_08];
80 if (input > FCLZ_MASK_32)
81 return 24 + byte_leading_zeros_table[(input >> 32) & FCLZ_MASK_08];
82 if (input > FCLZ_MASK_24)
83 return 32 + byte_leading_zeros_table[(input >> 24) & FCLZ_MASK_08];
84 if (input > FCLZ_MASK_16)
85 return 40 + byte_leading_zeros_table[(input >> 16) & FCLZ_MASK_08];
86 if (input > FCLZ_MASK_08)
87 return 48 + byte_leading_zeros_table[(input >> 8) & FCLZ_MASK_08];
89 return 56 + byte_leading_zeros_table[(input ) & FCLZ_MASK_08];
92 static inline uint8_t count_leading_zeros_in_u32(uint32_t input) {
93 if (input > FCLZ_MASK_24)
94 return byte_leading_zeros_table[(input >> 24) & FCLZ_MASK_08];
95 if (input > FCLZ_MASK_16)
96 return 8 + byte_leading_zeros_table[(input >> 16) & FCLZ_MASK_08];
97 if (input > FCLZ_MASK_08)
98 return 16 + byte_leading_zeros_table[(input >> 8) & FCLZ_MASK_08];
100 return 24 + byte_leading_zeros_table[(input ) & FCLZ_MASK_08];
103 static inline uint8_t count_trailing_zeros_in_u32(uint32_t input) {
104 for (
int i = 0; i < 4; i++) {
105 const int byte = input & 0xff;
106 if (
byte != 0)
return static_cast<uint8_t
>((i << 3) + byte_trailing_zeros_table[
byte]);
112 static inline uint8_t count_trailing_zeros_in_u64(uint64_t input) {
113 for (
int i = 0; i < 8; i++) {
114 const int byte = input & 0xff;
115 if (
byte != 0)
return static_cast<uint8_t
>((i << 3) + byte_trailing_zeros_table[
byte]);
DataSketches namespace.
Definition: binomial_bounds.hpp:38