Skip to content

Commit 3fe4c27

Browse files
lemirejaja360
authored andcommitted
More fixes
1 parent edcf2a3 commit 3fe4c27

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

benchmarks/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@ int main(void) {
1616
}
1717
" from_chars_ok)
1818
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)
30+
target_compile_definitions(benchmark PUBLIC TO_CHARS_SUPPORTED=$<IF:$<BOOL:${to_chars}>,1,0>)
1931

32+
CPMAddPackage(
33+
NAME fast_float
34+
GITHUB_REPOSITORY "fastfloat/fast_float"
35+
GIT_TAG v8.0.2)
36+
target_link_libraries(benchmark PUBLIC fast_float)
2037
if (NOT WIN32)
2138
target_link_libraries(benchmark PUBLIC netlib)
2239
target_compile_definitions(benchmark PUBLIC NETLIB_SUPPORTED=1)
@@ -25,7 +42,7 @@ else()
2542
endif()
2643

2744
if (NOT CYGWIN)
28-
target_link_libraries(benchmark PUBLIC absl::strings absl::str_format)
45+
target_link_libraries(benchmark PUBLIC absl::str_format)
2946
target_compile_definitions(benchmark PUBLIC ABSEIL_SUPPORTED=1)
3047
else()
3148
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: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
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;
2731

28-
#if FROM_CHARS_SUPPORTED
2932
template <arithmetic_float T>
3033
void 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

6973
template <arithmetic_float T>
7074
void process(const std::vector<T> &lines,
@@ -133,23 +137,22 @@ cxxopts::Options
133137

134138
int 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

Comments
 (0)