| 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 <system_error> |
| 9 |
|
| 10 |
template <typename T> char *to_string(T d, char *buffer) { |
| 11 |
auto written = std::snprintf(buffer, 128, "%.*e", 64, d); |
| 12 |
return buffer + written; |
| 13 |
} |
| 14 |
|
| 15 |
static fast_float::value128 g_lehmer64_state; |
| 16 |
|
| 17 |
/** |
| 18 |
* D. H. Lehmer, Mathematical methods in large-scale computing units. |
| 19 |
* Proceedings of a Second Symposium on Large Scale Digital Calculating |
| 20 |
* Machinery; |
| 21 |
* Annals of the Computation Laboratory, Harvard Univ. 26 (1951), pp. 141-146. |
| 22 |
* |
| 23 |
* P L'Ecuyer, Tables of linear congruential generators of different sizes and |
| 24 |
* good lattice structure. Mathematics of Computation of the American |
| 25 |
* Mathematical |
| 26 |
* Society 68.225 (1999): 249-260. |
| 27 |
*/ |
| 28 |
|
| 29 |
static inline void lehmer64_seed(uint64_t seed) { |
| 30 |
g_lehmer64_state.high = 0; |
| 31 |
g_lehmer64_state.low = seed; |
| 32 |
} |
| 33 |
|
| 34 |
static inline uint64_t lehmer64() { |
| 35 |
fast_float::value128 v = fast_float::full_multiplication( |
| 36 |
g_lehmer64_state.low, UINT64_C(0xda942042e4dd58b5)); |
| 37 |
v.high += g_lehmer64_state.high * UINT64_C(0xda942042e4dd58b5); |
| 38 |
g_lehmer64_state = v; |
| 39 |
return v.high; |
| 40 |
} |
| 41 |
|
| 42 |
size_t errors; |
| 43 |
|
| 44 |
void random_values(size_t N) { |
| 45 |
char buffer[128]; |
| 46 |
lehmer64_seed(N); |
| 47 |
for (size_t t = 0; t < N; t++) { |
| 48 |
if ((t % 1048576) == 0) { |
| 49 |
std::cout << "."; |
| 50 |
std::cout.flush(); |
| 51 |
} |
| 52 |
uint64_t word = lehmer64(); |
| 53 |
double v; |
| 54 |
memcpy(&v, &word, sizeof(v)); |
| 55 |
{ |
| 56 |
char const *string_end = to_string(v, buffer); |
| 57 |
double result_value; |
| 58 |
auto result = fast_float::from_chars(buffer, string_end, result_value); |
| 59 |
// Starting with version 4.0 for fast_float, we return result_out_of_range |
| 60 |
// if the value is either too small (too close to zero) or too large |
| 61 |
// (effectively infinity). So std::errc::result_out_of_range is normal for |
| 62 |
// well-formed input strings. |
| 63 |
if (result.ec != std::errc() && |
| 64 |
result.ec != std::errc::result_out_of_range) { |
| 65 |
std::cerr << "parsing error ? " << buffer << std::endl; |
| 66 |
errors++; |
| 67 |
if (errors > 10) { |
| 68 |
abort(); |
| 69 |
} |
| 70 |
continue; |
| 71 |
} |
| 72 |
if (std::isnan(v)) { |
| 73 |
if (!std::isnan(result_value)) { |
| 74 |
std::cerr << "not nan" << buffer << std::endl; |
| 75 |
errors++; |
| 76 |
if (errors > 10) { |
| 77 |
abort(); |
| 78 |
} |
| 79 |
} |
| 80 |
} else if (copysign(1, result_value) != copysign(1, v)) { |
| 81 |
std::cerr << buffer << std::endl; |
| 82 |
std::cerr << "I got " << std::hexfloat << result_value |
| 83 |
<< " but I was expecting " << v << std::endl; |
| 84 |
abort(); |
| 85 |
} else if (result_value != v) { |
| 86 |
std::cerr << "no match ? '" << buffer << "'" << std::endl; |
| 87 |
std::cout << "started with " << std::hexfloat << v << std::endl; |
| 88 |
std::cout << "got back " << std::hexfloat << result_value << std::endl; |
| 89 |
std::cout << std::dec; |
| 90 |
errors++; |
| 91 |
if (errors > 10) { |
| 92 |
abort(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
std::cout << std::endl; |
| 98 |
} |
| 99 |
|
| 100 |
int main() { |
| 101 |
errors = 0; |
| 102 |
size_t N = |
| 103 |
size_t(1) << (sizeof(size_t) * 4); // shift: 32 for 64bit, 16 for 32bit |
| 104 |
random_values(N); |
| 105 |
if (errors == 0) { |
| 106 |
std::cout << std::endl; |
| 107 |
std::cout << "all ok" << std::endl; |
| 108 |
return EXIT_SUCCESS; |
| 109 |
} |
| 110 |
std::cerr << std::endl; |
| 111 |
std::cerr << "errors were encountered" << std::endl; |
| 112 |
return EXIT_FAILURE; |
| 113 |
} |