Skip to content

Commit 1ad224e

Browse files
authored
Merge pull request #349 from shikharish/uint8
uint8_t parsing
2 parents 0920535 + 97cb3ec commit 1ad224e

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

include/fast_float/ascii_number.h

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

510510
UC const *const start_digits = p;
511511

512+
FASTFLOAT_IF_CONSTEXPR17((std::is_same<T, std::uint8_t>::value)) {
513+
const size_t len = (size_t)(pend - p);
514+
if (len == 0) {
515+
if (has_leading_zeros) {
516+
value = 0;
517+
answer.ec = std::errc();
518+
answer.ptr = p;
519+
} else {
520+
answer.ec = std::errc::invalid_argument;
521+
answer.ptr = first;
522+
}
523+
return answer;
524+
}
525+
526+
union {
527+
uint8_t as_str[4];
528+
uint32_t as_int;
529+
} digits;
530+
531+
if (cpp20_and_in_constexpr()) {
532+
digits.as_int = 0;
533+
for (size_t j = 0; j < 4 && j < len; ++j) {
534+
digits.as_str[j] = static_cast<uint8_t>(p[j]);
535+
}
536+
} else if (len >= 4) {
537+
memcpy(&digits.as_int, p, 4);
538+
} else {
539+
uint32_t b0 = static_cast<uint8_t>(p[0]);
540+
uint32_t b1 = (len > 1) ? static_cast<uint8_t>(p[1]) : 0xFFu;
541+
uint32_t b2 = (len > 2) ? static_cast<uint8_t>(p[2]) : 0xFFu;
542+
uint32_t b3 = 0xFFu;
543+
#if FASTFLOAT_IS_BIG_ENDIAN
544+
digits.as_int = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
545+
#else
546+
digits.as_int = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
547+
#endif
548+
}
549+
550+
uint32_t magic =
551+
((digits.as_int + 0x46464646u) | (digits.as_int - 0x30303030u)) &
552+
0x80808080u;
553+
uint32_t tz = (uint32_t)countr_zero_32(magic); // 7, 15, 23, 31, or 32
554+
uint32_t nd = (tz == 32) ? 4 : (tz >> 3);
555+
nd = (uint32_t)std::min((size_t)nd, len);
556+
if (nd == 0) {
557+
if (has_leading_zeros) {
558+
value = 0;
559+
answer.ec = std::errc();
560+
answer.ptr = p;
561+
return answer;
562+
}
563+
answer.ec = std::errc::invalid_argument;
564+
answer.ptr = first;
565+
return answer;
566+
}
567+
if (nd > 3) {
568+
const UC *q = p + nd;
569+
size_t rem = len - nd;
570+
while (rem) {
571+
if (*q < UC('0') || *q > UC('9'))
572+
break;
573+
++q;
574+
--rem;
575+
}
576+
answer.ec = std::errc::result_out_of_range;
577+
answer.ptr = q;
578+
return answer;
579+
}
580+
581+
digits.as_int ^= 0x30303030u;
582+
digits.as_int <<= ((4 - nd) * 8);
583+
584+
uint32_t check = ((digits.as_int >> 24) & 0xff) |
585+
((digits.as_int >> 8) & 0xff00) |
586+
((digits.as_int << 8) & 0xff0000);
587+
if (check > 0x00020505) {
588+
answer.ec = std::errc::result_out_of_range;
589+
answer.ptr = p + nd;
590+
return answer;
591+
}
592+
value = (uint8_t)((0x640a01 * digits.as_int) >> 24);
593+
answer.ec = std::errc();
594+
answer.ptr = p + nd;
595+
return answer;
596+
}
597+
512598
uint64_t i = 0;
513599
if (base == 10) {
514600
loop_parse_if_eight_digits(p, pend, i); // use SIMD if possible

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;

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ endif()
9494
option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)
9595

9696
if (FASTFLOAT_EXHAUSTIVE)
97+
fast_float_add_cpp_test(ipv4_test)
9798
fast_float_add_cpp_test(short_random_string)
9899
fast_float_add_cpp_test(exhaustive32_midpoint)
99100
fast_float_add_cpp_test(random_string)

tests/ipv4_test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
#include <charconv>
3+
#include <cstdint>
4+
#include <iostream>
5+
#include <algorithm>
6+
#include "fast_float/fast_float.h"
7+
8+
char *uint8_to_chars_manual(char *ptr, uint8_t value) {
9+
if (value == 0) {
10+
*ptr++ = '0';
11+
return ptr;
12+
}
13+
char *start = ptr;
14+
while (value > 0) {
15+
*ptr++ = '0' + (value % 10);
16+
value /= 10;
17+
}
18+
// Reverse the digits written so far
19+
std::reverse(start, ptr);
20+
return ptr;
21+
}
22+
23+
void uint32_to_ipv4_string(uint32_t ip, char *buffer) {
24+
uint8_t octets[4] = {static_cast<uint8_t>(ip >> 24),
25+
static_cast<uint8_t>(ip >> 16),
26+
static_cast<uint8_t>(ip >> 8), static_cast<uint8_t>(ip)};
27+
28+
char *ptr = buffer;
29+
30+
for (int i = 0; i < 4; ++i) {
31+
ptr = uint8_to_chars_manual(ptr, octets[i]);
32+
33+
if (i < 3) {
34+
*ptr++ = '.';
35+
}
36+
}
37+
*ptr = '\0';
38+
}
39+
40+
fastfloat_really_inline uint32_t ipv4_string_to_uint32(const char *str,
41+
const char *end) {
42+
uint32_t ip = 0;
43+
const char *current = str;
44+
45+
for (int i = 0; i < 4; ++i) {
46+
uint8_t value;
47+
auto r = fast_float::from_chars(current, end, value);
48+
if (r.ec != std::errc()) {
49+
throw std::invalid_argument("Invalid IP address format");
50+
}
51+
current = r.ptr;
52+
ip = (ip << 8) | value;
53+
54+
if (i < 3) {
55+
if (current == end || *current++ != '.') {
56+
throw std::invalid_argument("Invalid IP address format");
57+
}
58+
}
59+
}
60+
return ip;
61+
}
62+
63+
bool test_all_ipv4_conversions() {
64+
std::cout << "Testing all IPv4 conversions... 0, 1000, 2000, 3000, 4000, "
65+
"5000, 6000, 7000, 8000, 9000, ..."
66+
<< std::endl;
67+
char buffer[16];
68+
for (uint64_t ip = 0; ip <= 0xFFFFFFFF; ip += 1000) {
69+
if (ip % 10000000 == 0) {
70+
std::cout << "." << std::flush;
71+
}
72+
uint32_to_ipv4_string(static_cast<uint32_t>(ip), buffer);
73+
const char *end = buffer + strlen(buffer);
74+
uint32_t parsed_ip = ipv4_string_to_uint32(buffer, end);
75+
if (parsed_ip != ip) {
76+
std::cerr << "Mismatch: original " << ip << ", parsed " << parsed_ip
77+
<< std::endl;
78+
return false;
79+
}
80+
}
81+
std::cout << std::endl;
82+
return true;
83+
}
84+
85+
int main() {
86+
if (test_all_ipv4_conversions()) {
87+
std::cout << "All IPv4 conversions passed!" << std::endl;
88+
return EXIT_SUCCESS;
89+
} else {
90+
std::cerr << "IPv4 conversion test failed!" << std::endl;
91+
return EXIT_FAILURE;
92+
}
93+
}

0 commit comments

Comments
 (0)