Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [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/NEON/kernels/NEMinMaxLocationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/misc/Utility.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 37 | #include <algorithm> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | #include <arm_neon.h> |
| 39 | #include <climits> |
| 40 | #include <cstddef> |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | NEMinMaxKernel::NEMinMaxKernel() |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 45 | : _func(), _input(nullptr), _min(), _max(), _mtx() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 49 | void NEMinMaxKernel::configure(const IImage *input, void *min, void *max) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | { |
| 51 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_ERROR_ON(nullptr == min); |
| 54 | ARM_COMPUTE_ERROR_ON(nullptr == max); |
| 55 | |
| 56 | _input = input; |
| 57 | _min = min; |
| 58 | _max = max; |
| 59 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 60 | switch(_input->info()->data_type()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 62 | case DataType::U8: |
| 63 | _func = &NEMinMaxKernel::minmax_U8; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | break; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 65 | case DataType::S16: |
| 66 | _func = &NEMinMaxKernel::minmax_S16; |
| 67 | break; |
| 68 | case DataType::F32: |
| 69 | _func = &NEMinMaxKernel::minmax_F32; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | break; |
| 71 | default: |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | break; |
| 74 | } |
| 75 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | // Configure kernel window |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 77 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 79 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | |
| 81 | INEKernel::configure(win); |
| 82 | } |
| 83 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 84 | void NEMinMaxKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 86 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 88 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 89 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 90 | |
| 91 | (this->*_func)(window); |
| 92 | } |
| 93 | |
| 94 | void NEMinMaxKernel::reset() |
| 95 | { |
| 96 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 97 | switch(_input->info()->data_type()) |
| 98 | { |
| 99 | case DataType::U8: |
| 100 | *static_cast<int32_t *>(_min) = UCHAR_MAX; |
| 101 | *static_cast<int32_t *>(_max) = 0; |
| 102 | break; |
| 103 | case DataType::S16: |
| 104 | *static_cast<int32_t *>(_min) = SHRT_MAX; |
| 105 | *static_cast<int32_t *>(_max) = SHRT_MIN; |
| 106 | break; |
| 107 | case DataType::F32: |
| 108 | *static_cast<float *>(_min) = std::numeric_limits<float>::max(); |
| 109 | *static_cast<float *>(_max) = std::numeric_limits<float>::lowest(); |
| 110 | break; |
| 111 | default: |
| 112 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 113 | break; |
| 114 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | template <typename T> |
| 118 | void NEMinMaxKernel::update_min_max(const T min, const T max) |
| 119 | { |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 120 | std::lock_guard<arm_compute::Mutex> lock(_mtx); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 122 | using type = typename std::conditional<std::is_same<T, float>::value, float, int32_t>::type; |
| 123 | |
| 124 | auto min_ptr = static_cast<type *>(_min); |
| 125 | auto max_ptr = static_cast<type *>(_max); |
| 126 | |
| 127 | if(min < *min_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 129 | *min_ptr = min; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 132 | if(max > *max_ptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 134 | *max_ptr = max; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 138 | void NEMinMaxKernel::minmax_U8(Window win) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | { |
| 140 | uint8x8_t carry_min = vdup_n_u8(UCHAR_MAX); |
| 141 | uint8x8_t carry_max = vdup_n_u8(0); |
| 142 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 143 | uint8_t carry_max_scalar = 0; |
| 144 | uint8_t carry_min_scalar = UCHAR_MAX; |
| 145 | |
| 146 | const int x_start = win.x().start(); |
| 147 | const int x_end = win.x().end(); |
| 148 | |
| 149 | // Handle X dimension manually to split into two loops |
| 150 | // First one will use vector operations, second one processes the left over pixels |
| 151 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 152 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | Iterator input(_input, win); |
| 154 | |
| 155 | execute_window_loop(win, [&](const Coordinates & id) |
| 156 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 157 | int x = x_start; |
| 158 | |
| 159 | // Vector loop |
| 160 | for(; x <= x_end - 16; x += 16) |
| 161 | { |
| 162 | const uint8x16_t pixels = vld1q_u8(input.ptr() + x); |
| 163 | const uint8x8_t tmp_min = vmin_u8(vget_high_u8(pixels), vget_low_u8(pixels)); |
| 164 | const uint8x8_t tmp_max = vmax_u8(vget_high_u8(pixels), vget_low_u8(pixels)); |
| 165 | carry_min = vmin_u8(tmp_min, carry_min); |
| 166 | carry_max = vmax_u8(tmp_max, carry_max); |
| 167 | } |
| 168 | |
| 169 | // Process leftover pixels |
| 170 | for(; x < x_end; ++x) |
| 171 | { |
| 172 | const uint8_t pixel = input.ptr()[x]; |
| 173 | carry_min_scalar = std::min(pixel, carry_min_scalar); |
| 174 | carry_max_scalar = std::max(pixel, carry_max_scalar); |
| 175 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | }, |
| 177 | input); |
| 178 | |
| 179 | // Reduce result |
| 180 | carry_min = vpmin_u8(carry_min, carry_min); |
| 181 | carry_max = vpmax_u8(carry_max, carry_max); |
| 182 | carry_min = vpmin_u8(carry_min, carry_min); |
| 183 | carry_max = vpmax_u8(carry_max, carry_max); |
| 184 | carry_min = vpmin_u8(carry_min, carry_min); |
| 185 | carry_max = vpmax_u8(carry_max, carry_max); |
| 186 | |
| 187 | // Extract max/min values |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 188 | const uint8_t min_i = std::min(vget_lane_u8(carry_min, 0), carry_min_scalar); |
| 189 | const uint8_t max_i = std::max(vget_lane_u8(carry_max, 0), carry_max_scalar); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | |
| 191 | // Perform reduction of local min/max values |
| 192 | update_min_max(min_i, max_i); |
| 193 | } |
| 194 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 195 | void NEMinMaxKernel::minmax_S16(Window win) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | { |
| 197 | int16x4_t carry_min = vdup_n_s16(SHRT_MAX); |
| 198 | int16x4_t carry_max = vdup_n_s16(SHRT_MIN); |
| 199 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 200 | int16_t carry_max_scalar = SHRT_MIN; |
| 201 | int16_t carry_min_scalar = SHRT_MAX; |
| 202 | |
| 203 | const int x_start = win.x().start(); |
| 204 | const int x_end = win.x().end(); |
| 205 | |
| 206 | // Handle X dimension manually to split into two loops |
| 207 | // First one will use vector operations, second one processes the left over pixels |
| 208 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 209 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 210 | Iterator input(_input, win); |
| 211 | |
| 212 | execute_window_loop(win, [&](const Coordinates & id) |
| 213 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 214 | int x = x_start; |
Kohei Takahashi | cedb78f | 2018-08-23 10:23:52 +0900 | [diff] [blame] | 215 | const auto in_ptr = reinterpret_cast<const int16_t *>(input.ptr()); |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 216 | |
| 217 | // Vector loop |
| 218 | for(; x <= x_end - 16; x += 16) |
| 219 | { |
| 220 | const int16x8x2_t pixels = vld2q_s16(in_ptr + x); |
| 221 | const int16x8_t tmp_min1 = vminq_s16(pixels.val[0], pixels.val[1]); |
| 222 | const int16x8_t tmp_max1 = vmaxq_s16(pixels.val[0], pixels.val[1]); |
| 223 | const int16x4_t tmp_min2 = vmin_s16(vget_high_s16(tmp_min1), vget_low_s16(tmp_min1)); |
| 224 | const int16x4_t tmp_max2 = vmax_s16(vget_high_s16(tmp_max1), vget_low_s16(tmp_max1)); |
| 225 | carry_min = vmin_s16(tmp_min2, carry_min); |
| 226 | carry_max = vmax_s16(tmp_max2, carry_max); |
| 227 | } |
| 228 | |
| 229 | // Process leftover pixels |
| 230 | for(; x < x_end; ++x) |
| 231 | { |
| 232 | const int16_t pixel = in_ptr[x]; |
| 233 | carry_min_scalar = std::min(pixel, carry_min_scalar); |
| 234 | carry_max_scalar = std::max(pixel, carry_max_scalar); |
| 235 | } |
| 236 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | }, |
| 238 | input); |
| 239 | |
| 240 | // Reduce result |
| 241 | carry_min = vpmin_s16(carry_min, carry_min); |
| 242 | carry_max = vpmax_s16(carry_max, carry_max); |
| 243 | carry_min = vpmin_s16(carry_min, carry_min); |
| 244 | carry_max = vpmax_s16(carry_max, carry_max); |
| 245 | |
| 246 | // Extract max/min values |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 247 | const int16_t min_i = std::min(vget_lane_s16(carry_min, 0), carry_min_scalar); |
| 248 | const int16_t max_i = std::max(vget_lane_s16(carry_max, 0), carry_max_scalar); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 249 | |
| 250 | // Perform reduction of local min/max values |
| 251 | update_min_max(min_i, max_i); |
| 252 | } |
| 253 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 254 | void NEMinMaxKernel::minmax_F32(Window win) |
| 255 | { |
| 256 | float32x2_t carry_min = vdup_n_f32(std::numeric_limits<float>::max()); |
| 257 | float32x2_t carry_max = vdup_n_f32(std::numeric_limits<float>::lowest()); |
| 258 | |
| 259 | float carry_min_scalar = std::numeric_limits<float>::max(); |
| 260 | float carry_max_scalar = std::numeric_limits<float>::lowest(); |
| 261 | |
| 262 | const int x_start = win.x().start(); |
| 263 | const int x_end = win.x().end(); |
| 264 | |
| 265 | // Handle X dimension manually to split into two loops |
| 266 | // First one will use vector operations, second one processes the left over pixels |
| 267 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 268 | |
| 269 | Iterator input(_input, win); |
| 270 | |
| 271 | execute_window_loop(win, [&](const Coordinates & id) |
| 272 | { |
| 273 | int x = x_start; |
Kohei Takahashi | cedb78f | 2018-08-23 10:23:52 +0900 | [diff] [blame] | 274 | const auto in_ptr = reinterpret_cast<const float *>(input.ptr()); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 275 | |
| 276 | // Vector loop |
| 277 | for(; x <= x_end - 8; x += 8) |
| 278 | { |
| 279 | const float32x4x2_t pixels = vld2q_f32(in_ptr + x); |
| 280 | const float32x4_t tmp_min1 = vminq_f32(pixels.val[0], pixels.val[1]); |
| 281 | const float32x4_t tmp_max1 = vmaxq_f32(pixels.val[0], pixels.val[1]); |
| 282 | const float32x2_t tmp_min2 = vmin_f32(vget_high_f32(tmp_min1), vget_low_f32(tmp_min1)); |
| 283 | const float32x2_t tmp_max2 = vmax_f32(vget_high_f32(tmp_max1), vget_low_f32(tmp_max1)); |
| 284 | carry_min = vmin_f32(tmp_min2, carry_min); |
| 285 | carry_max = vmax_f32(tmp_max2, carry_max); |
| 286 | } |
| 287 | |
| 288 | // Process leftover pixels |
| 289 | for(; x < x_end; ++x) |
| 290 | { |
| 291 | const float pixel = in_ptr[x]; |
| 292 | carry_min_scalar = std::min(pixel, carry_min_scalar); |
| 293 | carry_max_scalar = std::max(pixel, carry_max_scalar); |
| 294 | } |
| 295 | |
| 296 | }, |
| 297 | input); |
| 298 | |
| 299 | // Reduce result |
| 300 | carry_min = vpmin_f32(carry_min, carry_min); |
| 301 | carry_max = vpmax_f32(carry_max, carry_max); |
| 302 | carry_min = vpmin_f32(carry_min, carry_min); |
| 303 | carry_max = vpmax_f32(carry_max, carry_max); |
| 304 | |
| 305 | // Extract max/min values |
| 306 | const float min_i = std::min(vget_lane_f32(carry_min, 0), carry_min_scalar); |
| 307 | const float max_i = std::max(vget_lane_f32(carry_max, 0), carry_max_scalar); |
| 308 | |
| 309 | // Perform reduction of local min/max values |
| 310 | update_min_max(min_i, max_i); |
| 311 | } |
| 312 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 313 | NEMinMaxLocationKernel::NEMinMaxLocationKernel() |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 314 | : _func(nullptr), _input(nullptr), _min(nullptr), _max(nullptr), _min_count(nullptr), _max_count(nullptr), _min_loc(nullptr), _max_loc(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 315 | { |
| 316 | } |
| 317 | |
| 318 | bool NEMinMaxLocationKernel::is_parallelisable() const |
| 319 | { |
| 320 | return false; |
| 321 | } |
| 322 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 323 | template <class T, std::size_t... N> |
| 324 | struct NEMinMaxLocationKernel::create_func_table<T, utility::index_sequence<N...>> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 325 | { |
| 326 | static const NEMinMaxLocationKernel::MinMaxLocFunction func_table[sizeof...(N)]; |
| 327 | }; |
| 328 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 329 | template <class T, std::size_t... N> |
| 330 | const NEMinMaxLocationKernel::MinMaxLocFunction NEMinMaxLocationKernel::create_func_table<T, utility::index_sequence<N...>>::func_table[sizeof...(N)] = |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 331 | { |
| 332 | &NEMinMaxLocationKernel::minmax_loc<T, bool(N & 8), bool(N & 4), bool(N & 2), bool(N & 1)>... |
| 333 | }; |
| 334 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 335 | void NEMinMaxLocationKernel::configure(const IImage *input, void *min, void *max, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 336 | ICoordinates2DArray *min_loc, ICoordinates2DArray *max_loc, |
| 337 | uint32_t *min_count, uint32_t *max_count) |
| 338 | { |
| 339 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 340 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 341 | ARM_COMPUTE_ERROR_ON(nullptr == min); |
| 342 | ARM_COMPUTE_ERROR_ON(nullptr == max); |
| 343 | |
| 344 | _input = input; |
| 345 | _min = min; |
| 346 | _max = max; |
| 347 | _min_count = min_count; |
| 348 | _max_count = max_count; |
| 349 | _min_loc = min_loc; |
| 350 | _max_loc = max_loc; |
| 351 | |
| 352 | unsigned int count_min = (nullptr != min_count ? 1 : 0); |
| 353 | unsigned int count_max = (nullptr != max_count ? 1 : 0); |
| 354 | unsigned int loc_min = (nullptr != min_loc ? 1 : 0); |
| 355 | unsigned int loc_max = (nullptr != max_loc ? 1 : 0); |
| 356 | |
| 357 | unsigned int table_idx = (count_min << 3) | (count_max << 2) | (loc_min << 1) | loc_max; |
| 358 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 359 | switch(input->info()->data_type()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 360 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 361 | case DataType::U8: |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 362 | _func = create_func_table<uint8_t, utility::index_sequence_t<16>>::func_table[table_idx]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 363 | break; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 364 | case DataType::S16: |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 365 | _func = create_func_table<int16_t, utility::index_sequence_t<16>>::func_table[table_idx]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 366 | break; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 367 | case DataType::F32: |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 368 | _func = create_func_table<float, utility::index_sequence_t<16>>::func_table[table_idx]; |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 369 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | default: |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 371 | ARM_COMPUTE_ERROR("Unsupported data type"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 375 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 376 | |
| 377 | // Configure kernel window |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 378 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 379 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 380 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 381 | |
| 382 | INEKernel::configure(win); |
| 383 | } |
| 384 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 385 | void NEMinMaxLocationKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 386 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 387 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 388 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 389 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 390 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 391 | |
| 392 | (this->*_func)(window); |
| 393 | } |
| 394 | |
| 395 | template <class T, bool count_min, bool count_max, bool loc_min, bool loc_max> |
| 396 | void NEMinMaxLocationKernel::minmax_loc(const Window &win) |
| 397 | { |
| 398 | if(count_min || count_max || loc_min || loc_max) |
| 399 | { |
| 400 | Iterator input(_input, win); |
| 401 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 402 | size_t min_count = 0; |
| 403 | size_t max_count = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 404 | |
| 405 | // Clear min location array |
| 406 | if(loc_min) |
| 407 | { |
| 408 | _min_loc->clear(); |
| 409 | } |
| 410 | |
| 411 | // Clear max location array |
| 412 | if(loc_max) |
| 413 | { |
| 414 | _max_loc->clear(); |
| 415 | } |
| 416 | |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 417 | using type = typename std::conditional<std::is_same<T, float>::value, float, int32_t>::type; |
| 418 | |
| 419 | auto min_ptr = static_cast<type *>(_min); |
| 420 | auto max_ptr = static_cast<type *>(_max); |
| 421 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 422 | execute_window_loop(win, [&](const Coordinates & id) |
| 423 | { |
| 424 | auto in_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 425 | int32_t idx = id.x(); |
| 426 | int32_t idy = id.y(); |
| 427 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 428 | const T pixel = *in_ptr; |
| 429 | Coordinates2D p{ idx, idy }; |
| 430 | |
| 431 | if(count_min || loc_min) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 432 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 433 | if(*min_ptr == pixel) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 434 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 435 | if(count_min) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 436 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 437 | ++min_count; |
| 438 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 439 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 440 | if(loc_min) |
| 441 | { |
| 442 | _min_loc->push_back(p); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 445 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 446 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 447 | if(count_max || loc_max) |
| 448 | { |
Michele Di Giorgio | ef4b4ae | 2017-07-04 17:19:43 +0100 | [diff] [blame] | 449 | if(*max_ptr == pixel) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 450 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 451 | if(count_max) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 452 | { |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 453 | ++max_count; |
| 454 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 455 | |
steniu01 | 4c2938e | 2017-06-19 15:44:45 +0100 | [diff] [blame] | 456 | if(loc_max) |
| 457 | { |
| 458 | _max_loc->push_back(p); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
| 462 | }, |
| 463 | input); |
| 464 | |
| 465 | if(count_min) |
| 466 | { |
| 467 | *_min_count = min_count; |
| 468 | } |
| 469 | |
| 470 | if(count_max) |
| 471 | { |
| 472 | *_max_count = max_count; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } // namespace arm_compute |