Debian Codegraph @

tests/powersoffive_hardround.cpp

raw
1 #include "fast_float/fast_float.h"
2
3 #include <ios>
4 #include <iostream>
5 #include <random>
6 #include <sstream>
7 #include <string>
8 #include <system_error>
9 #include <utility>
10 #include <vector>
11
12 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
13 defined(sun) || defined(__sun)
14 // Anything at all that is related to cygwin, msys and so forth will
15 // always use this fallback because we cannot rely on it behaving as normal
16 // gcc.
17 #include <locale>
18
19 // workaround for CYGWIN
20 double cygwin_strtod_l(char const *start, char **end) {
21 double d;
22 std::stringstream ss;
23 ss.imbue(std::locale::classic());
24 ss << start;
25 ss >> d;
26 if (ss.fail()) {
27 *end = nullptr;
28 }
29 if (ss.eof()) {
30 ss.clear();
31 }
32 auto nread = ss.tellg();
33 *end = const_cast<char *>(start) + nread;
34 return d;
35 }
36
37 float cygwin_strtof_l(char const *start, char **end) {
38 float d;
39 std::stringstream ss;
40 ss.imbue(std::locale::classic());
41 ss << start;
42 ss >> d;
43 if (ss.fail()) {
44 *end = nullptr;
45 }
46 if (ss.eof()) {
47 ss.clear();
48 }
49 auto nread = ss.tellg();
50 *end = const_cast<char *>(start) + nread;
51 return d;
52 }
53 #endif
54
55 std::pair<double, bool> strtod_from_string(char const *st) {
56 double d;
57 char *pr;
58 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
59 defined(sun) || defined(__sun)
60 d = cygwin_strtod_l(st, &pr);
61 #elif defined(_WIN32)
62 static _locale_t c_locale = _create_locale(LC_ALL, "C");
63 d = _strtod_l(st, &pr, c_locale);
64 #else
65 static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
66 d = strtod_l(st, &pr, c_locale);
67 #endif
68 if (st == pr) {
69 std::cerr << "strtod_l could not parse '" << st << std::endl;
70 return std::make_pair(0, false);
71 }
72 return std::make_pair(d, true);
73 }
74
75 std::pair<float, bool> strtof_from_string(char *st) {
76 float d;
77 char *pr;
78 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
79 defined(sun) || defined(__sun)
80 d = cygwin_strtof_l(st, &pr);
81 #elif defined(_WIN32)
82 static _locale_t c_locale = _create_locale(LC_ALL, "C");
83 d = _strtof_l(st, &pr, c_locale);
84 #else
85 static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
86 d = strtof_l(st, &pr, c_locale);
87 #endif
88 if (st == pr) {
89 std::cerr << "strtof_l could not parse '" << st << std::endl;
90 return std::make_pair(0.0f, false);
91 }
92 return std::make_pair(d, true);
93 }
94
95 bool tester() {
96 std::random_device rd;
97 std::mt19937 gen(rd());
98 for (int q = 18; q <= 27; q++) {
99 std::cout << "q = " << -q << std::endl;
100 uint64_t power5 = 1;
101 for (int k = 0; k < q; k++) {
102 power5 *= 5;
103 }
104 uint64_t low_threshold = 0x20000000000000 / power5 + 1;
105 uint64_t threshold = 0xFFFFFFFFFFFFFFFF / power5;
106 std::uniform_int_distribution<uint64_t> dis(low_threshold, threshold);
107 for (size_t i = 0; i < 10000; i++) {
108 uint64_t mantissa = dis(gen) * power5;
109 std::stringstream ss;
110 ss << mantissa;
111 ss << "e";
112 ss << -q;
113 std::string to_be_parsed = ss.str();
114 std::pair<double, bool> expected_double =
115 strtod_from_string(to_be_parsed.c_str());
116 double result_value;
117 auto result = fast_float::from_chars(
118 to_be_parsed.data(), to_be_parsed.data() + to_be_parsed.size(),
119 result_value);
120 if (result.ec != std::errc() &&
121 result.ec != std::errc::result_out_of_range) {
122 std::cout << to_be_parsed << std::endl;
123 std::cerr << " I could not parse " << std::endl;
124 return false;
125 }
126 if (result_value != expected_double.first) {
127 std::cout << to_be_parsed << std::endl;
128 std::cerr << std::hexfloat << result_value << std::endl;
129 std::cerr << std::hexfloat << expected_double.first << std::endl;
130 std::cerr << " Mismatch " << std::endl;
131 return false;
132 }
133 }
134 }
135 return true;
136 }
137
138 int main() {
139 if (tester()) {
140 std::cout << std::endl;
141 std::cout << "all ok" << std::endl;
142 return EXIT_SUCCESS;
143 }
144 std::cerr << std::endl;
145 std::cerr << "errors were encountered" << std::endl;
146 return EXIT_FAILURE;
147 }