Skip to content

Commit 33ce3bf

Browse files
committed
[WTF] Replace dragonbox with zmij
https://bugs.webkit.org/show_bug.cgi?id=328124 rdar://190466337 Reviewed by Devin Rousso. This patch replaces dragonbox with zmij, which is state-of-the-art double-to-string conversion. We import zmij as-is, and wrap it for following edge cases in dtoa.cpp. 1. zero 2. subnormals 3. non-finite values 4. the magnitudes [2^-30, 2^-13) and [2^53, 2^70), where zmij picks a different notation or pads the exponent Test: Tools/TestWebKitAPI/Tests/WTF/ZmijTest.cpp * Source/JavaScriptCore/runtime/JSONObject.cpp: (JSC::bufferMode>::append): * Source/JavaScriptCore/runtime/NumberPrototype.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): * Source/WTF/LICENSE-dragonbox.txt: Removed. * Source/WTF/LICENSE-zmij.txt: Added. * Source/WTF/WTF.xcodeproj/project.pbxproj: * Source/WTF/wtf/CMakeLists.txt: * Source/WTF/wtf/Zmij.cpp: Added. * Source/WTF/wtf/dragonbox/detail/bits.h: Removed. * Source/WTF/wtf/dragonbox/detail/cache_holder.h: Removed. * Source/WTF/wtf/dragonbox/detail/decimal_fp.h: Removed. * Source/WTF/wtf/dragonbox/detail/div.h: Removed. * Source/WTF/wtf/dragonbox/detail/log.h: Removed. * Source/WTF/wtf/dragonbox/detail/policy.h: Removed. * Source/WTF/wtf/dragonbox/detail/policy_holder.h: Removed. * Source/WTF/wtf/dragonbox/detail/util.h: Removed. * Source/WTF/wtf/dragonbox/detail/wuint.h: Removed. * Source/WTF/wtf/dragonbox/dragonbox.h: Removed. * Source/WTF/wtf/dragonbox/dragonbox_to_chars.cpp: Removed. * Source/WTF/wtf/dragonbox/dragonbox_to_chars.h: Removed. * Source/WTF/wtf/dragonbox/ieee754_format.h: Removed. * Source/WTF/wtf/dtoa.cpp: (WTF::eightDecimalDigits): (WTF::numberToStringAndSize): Deleted. (WTF::numberToStringWithTrailingPoint): Deleted. (WTF::truncateTrailingZeros): Deleted. (WTF::numberToFixedPrecisionString): Deleted. (WTF::numberToFixedWidthString): Deleted. (WTF::numberToCSSString): Deleted. * Source/WTF/wtf/dtoa.h: * Source/WTF/wtf/zmij/zmij.cc.h: Added. * Source/WTF/wtf/zmij/zmij.h: Added. * Tools/Scripts/webkitpy/style/checker.py: * Tools/TestWebKitAPI/CMakeLists.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WTF/DragonBoxTest.cpp: Removed. * Tools/TestWebKitAPI/Tests/WTF/ZmijTest.cpp: Added. (TestWebKitAPI::zmijToString): (TestWebKitAPI::zmijToExponential): (TestWebKitAPI::doubleConversionToString): (TestWebKitAPI::doubleConversionToExponential): (TestWebKitAPI::checkDouble): (TestWebKitAPI::checkFloat): (TestWebKitAPI::TEST(WTF, ZmijSpecialValues)): (TestWebKitAPI::TEST(WTF, ZmijRandom)): (TestWebKitAPI::TEST(WTF, Zmij_perf)): Canonical link: https://commits.webkit.org/324317@main
1 parent b6b871f commit 33ce3bf

29 files changed

Lines changed: 3639 additions & 4877 deletions

‎Source/JavaScriptCore/runtime/JSONObject.cpp‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <charconv>
4343
#include <wtf/MathExtras.h>
4444
#include <wtf/UnalignedAccess.h>
45-
#include <wtf/dragonbox/dragonbox_to_chars.h>
45+
#include <wtf/dtoa.h>
4646
#include <wtf/text/EscapedFormsForJSON.h>
4747
#include <wtf/text/MakeString.h>
4848
#include <wtf/text/ParsingUtilities.h>
@@ -1382,17 +1382,15 @@ void FastStringifier<CharType, bufferMode>::append(JSValue value)
13821382
append('n', 'u', 'l', 'l');
13831383
return;
13841384
}
1385-
if (!hasRemainingCapacity(WTF::dragonbox::max_string_length<WTF::dragonbox::ieee754_binary64>())) [[unlikely]] {
1385+
if (!hasRemainingCapacity(numberToShortestStringBufferSize)) [[unlikely]] {
13861386
recordBufferFull();
13871387
return;
13881388
}
1389-
if constexpr (sizeof(CharType) == 1) {
1390-
auto cursor = WTF::dragonbox::detail::to_chars_n<WTF::dragonbox::Mode::ToShortest>(number, byteCast<char>(bufferSpan().subspan(m_length)));
1391-
m_length = cursor.data() - reinterpret_cast<char*>(buffer());
1392-
} else {
1393-
std::array<char, WTF::dragonbox::max_string_length<WTF::dragonbox::ieee754_binary64>()> temporary;
1394-
auto cursor = WTF::dragonbox::detail::to_chars_n<WTF::dragonbox::Mode::ToShortest>(number, std::span { temporary });
1395-
size_t length = cursor.data() - temporary.data();
1389+
if constexpr (sizeof(CharType) == 1)
1390+
m_length += WTF::numberToShortestString(number, byteCast<char>(bufferSpan().subspan(m_length)).template first<numberToShortestStringBufferSize>());
1391+
else {
1392+
std::array<char, numberToShortestStringBufferSize> temporary;
1393+
size_t length = WTF::numberToShortestString(number, temporary);
13961394
WTF::copyElements(spanReinterpretCast<uint16_t>(bufferSpan().subspan(m_length)), byteCast<uint8_t>(std::span { temporary }).first(length));
13971395
m_length += length;
13981396
}

‎Source/JavaScriptCore/runtime/NumberPrototype.cpp‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "Uint16WithFraction.h"
3232
#include <wtf/Assertions.h>
3333
#include <wtf/TZoneMallocInlines.h>
34-
#include <wtf/dragonbox/dragonbox_to_chars.h>
3534
#include <wtf/dtoa.h>
3635
#include <wtf/dtoa/double-conversion.h>
3736
#include <wtf/text/MakeString.h>
@@ -414,14 +413,13 @@ JSC_DEFINE_HOST_FUNCTION(numberProtoFuncToExponential, (JSGlobalObject* globalOb
414413

415414
// Round if the argument is not undefined, always format as exponential.
416415
NumberToStringBuffer buffer;
416+
if (arg.isUndefined())
417+
return JSValue::encode(jsString(vm, String::fromLatin1(WTF::numberToShortestExponentialString(x, buffer))));
418+
417419
DoubleConversionStringBuilder builder { std::span<char> { buffer } };
418420
builder.Reset();
419-
if (arg.isUndefined())
420-
WTF::dragonbox::ToExponential(x, &builder);
421-
else {
422-
const DoubleToStringConverter& converter = DoubleToStringConverter::EcmaScriptConverter();
423-
converter.ToExponential(x, decimalPlaces, &builder);
424-
}
421+
const DoubleToStringConverter& converter = DoubleToStringConverter::EcmaScriptConverter();
422+
converter.ToExponential(x, decimalPlaces, &builder);
425423
return JSValue::encode(jsString(vm, String::fromLatin1(builder.Finalize())));
426424
}
427425

‎Source/WTF/LICENSE-dragonbox.txt‎

Lines changed: 0 additions & 248 deletions
This file was deleted.

‎Source/WTF/LICENSE-zmij.txt‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Victor Zverovich
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)