Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JSC] Optimize parseFloat(number)
https://bugs.webkit.org/show_bug.cgi?id=258115 rdar://110822592 Reviewed by Mark Lam. parseFloat(String(double)) can generate the exact same double *except for -0.0*. This patch adds a fast path returning double when parseFloat's input is double. We also extend parseInt optimization in C++ runtime function, which can now accept radix = 10 cases too (it is already handled in DFG and FTL, but expand it for lower tiers). * JSTests/stress/parse-float-double.js: Added. (shouldBe): (parseFloatDouble): * JSTests/stress/parse-int-double-radix-undefined.js: Added. (shouldBe): (parseIntDouble): * Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): Canonical link: https://commits.webkit.org/265189@main
- Loading branch information
1 parent
81c064d
commit c6ee90f
Showing
3 changed files
with
78 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function shouldBe(actual, expected) { | ||
if (actual !== expected) | ||
throw new Error('bad value: ' + actual); | ||
} | ||
|
||
function parseFloatDouble(value) { | ||
return parseFloat(value); | ||
} | ||
noInline(parseFloatDouble); | ||
|
||
for (var i = 0; i < 1e5; ++i) { | ||
shouldBe(Object.is(parseFloatDouble(0.0), 0), true); | ||
shouldBe(Object.is(parseFloatDouble(-0.0), 0), true); // Not -0 since -0.0.toString() is "0". | ||
shouldBe(Object.is(parseFloatDouble(-1.0), -1.0), true); | ||
shouldBe(Object.is(parseFloatDouble(-0.01), -0.01), true); | ||
shouldBe(Object.is(parseFloatDouble(-1.1), -1.1), true); | ||
shouldBe(Object.is(parseFloatDouble(-1.0), -1.0), true); | ||
shouldBe(Object.is(parseFloatDouble(-0.9), -0.9), true); | ||
shouldBe(Object.is(parseFloatDouble(-1.000000001), -1.000000001), true); | ||
shouldBe(Object.is(parseFloatDouble(-0.000000001), -0.000000001), true); | ||
shouldBe(Object.is(parseFloatDouble(0.000000001), 0.000000001), true); | ||
shouldBe(Object.is(parseFloatDouble(0.000001), 0.000001), true); | ||
shouldBe(Object.is(parseFloatDouble(0.000001), 0.000001), true); | ||
shouldBe(Object.is(parseFloatDouble(0.0000001), 0.0000001), true); | ||
shouldBe(Object.is(parseFloatDouble(-0.0000001), -0.0000001), true); | ||
shouldBe(Object.is(parseFloatDouble(1e+21), 1e+21), true); | ||
shouldBe(Object.is(parseFloatDouble(1e+20), 1e+20), true); | ||
shouldBe(Object.is(parseFloatDouble(NaN), NaN), true); | ||
shouldBe(Object.is(parseFloatDouble(Infinity), Infinity), true); | ||
shouldBe(Object.is(parseFloatDouble(-Infinity), -Infinity), true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function shouldBe(actual, expected) { | ||
if (actual !== expected) | ||
throw new Error('bad value: ' + actual); | ||
} | ||
|
||
function parseIntDouble(value) { | ||
return parseInt(value); | ||
} | ||
noInline(parseIntDouble); | ||
|
||
for (var i = 0; i < 1e5; ++i) { | ||
shouldBe(Object.is(parseIntDouble(-0.0), 0), true); // Not -0 since -0.0.toString() is "0". | ||
shouldBe(Object.is(parseIntDouble(-1.0), -1.0), true); | ||
shouldBe(Object.is(parseIntDouble(-0.01), -0), true); | ||
shouldBe(Object.is(parseIntDouble(-1.1), -1.0), true); | ||
shouldBe(Object.is(parseIntDouble(-1.0), -1.0), true); | ||
shouldBe(Object.is(parseIntDouble(-0.9), -0.0), true); | ||
shouldBe(Object.is(parseIntDouble(-1.000000001), -1.0), true); | ||
shouldBe(Object.is(parseIntDouble(-0.000000001), -1), true); // Since it is -1e-9. | ||
shouldBe(Object.is(parseIntDouble(0.000000001), 1), true); // Since it is 1e-9. | ||
shouldBe(Object.is(parseIntDouble(0.000001), 0), true); | ||
shouldBe(Object.is(parseIntDouble(0.000001), 0), true); | ||
shouldBe(Object.is(parseIntDouble(0.0000001), 1), true); // Since it is 1e-6. | ||
shouldBe(Object.is(parseIntDouble(-0.0000001), -1), true); // Since it is -1e-6. | ||
shouldBe(Object.is(parseIntDouble(1e+21), 1), true); // Since it is 1e+21. | ||
shouldBe(Object.is(parseIntDouble(1e+20), 1e+20), true); | ||
shouldBe(Object.is(parseIntDouble(NaN), NaN), true); | ||
shouldBe(Object.is(parseIntDouble(Infinity), NaN), true); | ||
shouldBe(Object.is(parseIntDouble(-Infinity), NaN), true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters