Skip to content

Commit 0309a2d

Browse files
committed
add the SwiftDtoa algorithm
1 parent e08fa8c commit 0309a2d

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ endif()
7777
target_link_libraries(benchmark PUBLIC dragonbox::dragonbox_to_chars)
7878
target_link_libraries(benchmark PUBLIC dragon_schubfach_lib)
7979
target_link_libraries(benchmark PUBLIC dragon4_lib)
80+
target_link_libraries(benchmark PUBLIC swift_lib)
8081
target_include_directories(benchmark PUBLIC ${grisu-exact_SOURCE_DIR})

benchmarks/algorithms.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
#include "ryu/ryu.h"
3333
#include "schubfach_32.h"
3434
#include "schubfach_64.h"
35+
#include "swift/Runtime/SwiftDtoa.h"
3536
#if (__SIZEOF_INT128__ == 16) && (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER))
3637
#include "yy_double.h"
3738
#define YY_DOUBLE_SUPPORTED 1
3839
#else
3940
#define YY_DOUBLE_SUPPORTED 0
4041
#endif
42+
4143
namespace Benchmarks {
4244

4345
enum Algorithm {
@@ -57,7 +59,8 @@ enum Algorithm {
5759
ABSEIL = 13,
5860
STD_TO_CHARS = 14,
5961
GRISU3 = 15,
60-
YY_DOUBLE = 16,
62+
SWIFT_DTOA = 16,
63+
YY_DOUBLE = 17,
6164
COUNT // Keep last
6265
};
6366

@@ -230,6 +233,14 @@ int double_conversion(T d, std::span<char>& buffer) {
230233
return strlen(builder.Finalize());
231234
}
232235

236+
template<arithmetic_float T>
237+
int swiftDtoa(T d, std::span<char>& buffer) {
238+
if constexpr (std::is_same_v<T, float>)
239+
return swift_dtoa_optimal_float(d, buffer.data(), buffer.size());
240+
else
241+
return swift_dtoa_optimal_double(d, buffer.data(), buffer.size());
242+
}
243+
233244
template<arithmetic_float T>
234245
int yy_double(T d, std::span<char>& buffer) {
235246
#if YY_DOUBLE_SUPPORTED

benchmarks/benchmark.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ int main(int argc, char **argv) {
197197
args[Benchmarks::ABSEIL] = { "abseil" , Benchmarks::abseil<T> , ABSEIL_SUPPORTED };
198198
args[Benchmarks::STD_TO_CHARS] = { "std::to_chars" , Benchmarks::std_to_chars<T> , TO_CHARS_SUPPORTED };
199199
args[Benchmarks::GRISU3] = { "grisu3" , Benchmarks::grisu3<T> , true };
200+
args[Benchmarks::SWIFT_DTOA] = { "SwiftDtoa" , Benchmarks::swiftDtoa<T> , true };
200201
args[Benchmarks::YY_DOUBLE] = { "yy_double" , Benchmarks::yy_double<T> , YY_DOUBLE_SUPPORTED };
201202
return args;
202203
};

dependencies/CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,31 @@ add_library(dragon4_lib STATIC
9797
if(MATH_LIBRARY)
9898
target_link_libraries(dragon4_lib PUBLIC ${MATH_LIBRARY})
9999
endif()
100-
101100
target_include_directories(dragon4_lib PUBLIC
102101
${dragon4_SOURCE_DIR}
103102
)
104103

104+
FetchContent_Declare(
105+
swift
106+
GIT_REPOSITORY https://github.com/swiftlang/swift.git
107+
GIT_TAG 6a862d2eb7128ff1f317b07e8ad1a6da939775f3
108+
GIT_SHALLOW TRUE
109+
)
110+
FetchContent_GetProperties(swift)
111+
if(NOT swift)
112+
FetchContent_Populate(swift) # Downloads code to ${dragon4_SOURCE_DIR}
113+
endif()
114+
set(swift_SOURCE_DIR ${swift_SOURCE_DIR} PARENT_SCOPE)
115+
add_library(swift_lib STATIC
116+
${swift_SOURCE_DIR}/stdlib/public/runtime/SwiftDtoa.cpp
117+
)
118+
if(MATH_LIBRARY)
119+
target_link_libraries(swift_lib PUBLIC ${MATH_LIBRARY})
120+
endif()
121+
target_include_directories(swift_lib PUBLIC
122+
${swift_SOURCE_DIR}/include
123+
)
124+
105125
FetchContent_Declare(
106126
drachennest # for schubfach
107127
GIT_REPOSITORY https://github.com/abolz/Drachennest.git

0 commit comments

Comments
 (0)