Debian Codegraph @

tests/exhaustive32_midpoint.cpp

raw
1 #include "fast_float/fast_float.h"
2
3 #include <cassert>
4 #include <cmath>
5 #include <cstdio>
6 #include <ios>
7 #include <iostream>
8 #include <limits>
9 #include <stdexcept>
10
11 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
12 // Anything at all that is related to cygwin, msys and so forth will
13 // always use this fallback because we cannot rely on it behaving as normal
14 // gcc.
15 #include <locale>
16 #include <sstream>
17
18 // workaround for CYGWIN
19 double cygwin_strtod_l(char const *start, char **end) {
20 double d;
21 std::stringstream ss;
22 ss.imbue(std::locale::classic());
23 ss << start;
24 ss >> d;
25 if (ss.fail()) {
26 *end = nullptr;
27 }
28 if (ss.eof()) {
29 ss.clear();
30 }
31 auto nread = ss.tellg();
32 *end = const_cast<char *>(start) + nread;
33 return d;
34 }
35
36 float cygwin_strtof_l(char const *start, char **end) {
37 float d;
38 std::stringstream ss;
39 ss.imbue(std::locale::classic());
40 ss << start;
41 ss >> d;
42 if (ss.fail()) {
43 *end = nullptr;
44 }
45 if (ss.eof()) {
46 ss.clear();
47 }
48 auto nread = ss.tellg();
49 *end = const_cast<char *>(start) + nread;
50 return d;
51 }
52 #endif
53
54 template <typename T> char *to_string(T d, char *buffer) {
55 auto written = std::snprintf(buffer, 64, "%.*e",
56 std::numeric_limits<T>::max_digits10 - 1, d);
57 return buffer + written;
58 }
59
60 void strtof_from_string(char const *st, float &d) {
61 char *pr = (char *)st;
62 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
63 defined(sun) || defined(__sun)
64 d = cygwin_strtof_l(st, &pr);
65 #elif defined(_WIN32)
66 static _locale_t c_locale = _create_locale(LC_ALL, "C");
67 d = _strtof_l(st, &pr, c_locale);
68 #else
69 static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
70 d = strtof_l(st, &pr, c_locale);
71 #endif
72 if (pr == st) {
73 throw std::runtime_error("bug in strtod_from_string");
74 }
75 }
76
77 bool allvalues() {
78 char buffer[64];
79 for (uint64_t w = 0; w <= 0xFFFFFFFF; w++) {
80 float v;
81 if ((w % 1048576) == 0) {
82 std::cout << ".";
83 std::cout.flush();
84 }
85 uint32_t word = uint32_t(w);
86 memcpy(&v, &word, sizeof(v));
87 if (std::isfinite(v)) {
88 float nextf = std::nextafterf(v, INFINITY);
89 if (copysign(1, v) != copysign(1, nextf)) {
90 continue;
91 }
92 if (!std::isfinite(nextf)) {
93 continue;
94 }
95 double v1{v};
96 assert(float(v1) == v);
97 double v2{nextf};
98 assert(float(v2) == nextf);
99 double midv{v1 + (v2 - v1) / 2};
100 float expected_midv = float(midv);
101
102 char const *string_end = to_string(midv, buffer);
103 float str_answer;
104 strtof_from_string(buffer, str_answer);
105
106 float result_value;
107 auto result = fast_float::from_chars(buffer, string_end, result_value);
108 // Starting with version 4.0 for fast_float, we return result_out_of_range
109 // if the value is either too small (too close to zero) or too large
110 // (effectively infinity). So std::errc::result_out_of_range is normal for
111 // well-formed input strings.
112 if (result.ec != std::errc() &&
113 result.ec != std::errc::result_out_of_range) {
114 std::cerr << "parsing error ? " << buffer << std::endl;
115 return false;
116 }
117 if (std::isnan(v)) {
118 if (!std::isnan(result_value)) {
119 std::cerr << "not nan" << buffer << std::endl;
120 std::cerr << "v " << std::hexfloat << v << std::endl;
121 std::cerr << "v2 " << std::hexfloat << v2 << std::endl;
122 std::cerr << "midv " << std::hexfloat << midv << std::endl;
123 std::cerr << "expected_midv " << std::hexfloat << expected_midv
124 << std::endl;
125 return false;
126 }
127 } else if (copysign(1, result_value) != copysign(1, v)) {
128 std::cerr << buffer << std::endl;
129 std::cerr << "v " << std::hexfloat << v << std::endl;
130 std::cerr << "v2 " << std::hexfloat << v2 << std::endl;
131 std::cerr << "midv " << std::hexfloat << midv << std::endl;
132 std::cerr << "expected_midv " << std::hexfloat << expected_midv
133 << std::endl;
134 std::cerr << "I got " << std::hexfloat << result_value
135 << " but I was expecting " << v << std::endl;
136 return false;
137 } else if (result_value != str_answer) {
138 std::cerr << "no match ? " << buffer << std::endl;
139 std::cerr << "v " << std::hexfloat << v << std::endl;
140 std::cerr << "v2 " << std::hexfloat << v2 << std::endl;
141 std::cerr << "midv " << std::hexfloat << midv << std::endl;
142 std::cerr << "expected_midv " << std::hexfloat << expected_midv
143 << std::endl;
144 std::cout << "started with " << std::hexfloat << midv << std::endl;
145 std::cout << "round down to " << std::hexfloat << str_answer
146 << std::endl;
147 std::cout << "got back " << std::hexfloat << result_value << std::endl;
148 std::cout << std::dec;
149 return false;
150 }
151 }
152 }
153 std::cout << std::endl;
154 return true;
155 }
156
157 inline void Assert(bool Assertion) {
158 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
159 defined(sun) || defined(__sun)
160 if (!Assertion) {
161 std::cerr << "Omitting hard failure on msys/cygwin/sun systems.";
162 }
163 #else
164 if (!Assertion) {
165 throw std::runtime_error("bug");
166 }
167 #endif
168 }
169
170 int main() {
171 #if defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__) || \
172 defined(sun) || defined(__sun)
173 std::cout << "Warning: msys/cygwin or solaris detected. This particular test "
174 "is likely to generate false failures due to our reliance on "
175 "the underlying runtime library as a gold standard."
176 << std::endl;
177 #endif
178 Assert(allvalues());
179 std::cout << std::endl;
180 std::cout << "all ok" << std::endl;
181 return EXIT_SUCCESS;
182 }