• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/core/GPUTarget.h"
25 #include "arm_compute/core/utils/math/SafeOps.h"
26 #include "tests/Globals.h"
27 #include "tests/Utils.h"
28 #include "tests/framework/Asserts.h"
29 #include "tests/framework/Macros.h"
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace validation
36 {
37 TEST_SUITE(UNIT)
TEST_SUITE(SafeIntegerOps)38 TEST_SUITE(SafeIntegerOps)
39 
40 TEST_CASE(IntegerOverflowAdd, framework::DatasetMode::ALL)
41 {
42     int32_t val_a  = 0x7FFFFFFF;
43     int32_t val_b  = 0xFF;
44     int32_t result = utils::math::safe_integer_add(val_a, val_b);
45 
46     // Check overflow
47     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS);
48 
49     val_a  = 0x8000FC24;
50     val_b  = 0x80000024;
51     result = utils::math::safe_integer_add(val_a, val_b);
52 
53     // Check underflow
54     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
55 }
56 
TEST_CASE(IntegerOverflowSub,framework::DatasetMode::ALL)57 TEST_CASE(IntegerOverflowSub, framework::DatasetMode::ALL)
58 {
59     int32_t val_a  = 0x7FFFFFFF;
60     int32_t val_b  = 0x8000FC24;
61     int32_t result = utils::math::safe_integer_sub(val_a, val_b);
62 
63     // Check overflow
64     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS);
65 
66     val_a  = 0x80000024;
67     val_b  = 0x7FFFFFFF;
68     result = utils::math::safe_integer_sub(val_a, val_b);
69 
70     // Check underflow
71     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
72 }
73 
TEST_CASE(IntegerOverflowMul,framework::DatasetMode::ALL)74 TEST_CASE(IntegerOverflowMul, framework::DatasetMode::ALL)
75 {
76     int32_t val_a  = 0xFFFFFFFF;
77     int32_t val_b  = 0x80000000;
78     int32_t result = utils::math::safe_integer_mul(val_a, val_b);
79 
80     // Check overflow with -1
81     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
82 
83     val_a  = 0x80000000;
84     val_b  = 0xFFFFFFFF;
85     result = utils::math::safe_integer_mul(val_a, val_b);
86 
87     // Check overflow with -1
88     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
89 
90     // Check overflow
91     val_a  = 0x7000FC24;
92     val_b  = 0x70000024;
93     result = utils::math::safe_integer_mul(val_a, val_b);
94     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS);
95 
96     // Check underflow
97     val_a  = 0x7000FC24;
98     val_b  = 0xF0000024;
99     result = utils::math::safe_integer_mul(val_a, val_b);
100     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
101 }
102 
TEST_CASE(IntegerOverflowDiv,framework::DatasetMode::ALL)103 TEST_CASE(IntegerOverflowDiv, framework::DatasetMode::ALL)
104 {
105     int32_t val_a  = std::numeric_limits<int32_t>::min();
106     int32_t val_b  = 0xFFFFFFFF;
107     int32_t result = utils::math::safe_integer_div(val_a, val_b);
108 
109     // Check overflow
110     ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS);
111 }
112 
113 TEST_SUITE_END() // SafeIntegerOps
114 TEST_SUITE_END() // UNIT
115 } // namespace validation
116 } // namespace test
117 } // namespace arm_compute
118