Commit 492101a8 authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

Use safe conversion helper to convert double to int64_t

Bug: v8:10933
Change-Id: I3a0526e4744b7a36d03d2bfd182616969b9db12b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2466377
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70547}
parent 367f66fc
......@@ -5,12 +5,13 @@
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits>
#include "include/v8config.h"
#include "src/base/bits.h"
#include "src/base/ieee754.h"
#include "src/base/safe_conversions.h"
#include "src/common/assert-scope.h"
#include "src/utils/memcopy.h"
#include "src/wasm/wasm-objects-inl.h"
......@@ -205,12 +206,8 @@ int32_t float32_to_uint64_wrapper(Address data) {
}
int32_t float64_to_int64_wrapper(Address data) {
// We use "<" here to check the upper bound because of rounding problems: With
// "<=" some inputs would be considered within int64 range which are actually
// not within int64 range.
double input = ReadUnalignedValue<double>(data);
if (input >= static_cast<double>(std::numeric_limits<int64_t>::min()) &&
input < static_cast<double>(std::numeric_limits<int64_t>::max())) {
if (base::IsValueInRangeForNumericType<int64_t>(input)) {
WriteUnalignedValue<int64_t>(data, static_cast<int64_t>(input));
return 1;
}
......@@ -270,11 +267,7 @@ void float32_to_uint64_sat_wrapper(Address data) {
void float64_to_int64_sat_wrapper(Address data) {
double input = ReadUnalignedValue<double>(data);
// We use "<" here to check the upper bound because of rounding problems: With
// "<=" some inputs would be considered within int64 range which are actually
// not within int64 range.
if (input < static_cast<double>(std::numeric_limits<int64_t>::max()) &&
input >= static_cast<double>(std::numeric_limits<int64_t>::min())) {
if (base::IsValueInRangeForNumericType<int64_t>(input)) {
WriteUnalignedValue<int64_t>(data, static_cast<int64_t>(input));
return;
}
......
......@@ -5,6 +5,7 @@
#include <cstdint>
#include "src/base/overflowing-math.h"
#include "src/base/safe_conversions.h"
#include "src/codegen/assembler-inl.h"
#include "src/objects/objects-inl.h"
#include "src/wasm/wasm-arguments.h"
......@@ -116,8 +117,7 @@ TEST(TestCWasmEntryArgPassing_int64_double) {
[](double d) { return static_cast<int64_t>(d); });
FOR_FLOAT64_INPUTS(d) {
if (d < static_cast<double>(std::numeric_limits<int64_t>::max()) &&
d >= static_cast<double>(std::numeric_limits<int64_t>::min())) {
if (base::IsValueInRangeForNumericType<int64_t>(d)) {
tester.CheckCall(d);
}
}
......
......@@ -8,9 +8,9 @@
#include "src/base/bits.h"
#include "src/base/overflowing-math.h"
#include "src/base/safe_conversions.h"
#include "src/codegen/assembler-inl.h"
#include "src/objects/objects-inl.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/value-helper.h"
#include "test/cctest/wasm/wasm-run-utils.h"
......@@ -759,8 +759,7 @@ WASM_EXEC_TEST(I64SConvertF64) {
BUILD(r, WASM_I64_SCONVERT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
if (i < static_cast<double>(std::numeric_limits<int64_t>::max()) &&
i >= static_cast<double>(std::numeric_limits<int64_t>::min())) {
if (base::IsValueInRangeForNumericType<int64_t>(i)) {
CHECK_EQ(static_cast<int64_t>(i), r.Call(i));
} else {
CHECK_TRAP64(r.Call(i));
......@@ -773,8 +772,7 @@ WASM_EXEC_TEST(I64SConvertSatF64) {
BUILD(r, WASM_I64_SCONVERT_SAT_F64(WASM_GET_LOCAL(0)));
FOR_FLOAT64_INPUTS(i) {
int64_t expected;
if (i < static_cast<double>(std::numeric_limits<int64_t>::max()) &&
i >= static_cast<double>(std::numeric_limits<int64_t>::min())) {
if (base::IsValueInRangeForNumericType<int64_t>(i)) {
expected = static_cast<int64_t>(i);
} else if (std::isnan(i)) {
expected = static_cast<int64_t>(0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment