Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 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" |
Michalis Spyrou | 75d4733 | 2019-11-05 17:46:49 +0000 | [diff] [blame] | 26 | #include "tests/AssetsLibrary.h" |
| 27 | #include "tests/Globals.h" |
| 28 | #include "tests/Utils.h" |
| 29 | #include "tests/framework/Asserts.h" |
| 30 | #include "tests/framework/Macros.h" |
| 31 | |
| 32 | namespace arm_compute |
| 33 | { |
| 34 | namespace test |
| 35 | { |
| 36 | namespace validation |
| 37 | { |
| 38 | TEST_SUITE(UNIT) |
| 39 | TEST_SUITE(SafeIntegerOps) |
| 40 | |
| 41 | TEST_CASE(IntegerOverflowAdd, framework::DatasetMode::ALL) |
| 42 | { |
| 43 | int32_t val_a = 0x7FFFFFFF; |
| 44 | int32_t val_b = 0xFF; |
| 45 | int32_t result = utils::math::safe_integer_add(val_a, val_b); |
| 46 | |
| 47 | // Check overflow |
| 48 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS); |
| 49 | |
| 50 | val_a = 0x8000FC24; |
| 51 | val_b = 0x80000024; |
| 52 | result = utils::math::safe_integer_add(val_a, val_b); |
| 53 | |
| 54 | // Check underflow |
| 55 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 56 | } |
| 57 | |
| 58 | TEST_CASE(IntegerOverflowSub, framework::DatasetMode::ALL) |
| 59 | { |
| 60 | int32_t val_a = 0x7FFFFFFF; |
| 61 | int32_t val_b = 0x8000FC24; |
| 62 | int32_t result = utils::math::safe_integer_sub(val_a, val_b); |
| 63 | |
| 64 | // Check overflow |
| 65 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS); |
| 66 | |
| 67 | val_a = 0x80000024; |
| 68 | val_b = 0x7FFFFFFF; |
| 69 | result = utils::math::safe_integer_sub(val_a, val_b); |
| 70 | |
| 71 | // Check underflow |
| 72 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 73 | } |
| 74 | |
| 75 | TEST_CASE(IntegerOverflowMul, framework::DatasetMode::ALL) |
| 76 | { |
| 77 | int32_t val_a = 0xFFFFFFFF; |
| 78 | int32_t val_b = 0x80000000; |
| 79 | int32_t result = utils::math::safe_integer_mul(val_a, val_b); |
| 80 | |
| 81 | // Check overflow with -1 |
| 82 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 83 | |
| 84 | val_a = 0x80000000; |
| 85 | val_b = 0xFFFFFFFF; |
| 86 | result = utils::math::safe_integer_mul(val_a, val_b); |
| 87 | |
| 88 | // Check overflow with -1 |
| 89 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 90 | |
| 91 | // Check overflow |
| 92 | val_a = 0x7000FC24; |
| 93 | val_b = 0x70000024; |
| 94 | result = utils::math::safe_integer_mul(val_a, val_b); |
| 95 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::max(), framework::LogLevel::ERRORS); |
| 96 | |
| 97 | // Check underflow |
| 98 | val_a = 0x7000FC24; |
| 99 | val_b = 0xF0000024; |
| 100 | result = utils::math::safe_integer_mul(val_a, val_b); |
| 101 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 102 | } |
| 103 | |
| 104 | TEST_CASE(IntegerOverflowDiv, framework::DatasetMode::ALL) |
| 105 | { |
| 106 | int32_t val_a = std::numeric_limits<int32_t>::min(); |
| 107 | int32_t val_b = 0xFFFFFFFF; |
| 108 | int32_t result = utils::math::safe_integer_div(val_a, val_b); |
| 109 | |
| 110 | // Check overflow |
| 111 | ARM_COMPUTE_EXPECT(result == std::numeric_limits<int32_t>::min(), framework::LogLevel::ERRORS); |
| 112 | } |
| 113 | |
| 114 | TEST_SUITE_END() // SafeIntegerOps |
| 115 | TEST_SUITE_END() // UNIT |
| 116 | } // namespace validation |
| 117 | } // namespace test |
| 118 | } // namespace arm_compute |