Skip to content

Commit f93ac56

Browse files
committed
c++14 constexpr
Signed-off-by: Shikhar <[email protected]>
1 parent 2b208cd commit f93ac56

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

include/fast_float/ascii_number.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
509509

510510
UC const *const start_digits = p;
511511

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

include/fast_float/float_common.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,52 @@ leading_zeroes(uint64_t input_num) {
362362
#endif
363363
}
364364

365+
/* Helper C++14 constexpr generic implementation of countr_zero for 32-bit */
366+
fastfloat_really_inline FASTFLOAT_CONSTEXPR14 int
367+
countr_zero_generic_32(uint32_t input_num) {
368+
if (input_num == 0) {
369+
return 32;
370+
}
371+
int last_bit = 0;
372+
if (!(input_num & 0x0000FFFF)) {
373+
input_num >>= 16;
374+
last_bit |= 16;
375+
}
376+
if (!(input_num & 0x00FF)) {
377+
input_num >>= 8;
378+
last_bit |= 8;
379+
}
380+
if (!(input_num & 0x0F)) {
381+
input_num >>= 4;
382+
last_bit |= 4;
383+
}
384+
if (!(input_num & 0x3)) {
385+
input_num >>= 2;
386+
last_bit |= 2;
387+
}
388+
if (!(input_num & 0x1)) {
389+
last_bit |= 1;
390+
}
391+
return last_bit;
392+
}
393+
394+
/* count trailing zeroes for 32-bit integers */
395+
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 int
396+
countr_zero_32(uint32_t input_num) {
397+
if (cpp20_and_in_constexpr()) {
398+
return countr_zero_generic_32(input_num);
399+
}
400+
#ifdef FASTFLOAT_VISUAL_STUDIO
401+
unsigned long trailing_zero = 0;
402+
if (_BitScanForward(&trailing_zero, input_num)) {
403+
return (int)trailing_zero;
404+
}
405+
return 32;
406+
#else
407+
return input_num == 0 ? 32 : __builtin_ctz(input_num);
408+
#endif
409+
}
410+
365411
// slow emulation routine for 32-bit
366412
fastfloat_really_inline constexpr uint64_t emulu(uint32_t x, uint32_t y) {
367413
return x * (uint64_t)y;

0 commit comments

Comments
 (0)