2121#include < iostream>
2222#include < string>
2323#include < variant>
24+ #include < fast_float/fast_float.h>
25+ #if FROM_CHARS_SUPPORTED || TO_CHARS_SUPPORTED
26+ #include < charconv> // technically included by "algorithms.h"
27+ #endif
2428
2529using Benchmarks::arithmetic_float;
2630using Benchmarks::BenchArgs;
2731
28- #if FROM_CHARS_SUPPORTED
2932template <arithmetic_float T>
3033void evaluateProperties (const std::vector<T> &lines,
3134 const std::array<BenchArgs<T>, Benchmarks::COUNT> &args) {
@@ -46,15 +49,17 @@ void evaluateProperties(const std::vector<T> &lines,
4649 const int vRef = Benchmarks::std_to_chars (d, bufRef);
4750 bufRef[vRef] = ' \0 ' ;
4851 T dRef;
49- auto [ptr, ec] = std::from_chars (bufRef.data (), bufRef.data () + vRef, dRef);
52+ // We prefer fast_float::from_chars over std::from_chars because it is more
53+ // likely to be available.
54+ auto [ptr, ec] = fast_float::from_chars (bufRef.data (), bufRef.data () + vRef, dRef);
5055 assert (ptr == bufRef.data () + vRef);
5156 assert (ec == std::errc ());
5257 assert (d == dRef);
5358 // Tested algorithm output
5459 const int vAlgo = algo.func (d, bufAlgo);
5560 bufAlgo[vAlgo] = ' \0 ' ;
5661 T dAlgo;
57- auto [ptrAlgo, ecAlgo] = std ::from_chars (bufAlgo.data (), bufAlgo.data () + vAlgo, dAlgo);
62+ auto [ptrAlgo, ecAlgo] = fast_float ::from_chars (bufAlgo.data (), bufAlgo.data () + vAlgo, dAlgo);
5863 assert (ptrAlgo == bufAlgo.data () + vAlgo);
5964 assert (ecAlgo == std::errc ());
6065 if ((incorrect += (d != dAlgo)) == 1 )
@@ -64,7 +69,6 @@ void evaluateProperties(const std::vector<T> &lines,
6469 fmt::println (" {:20} {:20}" , algo.name , incorrect == 0 ? " yes" : " no" );
6570 }
6671}
67- #endif // FROM_CHARS_SUPPORTED
6872
6973template <arithmetic_float T>
7074void process (const std::vector<T> &lines,
@@ -133,23 +137,22 @@ cxxopts::Options
133137
134138int main (int argc, char **argv) {
135139 try {
136- options.add_options ()(" f,file" , " File name." ,
137- cxxopts::value<std::string>()->default_value (" " ))(
138- " v,volume" , " Volume (number of floats generated)." ,
139- cxxopts::value<size_t >()->default_value (" 100000" ))(
140- " m,model" , " Random Model." ,
141- cxxopts::value<std::string>()->default_value (" uniform" ))(
142- " s,single" , " Use single precision instead of double." ,
140+ options.add_options ()
141+ (" f,file" , " File name." ,
142+ cxxopts::value<std::string>()->default_value (" " ))
143+ (" v,volume" , " Volume (number of floats generated)." ,
144+ cxxopts::value<size_t >()->default_value (" 100000" ))
145+ (" m,model" , " Random Model." ,
146+ cxxopts::value<std::string>()->default_value (" uniform" ))
147+ (" s,single" , " Use single precision instead of double." ,
143148 cxxopts::value<bool >()->default_value (" false" ))
144- #if FROM_CHARS_SUPPORTED
145149 (" t,test" , " Test the algorithms and find their properties." ,
146150 cxxopts::value<bool >()->default_value (" false" ))
147- #endif // FROM_CHARS_SUPPORTED
148151 (" d,dragon" , " Enable dragon4 (current impl. triggers some asserts)." ,
149- cxxopts::value<bool >()->default_value (" false" ))(
150- " e,errol" , " Enable errol3 (current impl. returns invalid values, e.g., for 0)." ,
151- cxxopts::value<bool >()->default_value (" false" ))(
152- " h,help" , " Print usage." );
152+ cxxopts::value<bool >()->default_value (" false" ))
153+ ( " e,errol" , " Enable errol3 (current impl. returns invalid values, e.g., for 0)." ,
154+ cxxopts::value<bool >()->default_value (" false" ))
155+ ( " h,help" , " Print usage." );
153156 const auto result = options.parse (argc, argv);
154157
155158 if (result[" help" ].as <bool >()) {
@@ -198,7 +201,7 @@ int main(int argc, char **argv) {
198201 args[Benchmarks::TEJU_JAGUA] = { " teju_jagua" , Benchmarks::teju_jagua<T> , true };
199202 args[Benchmarks::DOUBLE_CONVERSION] = { " double_conversion" , Benchmarks::double_conversion<T> , true };
200203 args[Benchmarks::ABSEIL] = { " abseil" , Benchmarks::abseil<T> , ABSEIL_SUPPORTED };
201- args[Benchmarks::STD_TO_CHARS] = { " std::to_chars" , Benchmarks::std_to_chars<T> , FROM_CHARS_SUPPORTED };
204+ args[Benchmarks::STD_TO_CHARS] = { " std::to_chars" , Benchmarks::std_to_chars<T> , TO_CHARS_SUPPORTED };
202205 return args;
203206 };
204207
@@ -214,11 +217,9 @@ int main(int argc, char **argv) {
214217 using T1 = typename std::decay_t <decltype (lines)>::value_type;
215218 using T2 = typename std::decay_t <decltype (args)>::value_type::Type;
216219 if constexpr (std::is_same_v<T1, T2>) {
217- #if FROM_CHARS_SUPPORTED
218220 if (test)
219221 evaluateProperties (lines, args);
220222 else
221- #endif // FROM_CHARS_SUPPORTED
222223 process (lines, args);
223224 }
224225 }, numbers, algorithms);
0 commit comments