c++14 constexpr

Signed-off-by: Shikhar <shikharish05@gmail.com>
This commit is contained in:
Shikhar 2025-12-23 06:13:17 +05:30
parent fce0ab61df
commit fdb0eddf99
2 changed files with 48 additions and 2 deletions

View File

@ -509,7 +509,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
UC const *const start_digits = p;
if constexpr (std::is_same_v<T, std::uint8_t>) {
FASTFLOAT_IF_CONSTEXPR17(std::is_same<T, std::uint8_t>::value) {
const size_t len = (size_t)(pend - p);
if (len == 0) {
if (has_leading_zeros) {
@ -550,7 +550,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
uint32_t magic =
((digits.as_int + 0x46464646u) | (digits.as_int - 0x30303030u)) &
0x80808080u;
uint32_t tz = (uint32_t)std::__countr_zero(magic); // 7, 15, 23, 31, or 32
uint32_t tz = (uint32_t)countr_zero_32(magic); // 7, 15, 23, 31, or 32
uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
nd = (uint32_t)std::min((size_t)nd, len);
if (nd == 0) {

View File

@ -362,6 +362,52 @@ leading_zeroes(uint64_t input_num) {
#endif
}
/* Helper C++14 constexpr generic implementation of countr_zero for 32-bit */
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int
countr_zero_generic_32(uint32_t input_num) {
if (input_num == 0) {
return 32;
}
int last_bit = 0;
if (!(input_num & 0x0000FFFF)) {
input_num >>= 16;
last_bit |= 16;
}
if (!(input_num & 0x00FF)) {
input_num >>= 8;
last_bit |= 8;
}
if (!(input_num & 0x0F)) {
input_num >>= 4;
last_bit |= 4;
}
if (!(input_num & 0x3)) {
input_num >>= 2;
last_bit |= 2;
}
if (!(input_num & 0x1)) {
last_bit |= 1;
}
return last_bit;
}
/* count trailing zeroes for 32-bit integers */
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int
countr_zero_32(uint32_t input_num) {
if (cpp20_and_in_constexpr()) {
return countr_zero_generic_32(input_num);
}
#ifdef FASTFLOAT_VISUAL_STUDIO
unsigned long trailing_zero = 0;
if (_BitScanForward(&trailing_zero, input_num)) {
return (int)trailing_zero;
}
return 32;
#else
return input_num == 0 ? 32 : __builtin_ctz(input_num);
#endif
}
// slow emulation routine for 32-bit
fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) {
return x * (uint64_t)y;