Skip to content

Commit 7265ea9

Browse files
jakobkummerowCommit Bot
authored andcommitted
Fix DoubleToFloat32 corner case
For a few double value above the max float, we have to round down to that max float rather than rounding up to infinity. Bug: chromium:956564 Change-Id: I34be1def5330bd4c3352b792d20dd500f108d9e1 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1585852 Commit-Queue: Jakob Kummerow <[email protected]> Commit-Queue: Andreas Haas <[email protected]> Auto-Submit: Jakob Kummerow <[email protected]> Reviewed-by: Andreas Haas <[email protected]> Cr-Commit-Position: refs/heads/master@{#61052}
1 parent 4153feb commit 7265ea9

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/conversions-inl.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,24 @@ inline unsigned int FastD2UI(double x) {
5959

6060

6161
inline float DoubleToFloat32(double x) {
62-
typedef std::numeric_limits<float> limits;
63-
if (x > limits::max()) return limits::infinity();
64-
if (x < limits::lowest()) return -limits::infinity();
62+
using limits = std::numeric_limits<float>;
63+
if (x > limits::max()) {
64+
// kRoundingThreshold is the maximum double that rounds down to
65+
// the maximum representable float. Its mantissa bits are:
66+
// 1111111111111111111111101111111111111111111111111111
67+
// [<--- float range --->]
68+
// Note the zero-bit right after the float mantissa range, which
69+
// determines the rounding-down.
70+
static const double kRoundingThreshold = 3.4028235677973362e+38;
71+
if (x <= kRoundingThreshold) return limits::max();
72+
return limits::infinity();
73+
}
74+
if (x < limits::lowest()) {
75+
// Same as above, mirrored to negative numbers.
76+
static const double kRoundingThreshold = -3.4028235677973362e+38;
77+
if (x >= kRoundingThreshold) return limits::lowest();
78+
return -limits::infinity();
79+
}
6580
return static_cast<float>(x);
6681
}
6782

0 commit comments

Comments
 (0)