77#include < cstring>
88#include < iostream>
99#include < string_view>
10+ #include < charconv>
1011
1112#include " algorithms.h"
1213#include " cxxopts.hpp"
@@ -37,24 +38,25 @@ size_t count_significant_digits(std::string_view num_str) {
3738}
3839
3940std::string float_to_hex (float f) {
40- if (std::isnan (f) || std::isinf (f)) {
41- return fmt::format (" {}" , f); // Handle special cases
42- }
43-
44- uint32_t bits = std::bit_cast<uint32_t >(f);
45- int exponent;
46- float mantissa = std::frexp (f, &exponent); // Get mantissa and exponent
47- uint32_t mantissa_bits = bits & 0x7FFFFF ; // 23-bit mantissa
48- int exp_bits = (bits >> 23 ) & 0xFF ; // 8-bit exponent
49- bool sign = bits >> 31 ; // Sign bit
50-
51- // Adjust for IEEE 754 representation
52- if (exp_bits == 0 && mantissa_bits == 0 ) {
53- return " 0x0p+0" ; // Zero case
54- }
41+ std::ostringstream oss;
42+ oss << std::hexfloat << f;
43+ return oss.str ();
44+ }
5545
56- // Convert to hex format
57- return fmt::format (" 0x1.{:06x}p{:+d}" , mantissa_bits, exponent - 23 );
46+ std::optional<float > parse_float (std::string_view sv) {
47+ float result;
48+ const char * begin = sv.data ();
49+ const char * end = sv.data () + sv.size ();
50+
51+ auto [ptr, ec] = std::from_chars (begin, end, result);
52+
53+ // Check if parsing succeeded and consumed the entire string
54+ if (ec == std::errc{} && ptr == end) {
55+ return result;
56+ }
57+
58+ // Return nullopt if parsing failed or didn't consume all input
59+ return std::nullopt ;
5860}
5961
6062void run_exhaustive32 (bool errol) {
@@ -74,7 +76,8 @@ void run_exhaustive32(bool errol) {
7476 std::span<char > bufRef (buf1, sizeof (buf1)), bufAlgo (buf2, sizeof (buf2));
7577 fmt::print (" # processing {}" , algo.name );
7678 fflush (stdout);
77- for (uint64_t i = 0 ; i < (1ULL << 32 ); ++i) {
79+ for (size_t i = 0 ; i < 1 ; i++) {
80+ // for (uint64_t i = 0; i < (1ULL << 32); ++i) {
7881 if (i % 0x2000000 == 0 ) {
7982 printf (" ." );
8083 fflush (stdout);
@@ -83,17 +86,41 @@ void run_exhaustive32(bool errol) {
8386 uint32_t i32 (i);
8487 float d;
8588 std::memcpy (&d, &i32 , sizeof (float ));
89+ d = 33554448 ;
8690 if (std::isnan (d) || std::isinf (d))
8791 continue ;
8892 // Reference output
8993 const size_t vRef = Benchmarks::std_to_chars (d, bufRef);
94+ d = 33554448 ;
95+
9096 const size_t vAlgo = algo.func (d, bufAlgo);
9197
9298 std::string_view svRef{bufRef.data (), vRef};
9399 std::string_view svAlgo{bufAlgo.data (), vAlgo};
94100
95101 auto countRef = count_significant_digits (svRef);
96102 auto countAlgo = count_significant_digits (svAlgo);
103+ auto backRef = parse_float (svRef);
104+ auto backAlgo = parse_float (svAlgo);
105+ if (!backRef || !backAlgo) {
106+ incorrect = true ;
107+ fmt::print (" parse error: d = {}, bufRef = {}, bufAlgo = {}" , float_to_hex (d),
108+ svRef, svAlgo);
109+ fflush (stdout);
110+ break ;
111+ }
112+ if (*backRef != d) {
113+ incorrect = true ;
114+ fmt::print (" mismatch: d = {}, backRef = {}" , d, *backRef);
115+ fflush (stdout);
116+ break ;
117+ }
118+ if (*backAlgo != d) {
119+ incorrect = true ;
120+ fmt::print (" mismatch: d = {}, backAlgo = {}" , d, *backAlgo);
121+ fflush (stdout);
122+ break ;
123+ }
97124 if (countRef != countAlgo) {
98125 incorrect = true ;
99126 fmt::print (" mismatch: d = {}, bufRef = {}, bufAlgo = {}" , float_to_hex (d),
0 commit comments