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