OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <functional> | 5 #include <functional> |
6 #include <limits> | 6 #include <limits> |
7 | 7 |
8 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
9 #include "test/cctest/compiler/codegen-tester.h" | 9 #include "test/cctest/compiler/codegen-tester.h" |
10 #include "test/cctest/compiler/value-helper.h" | 10 #include "test/cctest/compiler/value-helper.h" |
(...skipping 3864 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3875 } | 3875 } |
3876 m.Return(one); | 3876 m.Return(one); |
3877 m.Call(); | 3877 m.Call(); |
3878 for (int i = 0; i < kInputSize; i++) { | 3878 for (int i = 0; i < kInputSize; i++) { |
3879 CHECK_EQ(outputs[i], i + 2); | 3879 CHECK_EQ(outputs[i], i + 2); |
3880 } | 3880 } |
3881 } | 3881 } |
3882 | 3882 |
3883 #endif // MACHINE_ASSEMBLER_SUPPORTS_CALL_C | 3883 #endif // MACHINE_ASSEMBLER_SUPPORTS_CALL_C |
3884 | 3884 |
3885 #endif | 3885 |
| 3886 static bool sadd_overflow(int32_t x, int32_t y, int32_t* val) { |
| 3887 int32_t v = |
| 3888 static_cast<int32_t>(static_cast<uint32_t>(x) + static_cast<uint32_t>(y)); |
| 3889 *val = v; |
| 3890 return (((v ^ x) & (v ^ y)) >> 31) & 1; |
| 3891 } |
| 3892 |
| 3893 |
| 3894 TEST(RunInt32AddWithOverflowP) { |
| 3895 int32_t actual_val = -1; |
| 3896 RawMachineAssemblerTester<int32_t> m; |
| 3897 Int32BinopTester bt(&m); |
| 3898 Node* val, *ovf; |
| 3899 m.Int32AddWithOverflow(bt.param0, bt.param1, &val, &ovf); |
| 3900 m.StoreToPointer(&actual_val, kMachineWord32, val); |
| 3901 bt.AddReturn(ovf); |
| 3902 FOR_INT32_INPUTS(i) { |
| 3903 FOR_INT32_INPUTS(j) { |
| 3904 int32_t expected_val; |
| 3905 int expected_ovf = sadd_overflow(*i, *j, &expected_val); |
| 3906 CHECK_EQ(expected_ovf, bt.call(*i, *j)); |
| 3907 CHECK_EQ(expected_val, actual_val); |
| 3908 } |
| 3909 } |
| 3910 } |
| 3911 |
| 3912 |
| 3913 TEST(RunInt32AddWithOverflowImm) { |
| 3914 int32_t actual_val = -1, expected_val = 0; |
| 3915 FOR_INT32_INPUTS(i) { |
| 3916 { |
| 3917 RawMachineAssemblerTester<int32_t> m(kMachineWord32); |
| 3918 Node* val, *ovf; |
| 3919 m.Int32AddWithOverflow(m.Int32Constant(*i), m.Parameter(0), &val, &ovf); |
| 3920 m.StoreToPointer(&actual_val, kMachineWord32, val); |
| 3921 m.Return(ovf); |
| 3922 FOR_INT32_INPUTS(j) { |
| 3923 int expected_ovf = sadd_overflow(*i, *j, &expected_val); |
| 3924 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 3925 CHECK_EQ(expected_val, actual_val); |
| 3926 } |
| 3927 } |
| 3928 { |
| 3929 RawMachineAssemblerTester<int32_t> m(kMachineWord32); |
| 3930 Node* val, *ovf; |
| 3931 m.Int32AddWithOverflow(m.Parameter(0), m.Int32Constant(*i), &val, &ovf); |
| 3932 m.StoreToPointer(&actual_val, kMachineWord32, val); |
| 3933 m.Return(ovf); |
| 3934 FOR_INT32_INPUTS(j) { |
| 3935 int expected_ovf = sadd_overflow(*i, *j, &expected_val); |
| 3936 CHECK_EQ(expected_ovf, m.Call(*j)); |
| 3937 CHECK_EQ(expected_val, actual_val); |
| 3938 } |
| 3939 } |
| 3940 FOR_INT32_INPUTS(j) { |
| 3941 RawMachineAssemblerTester<int32_t> m; |
| 3942 Node* val, *ovf; |
| 3943 m.Int32AddWithOverflow(m.Int32Constant(*i), m.Int32Constant(*j), &val, |
| 3944 &ovf); |
| 3945 m.StoreToPointer(&actual_val, kMachineWord32, val); |
| 3946 m.Return(ovf); |
| 3947 int expected_ovf = sadd_overflow(*i, *j, &expected_val); |
| 3948 CHECK_EQ(expected_ovf, m.Call()); |
| 3949 CHECK_EQ(expected_val, actual_val); |
| 3950 } |
| 3951 } |
| 3952 } |
| 3953 |
| 3954 |
| 3955 TEST(RunInt32AddWithOverflowInBranchP) { |
| 3956 MLabel blocka, blockb; |
| 3957 RawMachineAssemblerTester<int32_t> m; |
| 3958 Int32BinopTester bt(&m); |
| 3959 Node* val, *ovf; |
| 3960 m.Int32AddWithOverflow(bt.param0, bt.param1, &val, &ovf); |
| 3961 m.Branch(ovf, &blocka, &blockb); |
| 3962 m.Bind(&blocka); |
| 3963 bt.AddReturn(m.Word32Not(val)); |
| 3964 m.Bind(&blockb); |
| 3965 bt.AddReturn(val); |
| 3966 FOR_UINT32_INPUTS(i) { |
| 3967 FOR_UINT32_INPUTS(j) { |
| 3968 int32_t expected; |
| 3969 if (sadd_overflow(*i, *j, &expected)) expected = ~expected; |
| 3970 CHECK_EQ(expected, bt.call(*i, *j)); |
| 3971 } |
| 3972 } |
| 3973 } |
| 3974 |
| 3975 #endif // V8_TURBOFAN_TARGET |
OLD | NEW |