Skip to content

Commit b987176

Browse files
authored
Merge pull request #15 from fastfloat/benchmark_fixes
Benchmark fixes
2 parents f09853a + 53a4f0d commit b987176

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

benchmarks/CMakeLists.txt

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,35 @@ int main(void) {
1414
std::from_chars_result result2 = std::from_chars(ptr, ptr, valued, std::chars_format::general);
1515
return 0;
1616
}
17-
" from_chars_ok)
18-
target_compile_definitions(benchmark PUBLIC FROM_CHARS_SUPPORTED=$<IF:${from_chars_ok},1,0>)
19-
17+
" FROM_CHARS_OK)
18+
target_compile_definitions(benchmark PUBLIC FROM_CHARS_SUPPORTED=$<IF:$<BOOL:${FROM_CHARS_OK}>,1,0>)
19+
check_source_compiles(CXX "
20+
#include <charconv>
21+
int main(void) {
22+
float valuef = 0;
23+
double valued = 0;
24+
const char* ptr;
25+
std::to_chars_result result1 = std::to_chars(ptr, ptr, valuef);
26+
std::to_chars_result result2 = std::to_chars(ptr, ptr, valued);
27+
return 0;
28+
}
29+
" TO_CHARS_OK)
30+
target_compile_definitions(benchmark PUBLIC TO_CHARS_SUPPORTED=$<IF:$<BOOL:${TO_CHARS_OK}>,1,0>)
31+
if(TO_CHARS_OK)
32+
message(STATUS "std::to_chars with floats is supported")
33+
else(TO_CHARS_OK)
34+
message(STATUS "std::to_chars with floats is NOT supported")
35+
endif(TO_CHARS_OK)
36+
if(FROM_CHARS_OK)
37+
message(STATUS "std::from_chars with floats is supported")
38+
else(FROM_CHARS_OK)
39+
message(STATUS "std::from_chars with floats is NOT supported")
40+
endif(FROM_CHARS_OK)
41+
CPMAddPackage(
42+
NAME fast_float
43+
GITHUB_REPOSITORY "fastfloat/fast_float"
44+
GIT_TAG v8.0.2)
45+
target_link_libraries(benchmark PUBLIC fast_float)
2046
if (NOT WIN32)
2147
target_link_libraries(benchmark PUBLIC netlib)
2248
target_compile_definitions(benchmark PUBLIC NETLIB_SUPPORTED=1)
@@ -25,7 +51,7 @@ else()
2551
endif()
2652

2753
if (NOT CYGWIN)
28-
target_link_libraries(benchmark PUBLIC absl::strings absl::str_format)
54+
target_link_libraries(benchmark PUBLIC absl::str_format)
2955
target_compile_definitions(benchmark PUBLIC ABSEIL_SUPPORTED=1)
3056
else()
3157
target_compile_definitions(benchmark PUBLIC ABSEIL_SUPPORTED=0)

benchmarks/algorithms.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "errol.h"
1010
#endif
1111

12-
#if FROM_CHARS_SUPPORTED
12+
#if TO_CHARS_SUPPORTED
1313
#include <charconv>
1414
#endif
1515

@@ -229,7 +229,7 @@ int abseil(T d, std::span<char>& buffer) {
229229

230230
template<arithmetic_float T>
231231
int std_to_chars(T d, std::span<char>& buffer) {
232-
#if FROM_CHARS_SUPPORTED
232+
#if TO_CHARS_SUPPORTED
233233
const auto [p, ec]
234234
= std::to_chars(buffer.data(), buffer.data() + buffer.size(), d);
235235
if (ec != std::errc()) {

benchmarks/benchmark.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
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

2529
using Benchmarks::arithmetic_float;
2630
using Benchmarks::BenchArgs;
@@ -45,16 +49,17 @@ void evaluateProperties(const std::vector<T> &lines,
4549
const int vRef = Benchmarks::std_to_chars(d, bufRef);
4650
bufRef[vRef] = '\0';
4751
T dRef;
48-
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);
4955
assert(ptr == bufRef.data() + vRef);
5056
assert(ec == std::errc());
5157
assert(d == dRef);
52-
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)
@@ -132,21 +137,22 @@ cxxopts::Options
132137

133138
int main(int argc, char **argv) {
134139
try {
135-
options.add_options()("f,file", "File name.",
136-
cxxopts::value<std::string>()->default_value(""))(
137-
"v,volume", "Volume (number of floats generated).",
138-
cxxopts::value<size_t>()->default_value("100000"))(
139-
"m,model", "Random Model.",
140-
cxxopts::value<std::string>()->default_value("uniform"))(
141-
"s,single", "Use single precision instead of double.",
142-
cxxopts::value<bool>()->default_value("false"))(
143-
"t,test", "Test the algorithms and find their properties.",
144-
cxxopts::value<bool>()->default_value("false"))(
145-
"d,dragon", "Enable dragon4 (current impl. triggers some asserts).",
146-
cxxopts::value<bool>()->default_value("false"))(
147-
"e,errol", "Enable errol3 (current impl. returns invalid values, e.g., for 0).",
148-
cxxopts::value<bool>()->default_value("false"))(
149-
"h,help", "Print usage.");
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.",
148+
cxxopts::value<bool>()->default_value("false"))
149+
("t,test", "Test the algorithms and find their properties.",
150+
cxxopts::value<bool>()->default_value("false"))
151+
("d,dragon", "Enable dragon4 (current impl. triggers some asserts).",
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.");
150156
const auto result = options.parse(argc, argv);
151157

152158
if (result["help"].as<bool>()) {
@@ -195,7 +201,7 @@ int main(int argc, char **argv) {
195201
args[Benchmarks::TEJU_JAGUA] = { "teju_jagua" , Benchmarks::teju_jagua<T> , true };
196202
args[Benchmarks::DOUBLE_CONVERSION] = { "double_conversion" , Benchmarks::double_conversion<T> , true };
197203
args[Benchmarks::ABSEIL] = { "abseil" , Benchmarks::abseil<T> , ABSEIL_SUPPORTED };
198-
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 };
199205
return args;
200206
};
201207

0 commit comments

Comments
 (0)