Commit 5a4394ac authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

[wasm-simd] Use saturated_cast and remove Saturate helper

Bug: v8:11074
Change-Id: I21926f3c8f640d26b9e067569455b49211321148
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2658075Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72438}
parent 3b4a80cf
......@@ -3929,19 +3929,6 @@ U Widen(T value) {
return static_cast<U>(value);
}
template <typename T, typename U>
U Narrow(T value) {
static_assert(sizeof(int8_t) < sizeof(T), "T must be int16_t or larger");
static_assert(sizeof(U) < sizeof(T), "T must larger than U");
static_assert(std::is_unsigned<T>() == std::is_unsigned<U>(),
"Signed-ness of T and U must match");
// Make sure value can be expressed in the smaller type; otherwise, the
// casted result is implementation defined.
DCHECK_LE(std::numeric_limits<T>::min(), value);
DCHECK_GE(std::numeric_limits<T>::max(), value);
return static_cast<U>(value);
}
template <typename T, typename U>
void Widen(Simulator* simulator, int Vd, int Vm) {
static const int kLanes = 8 / sizeof(T);
......@@ -3974,19 +3961,7 @@ void SaturatingNarrow(Simulator* simulator, int Vd, int Vm) {
U dst[kLanes];
simulator->get_neon_register(Vm, src);
for (int i = 0; i < kLanes; i++) {
dst[i] = Narrow<T, U>(Saturate<U>(src[i]));
}
simulator->set_neon_register<U, kDoubleSize>(Vd, dst);
}
template <typename T, typename U>
void SaturatingUnsignedNarrow(Simulator* simulator, int Vd, int Vm) {
static const int kLanes = 16 / sizeof(T);
T src[kLanes];
U dst[kLanes];
simulator->get_neon_register(Vm, src);
for (int i = 0; i < kLanes; i++) {
dst[i] = Saturate<U>(src[i]);
dst[i] = base::saturated_cast<U>(src[i]);
}
simulator->set_neon_register<U, kDoubleSize>(Vd, dst);
}
......@@ -4731,7 +4706,7 @@ void Simulator::DecodeAdvancedSIMDTwoOrThreeRegisters(Instruction* instr) {
if (src_unsigned) {
SaturatingNarrow<uint16_t, uint8_t>(this, Vd, Vm);
} else if (dst_unsigned) {
SaturatingUnsignedNarrow<int16_t, uint8_t>(this, Vd, Vm);
SaturatingNarrow<int16_t, uint8_t>(this, Vd, Vm);
} else {
SaturatingNarrow<int16_t, int8_t>(this, Vd, Vm);
}
......@@ -4741,7 +4716,7 @@ void Simulator::DecodeAdvancedSIMDTwoOrThreeRegisters(Instruction* instr) {
if (src_unsigned) {
SaturatingNarrow<uint32_t, uint16_t>(this, Vd, Vm);
} else if (dst_unsigned) {
SaturatingUnsignedNarrow<int32_t, uint16_t>(this, Vd, Vm);
SaturatingNarrow<int32_t, uint16_t>(this, Vd, Vm);
} else {
SaturatingNarrow<int32_t, int16_t>(this, Vd, Vm);
}
......@@ -4751,7 +4726,7 @@ void Simulator::DecodeAdvancedSIMDTwoOrThreeRegisters(Instruction* instr) {
if (src_unsigned) {
SaturatingNarrow<uint64_t, uint32_t>(this, Vd, Vm);
} else if (dst_unsigned) {
SaturatingUnsignedNarrow<int64_t, uint32_t>(this, Vd, Vm);
SaturatingNarrow<int64_t, uint32_t>(this, Vd, Vm);
} else {
SaturatingNarrow<int64_t, int32_t>(this, Vd, Vm);
}
......
......@@ -8,6 +8,7 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <string>
#include <type_traits>
......@@ -17,6 +18,7 @@
#include "src/base/logging.h"
#include "src/base/macros.h"
#include "src/base/platform/platform.h"
#include "src/base/safe_conversions.h"
#include "src/base/v8-fallthrough.h"
#include "src/common/globals.h"
#include "src/utils/allocation.h"
......@@ -122,15 +124,6 @@ inline double Modulo(double x, double y) {
#endif
}
template <typename T>
T Saturate(int64_t value) {
static_assert(sizeof(int64_t) > sizeof(T), "T must be int32_t or smaller");
int64_t min = static_cast<int64_t>(std::numeric_limits<T>::min());
int64_t max = static_cast<int64_t>(std::numeric_limits<T>::max());
int64_t clamped = std::max(min, std::min(max, value));
return static_cast<T>(clamped);
}
template <typename T>
T SaturateAdd(T a, T b) {
if (std::is_signed<T>::value) {
......@@ -187,7 +180,7 @@ T SaturateRoundingQMul(T a, T b) {
int64_t product = a * b;
product += round_const;
product >>= (size_in_bits - 1);
return Saturate<T>(product);
return base::saturated_cast<T>(product);
}
// Multiply two numbers, returning a result that is twice as wide, no overflow.
......
......@@ -2338,8 +2338,8 @@ WASM_SIMD_TEST(I16x8ConvertI32x4) {
FOR_INT32_INPUTS(x) {
r.Call(x);
int16_t expected_signed = Saturate<int16_t>(x);
int16_t expected_unsigned = Saturate<uint16_t>(x);
int16_t expected_signed = base::saturated_cast<int16_t>(x);
int16_t expected_unsigned = base::saturated_cast<uint16_t>(x);
for (int i = 0; i < 8; i++) {
CHECK_EQ(expected_signed, ReadLittleEndianValue<int16_t>(&g0[i]));
CHECK_EQ(expected_unsigned, ReadLittleEndianValue<int16_t>(&g1[i]));
......@@ -2767,8 +2767,8 @@ WASM_SIMD_TEST(I8x16ConvertI16x8) {
FOR_INT16_INPUTS(x) {
r.Call(x);
int8_t expected_signed = Saturate<int8_t>(x);
int8_t expected_unsigned = Saturate<uint8_t>(x);
int8_t expected_signed = base::saturated_cast<int8_t>(x);
int8_t expected_unsigned = base::saturated_cast<uint8_t>(x);
for (int i = 0; i < 16; i++) {
CHECK_EQ(expected_signed, ReadLittleEndianValue<int8_t>(&g0[i]));
CHECK_EQ(expected_unsigned, ReadLittleEndianValue<int8_t>(&g1[i]));
......
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