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