Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +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 "Comparisons.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | #include "tests/validation/Helpers.h" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace validation |
| 34 | { |
| 35 | namespace reference |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | template <typename T> |
| 40 | uint8_t compare_op(ComparisonOperation op, T src1, T src2) |
| 41 | { |
| 42 | uint8_t result = 0; |
| 43 | switch(op) |
| 44 | { |
| 45 | case ComparisonOperation::Equal: |
| 46 | result = static_cast<uint8_t>(src1 == src2); |
| 47 | break; |
| 48 | case ComparisonOperation::NotEqual: |
| 49 | result = static_cast<uint8_t>(src1 != src2); |
| 50 | break; |
| 51 | case ComparisonOperation::GreaterEqual: |
| 52 | result = static_cast<uint8_t>(src1 >= src2); |
| 53 | break; |
| 54 | case ComparisonOperation::Greater: |
| 55 | result = static_cast<uint8_t>(src1 > src2); |
| 56 | break; |
| 57 | case ComparisonOperation::LessEqual: |
| 58 | result = static_cast<uint8_t>(src1 <= src2); |
| 59 | break; |
| 60 | case ComparisonOperation::Less: |
| 61 | result = static_cast<uint8_t>(src1 < src2); |
| 62 | break; |
| 63 | default: |
| 64 | ARM_COMPUTE_ERROR("Unsupported operation"); |
| 65 | } |
| 66 | return (result != 0) ? 255 : 0; |
| 67 | } |
| 68 | |
| 69 | template <size_t dim> |
| 70 | struct BroadcastUnroll |
| 71 | { |
| 72 | template <typename T> |
| 73 | static void unroll(ComparisonOperation op, |
| 74 | const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<uint8_t> &dst, |
| 75 | Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 76 | { |
| 77 | const bool src1_is_broadcast = (src1.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 78 | const bool src2_is_broadcast = (src2.shape()[dim - 1] != dst.shape()[dim - 1]); |
| 79 | |
| 80 | id_src1.set(dim - 1, 0); |
| 81 | id_src2.set(dim - 1, 0); |
| 82 | id_dst.set(dim - 1, 0); |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 83 | #if defined(_OPENMP) |
| 84 | #pragma omp parallel for |
| 85 | #endif /* _OPENMP */ |
| 86 | for(size_t i = 0; i < dst.shape()[dim - 1]; ++i) |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 87 | { |
| 88 | BroadcastUnroll < dim - 1 >::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst); |
| 89 | |
| 90 | id_src1[dim - 1] += !src1_is_broadcast; |
| 91 | id_src2[dim - 1] += !src2_is_broadcast; |
Michalis Spyrou | d1d7722 | 2020-04-08 14:10:15 +0100 | [diff] [blame] | 92 | ++id_dst[dim - 1]; |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | template <> |
| 98 | struct BroadcastUnroll<0> |
| 99 | { |
| 100 | template <typename T> |
| 101 | static void unroll(ComparisonOperation op, |
| 102 | const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, SimpleTensor<uint8_t> &dst, |
| 103 | Coordinates &id_src1, Coordinates &id_src2, Coordinates &id_dst) |
| 104 | { |
| 105 | dst[coord2index(dst.shape(), id_dst)] = compare_op(op, src1[coord2index(src1.shape(), id_src1)], src2[coord2index(src2.shape(), id_src2)]); |
| 106 | } |
| 107 | }; |
| 108 | } // namespace |
| 109 | |
| 110 | template <typename T> |
| 111 | SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<T> &src1, const SimpleTensor<T> &src2) |
| 112 | { |
| 113 | SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8); |
| 114 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 115 | Coordinates id_src1{}; |
| 116 | Coordinates id_src2{}; |
| 117 | Coordinates id_dst{}; |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 118 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst); |
| 119 | return dst; |
| 120 | } |
| 121 | |
| 122 | template <> |
| 123 | SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<uint8_t> &src1, const SimpleTensor<uint8_t> &src2) |
| 124 | { |
| 125 | SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8); |
| 126 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 127 | Coordinates id_src1{}; |
| 128 | Coordinates id_src2{}; |
| 129 | Coordinates id_dst{}; |
| 130 | |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 131 | if(src1.data_type() == DataType::QASYMM8) |
| 132 | { |
| 133 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 134 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 135 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | // DataType::U8 |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 140 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst); |
| 141 | } |
| 142 | return dst; |
| 143 | } |
| 144 | |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 145 | template <> |
| 146 | SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<int8_t> &src1, const SimpleTensor<int8_t> &src2) |
| 147 | { |
| 148 | SimpleTensor<uint8_t> dst(TensorShape::broadcast_shape(src1.shape(), src2.shape()), DataType::U8); |
| 149 | |
| 150 | Coordinates id_src1{}; |
| 151 | Coordinates id_src2{}; |
| 152 | Coordinates id_dst{}; |
| 153 | |
| 154 | if(src1.data_type() == DataType::QASYMM8_SIGNED) |
| 155 | { |
| 156 | SimpleTensor<float> src1_tmp = convert_from_asymmetric(src1); |
| 157 | SimpleTensor<float> src2_tmp = convert_from_asymmetric(src2); |
| 158 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1_tmp, src2_tmp, dst, id_src1, id_src2, id_dst); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | // DataType::U8 |
| 163 | BroadcastUnroll<Coordinates::num_max_dimensions>::unroll(op, src1, src2, dst, id_src1, id_src2, id_dst); |
| 164 | } |
| 165 | return dst; |
| 166 | } |
| 167 | |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 168 | template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<half> &src1, const SimpleTensor<half> &src2); |
| 169 | template SimpleTensor<uint8_t> compare(ComparisonOperation op, const SimpleTensor<float> &src1, const SimpleTensor<float> &src2); |
| 170 | |
| 171 | } // namespace reference |
| 172 | } // namespace validation |
| 173 | } // namespace test |
| 174 | } // namespace arm_compute |