Index: test/cctest/compiler/test-run-machops.cc |
diff --git a/test/cctest/compiler/test-run-machops.cc b/test/cctest/compiler/test-run-machops.cc |
index d05360d2f623d47cb178b8df3f05aa89cb11bbbc..4fa2938bd6eb98874dc9ef2b4bf2b9e98778ef17 100644 |
--- a/test/cctest/compiler/test-run-machops.cc |
+++ b/test/cctest/compiler/test-run-machops.cc |
@@ -3891,6 +3891,14 @@ static bool sadd_overflow(int32_t x, int32_t y, int32_t* val) { |
} |
+static bool ssub_overflow(int32_t x, int32_t y, int32_t* val) { |
+ int32_t v = |
+ static_cast<int32_t>(static_cast<uint32_t>(x) - static_cast<uint32_t>(y)); |
+ *val = v; |
+ return (((v ^ x) & (v ^ ~y)) >> 31) & 1; |
+} |
+ |
+ |
TEST(RunInt32AddWithOverflowP) { |
int32_t actual_val = -1; |
RawMachineAssemblerTester<int32_t> m; |
@@ -3972,4 +3980,86 @@ TEST(RunInt32AddWithOverflowInBranchP) { |
} |
} |
+ |
+TEST(RunInt32SubWithOverflowP) { |
+ int32_t actual_val = -1; |
+ RawMachineAssemblerTester<int32_t> m; |
+ Int32BinopTester bt(&m); |
+ Node* val, *ovf; |
+ m.Int32SubWithOverflow(bt.param0, bt.param1, &val, &ovf); |
+ m.StoreToPointer(&actual_val, kMachineWord32, val); |
+ bt.AddReturn(ovf); |
+ FOR_INT32_INPUTS(i) { |
+ FOR_INT32_INPUTS(j) { |
+ int32_t expected_val; |
+ int expected_ovf = ssub_overflow(*i, *j, &expected_val); |
+ CHECK_EQ(expected_ovf, bt.call(*i, *j)); |
+ CHECK_EQ(expected_val, actual_val); |
+ } |
+ } |
+} |
+ |
+ |
+TEST(RunInt32SubWithOverflowImm) { |
+ int32_t actual_val = -1, expected_val = 0; |
+ FOR_INT32_INPUTS(i) { |
+ { |
+ RawMachineAssemblerTester<int32_t> m(kMachineWord32); |
+ Node* val, *ovf; |
+ m.Int32SubWithOverflow(m.Int32Constant(*i), m.Parameter(0), &val, &ovf); |
+ m.StoreToPointer(&actual_val, kMachineWord32, val); |
+ m.Return(ovf); |
+ FOR_INT32_INPUTS(j) { |
+ int expected_ovf = ssub_overflow(*i, *j, &expected_val); |
+ CHECK_EQ(expected_ovf, m.Call(*j)); |
+ CHECK_EQ(expected_val, actual_val); |
+ } |
+ } |
+ { |
+ RawMachineAssemblerTester<int32_t> m(kMachineWord32); |
+ Node* val, *ovf; |
+ m.Int32SubWithOverflow(m.Parameter(0), m.Int32Constant(*i), &val, &ovf); |
+ m.StoreToPointer(&actual_val, kMachineWord32, val); |
+ m.Return(ovf); |
+ FOR_INT32_INPUTS(j) { |
+ int expected_ovf = ssub_overflow(*j, *i, &expected_val); |
+ CHECK_EQ(expected_ovf, m.Call(*j)); |
+ CHECK_EQ(expected_val, actual_val); |
+ } |
+ } |
+ FOR_INT32_INPUTS(j) { |
+ RawMachineAssemblerTester<int32_t> m; |
+ Node* val, *ovf; |
+ m.Int32SubWithOverflow(m.Int32Constant(*i), m.Int32Constant(*j), &val, |
+ &ovf); |
+ m.StoreToPointer(&actual_val, kMachineWord32, val); |
+ m.Return(ovf); |
+ int expected_ovf = ssub_overflow(*i, *j, &expected_val); |
+ CHECK_EQ(expected_ovf, m.Call()); |
+ CHECK_EQ(expected_val, actual_val); |
+ } |
+ } |
+} |
+ |
+ |
+TEST(RunInt32SubWithOverflowInBranchP) { |
+ MLabel blocka, blockb; |
+ RawMachineAssemblerTester<int32_t> m; |
+ Int32BinopTester bt(&m); |
+ Node* val, *ovf; |
+ m.Int32SubWithOverflow(bt.param0, bt.param1, &val, &ovf); |
+ m.Branch(ovf, &blocka, &blockb); |
+ m.Bind(&blocka); |
+ bt.AddReturn(m.Word32Not(val)); |
+ m.Bind(&blockb); |
+ bt.AddReturn(val); |
+ FOR_UINT32_INPUTS(i) { |
+ FOR_UINT32_INPUTS(j) { |
+ int32_t expected; |
+ if (ssub_overflow(*i, *j, &expected)) expected = ~expected; |
+ CHECK_EQ(expected, bt.call(*i, *j)); |
+ } |
+ } |
+} |
+ |
#endif // V8_TURBOFAN_TARGET |