Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +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 | */ |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 24 | #ifndef SRC_CORE_NEON_KERNELS_SCALE_LIST_H |
| 25 | #define SRC_CORE_NEON_KERNELS_SCALE_LIST_H |
| 26 | |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensorPack.h" |
| 29 | #include "arm_compute/core/Window.h" |
| 30 | #include "src/core/NEON/NEMath.h" |
| 31 | #include "src/core/NEON/wrapper/wrapper.h" |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 32 | #include "src/core/helpers/ScaleHelpers.h" |
| 33 | #include "src/core/utils/ScaleUtils.h" |
| 34 | #include "support/Rounding.h" |
| 35 | |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 36 | namespace arm_compute |
| 37 | { |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 38 | namespace cpu |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 39 | { |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 40 | #define DECLARE_SCALE_KERNEL(func_name) \ |
| 41 | void func_name(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, \ |
| 42 | InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, \ |
| 43 | bool align_corners, const Window &window) |
| 44 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 45 | DECLARE_SCALE_KERNEL(s16_neon_scale); |
| 46 | DECLARE_SCALE_KERNEL(u8_neon_scale); |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 47 | DECLARE_SCALE_KERNEL(qasymm8_neon_scale); |
| 48 | DECLARE_SCALE_KERNEL(qasymm8_signed_neon_scale); |
| 49 | |
| 50 | #undef DECLARE_SCALE_KERNEL |
| 51 | |
| 52 | template <typename T> |
| 53 | void nearest_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, float sampling_offset, |
| 54 | bool align_corners, const Window &window) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 55 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_UNUSED(offsets); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 57 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 58 | // Compute the ratio between source and destination dimensions |
| 59 | const float scale_x = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), align_corners); |
| 60 | const float scale_y = scale_utils::calculate_resize_ratio(src->info()->dimension(2), dst->info()->dimension(2), align_corners); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 61 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 62 | const int in_stride_y = src->info()->strides_in_bytes()[1]; |
| 63 | const int in_stride_z = src->info()->strides_in_bytes()[2]; |
| 64 | const int in_stride_w = src->info()->strides_in_bytes()[3]; |
| 65 | const int out_stride_y = dst->info()->strides_in_bytes()[1]; |
| 66 | const int out_stride_z = dst->info()->strides_in_bytes()[2]; |
| 67 | const int out_stride_w = dst->info()->strides_in_bytes()[3]; |
| 68 | const int out_dim_ch = dst->info()->dimension(0); |
| 69 | const int step_cout = 16 / sizeof(T); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 70 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 71 | Window window_execution = window; |
| 72 | window_execution.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 73 | Window win_in_out(window); |
| 74 | win_in_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 75 | win_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 76 | Iterator in(src, win_in_out); |
| 77 | Iterator out(dst, win_in_out); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 78 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 79 | const int xo_start = window_execution.y().start(); |
| 80 | const int xo_end = window_execution.y().end(); |
| 81 | const int xo_step = window_execution.y().step(); |
| 82 | const int yo_start = window_execution.z().start(); |
| 83 | const int yo_end = window_execution.z().end(); |
| 84 | const int yo_step = window_execution.z().step(); |
| 85 | const int bo_start = window_execution[3].start(); |
| 86 | const int bo_end = window_execution[3].end(); |
| 87 | const int bo_step = window_execution[3].step(); |
| 88 | |
| 89 | for(int bo = bo_start; bo < bo_end; bo += bo_step) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 90 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 91 | const uint8_t *in_ptr_base = in.ptr() + bo * in_stride_w; |
| 92 | uint8_t *out_ptr_base = out.ptr() + bo * out_stride_w; |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 93 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 94 | for(int yo = yo_start; yo < yo_end; yo += yo_step) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 95 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 96 | // Floating-point coordinate |
| 97 | float yi_f = ((yo + sampling_offset) * scale_y); |
| 98 | int yi = 0; |
| 99 | if(align_corners) |
| 100 | { |
| 101 | yi = utils::rounding::round_half_away_from_zero(yi_f); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | yi = static_cast<int>(std::floor(yi_f)); |
| 106 | } |
| 107 | |
| 108 | for(int xo = xo_start; xo < xo_end; xo += xo_step) |
| 109 | { |
| 110 | // Floating-point coordinate |
| 111 | float xi_f = ((xo + sampling_offset) * scale_x); |
| 112 | int xi = 0; |
| 113 | if(align_corners) |
| 114 | { |
| 115 | xi = utils::rounding::round_half_away_from_zero(xi_f); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | xi = static_cast<int>(std::floor(xi_f)); |
| 120 | } |
| 121 | |
| 122 | const uint8_t *in_ptr = in_ptr_base + xi * in_stride_y + yi * in_stride_z; |
| 123 | uint8_t *out_ptr = out_ptr_base + xo * out_stride_y + yo * out_stride_z; |
| 124 | |
| 125 | int cout = 0; |
| 126 | for(; cout <= (out_dim_ch - step_cout); cout += step_cout) |
| 127 | { |
| 128 | auto out0 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T))); |
| 129 | wrapper::vstore(reinterpret_cast<T *>(out_ptr + cout * sizeof(T)), out0); |
| 130 | } |
| 131 | |
| 132 | for(; cout < out_dim_ch; ++cout) |
| 133 | { |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 134 | auto out0 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T))); |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 135 | *(reinterpret_cast<T *>(out_ptr + cout * sizeof(T))) = out0; |
| 136 | } |
| 137 | } |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 138 | } |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 139 | } |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 142 | template <typename T> |
| 143 | void bilinear_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, |
| 144 | BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, |
| 145 | bool align_corners, const Window &window) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 146 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 147 | ARM_COMPUTE_UNUSED(offsets); |
| 148 | ARM_COMPUTE_UNUSED(dx); |
| 149 | ARM_COMPUTE_UNUSED(dy); |
| 150 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 151 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 152 | // Compute the ratio between source and destination dimensions |
| 153 | const float scale_x = scale_utils::calculate_resize_ratio(src->info()->dimension(1), dst->info()->dimension(1), align_corners); |
| 154 | const float scale_y = scale_utils::calculate_resize_ratio(src->info()->dimension(2), dst->info()->dimension(2), align_corners); |
| 155 | |
| 156 | const int in_stride_y = src->info()->strides_in_bytes()[1]; |
| 157 | const int in_stride_z = src->info()->strides_in_bytes()[2]; |
| 158 | const int in_stride_w = src->info()->strides_in_bytes()[3]; |
| 159 | const int out_stride_y = dst->info()->strides_in_bytes()[1]; |
| 160 | const int out_stride_z = dst->info()->strides_in_bytes()[2]; |
| 161 | const int out_stride_w = dst->info()->strides_in_bytes()[3]; |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 162 | const int in_dim_w = src->info()->dimension(1); |
| 163 | const int in_dim_h = src->info()->dimension(2); |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 164 | const int out_dim_ch = dst->info()->dimension(0); |
| 165 | const int step_cout = 16 / sizeof(T); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 166 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 167 | Window window_execution = window; |
| 168 | window_execution.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 169 | Window win_in_out(window); |
| 170 | win_in_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 171 | win_in_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 172 | Iterator in(src, win_in_out); |
| 173 | Iterator out(dst, win_in_out); |
| 174 | |
| 175 | const int xo_start = window_execution.y().start(); |
| 176 | const int xo_end = window_execution.y().end(); |
| 177 | const int xo_step = window_execution.y().step(); |
| 178 | const int yo_start = window_execution.z().start(); |
| 179 | const int yo_end = window_execution.z().end(); |
| 180 | const int yo_step = window_execution.z().step(); |
| 181 | const int bo_start = window_execution[3].start(); |
| 182 | const int bo_end = window_execution[3].end(); |
| 183 | const int bo_step = window_execution[3].step(); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 184 | |
| 185 | if(border_mode == BorderMode::CONSTANT) |
| 186 | { |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 187 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 188 | using ConstType = typename std::conditional<std::is_same<T, float16_t>::value, half, T>::type; |
| 189 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 190 | using ConstType = T; |
| 191 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 192 | const T const_border_value = static_cast<T>(constant_border_value.get<ConstType>()); |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 193 | |
| 194 | for(int bo = bo_start; bo < bo_end; bo += bo_step) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 195 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 196 | const uint8_t *in_ptr_base = in.ptr() + bo * in_stride_w; |
| 197 | uint8_t *out_ptr_base = out.ptr() + bo * out_stride_w; |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 198 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 199 | for(int yo = yo_start; yo < yo_end; yo += yo_step) |
| 200 | { |
| 201 | // Floating-point coordinate |
| 202 | const float yi_f = ((yo + sampling_offset) * scale_y - sampling_offset); |
| 203 | // Integer coordinate |
| 204 | const auto yi = static_cast<int>(std::floor(yi_f)); |
| 205 | // Weight for the y coordinate |
| 206 | const auto a1 = (yi_f - static_cast<float>(yi)); |
| 207 | const auto b1 = (1.f - a1); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 208 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 209 | for(int xo = xo_start; xo < xo_end; xo += xo_step) |
| 210 | { |
| 211 | // Floating-point coordinate |
| 212 | const float xi_f = ((xo + sampling_offset) * scale_x - sampling_offset); |
| 213 | // Integer coordinate |
| 214 | const auto xi = static_cast<int>(std::floor(xi_f)); |
| 215 | // Weight for the x coordinate |
| 216 | const auto a = (xi_f - static_cast<float>(xi)); |
| 217 | const auto b = (1.f - a); |
| 218 | |
| 219 | const auto s00_s = static_cast<T>(b * b1); |
| 220 | const auto s01_s = static_cast<T>(a * b1); |
| 221 | const auto s10_s = static_cast<T>(b * a1); |
| 222 | const auto s11_s = static_cast<T>(a * a1); |
| 223 | |
| 224 | const uint8_t *in_ptr = in_ptr_base + xi * in_stride_y + yi * in_stride_z; |
| 225 | uint8_t *out_ptr = out_ptr_base + xo * out_stride_y + yo * out_stride_z; |
| 226 | |
| 227 | int cout = 0; |
| 228 | for(; cout <= (out_dim_ch - step_cout); cout += step_cout) |
| 229 | { |
| 230 | auto in00 = wrapper::vdup_n(static_cast<T>(const_border_value), ExactTagType{}); |
| 231 | auto in01 = wrapper::vdup_n(static_cast<T>(const_border_value), ExactTagType{}); |
| 232 | auto in10 = wrapper::vdup_n(static_cast<T>(const_border_value), ExactTagType{}); |
| 233 | auto in11 = wrapper::vdup_n(static_cast<T>(const_border_value), ExactTagType{}); |
| 234 | if((yi >= 0) && (yi < in_dim_h)) |
| 235 | { |
| 236 | if((xi >= 0) && (xi < in_dim_w)) |
| 237 | { |
| 238 | in00 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T))); |
| 239 | } |
| 240 | if(((xi + 1) >= 0) && ((xi + 1) < in_dim_w)) |
| 241 | { |
| 242 | in01 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_y)); |
| 243 | } |
| 244 | } |
| 245 | if(((yi + 1) >= 0) && ((yi + 1) < in_dim_h)) |
| 246 | { |
| 247 | if((xi >= 0) && (xi < in_dim_w)) |
| 248 | { |
| 249 | in10 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_z)); |
| 250 | } |
| 251 | if(((xi + 1) >= 0) && ((xi + 1) < in_dim_w)) |
| 252 | { |
| 253 | in11 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_y + in_stride_z)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | const auto s00 = wrapper::vdup_n(s00_s, ExactTagType{}); |
| 258 | const auto s01 = wrapper::vdup_n(s01_s, ExactTagType{}); |
| 259 | const auto s10 = wrapper::vdup_n(s10_s, ExactTagType{}); |
| 260 | const auto s11 = wrapper::vdup_n(s11_s, ExactTagType{}); |
| 261 | auto out0 = wrapper::vdup_n(static_cast<T>(0), ExactTagType{}); |
| 262 | out0 = wrapper::vmla(out0, in00, s00); |
| 263 | out0 = wrapper::vmla(out0, in01, s01); |
| 264 | out0 = wrapper::vmla(out0, in10, s10); |
| 265 | out0 = wrapper::vmla(out0, in11, s11); |
| 266 | wrapper::vstore(reinterpret_cast<T *>(out_ptr + cout * sizeof(T)), out0); |
| 267 | } |
| 268 | |
| 269 | for(; cout < out_dim_ch; ++cout) |
| 270 | { |
| 271 | auto in00 = static_cast<T>(const_border_value); |
| 272 | auto in01 = static_cast<T>(const_border_value); |
| 273 | auto in10 = static_cast<T>(const_border_value); |
| 274 | auto in11 = static_cast<T>(const_border_value); |
| 275 | if((yi >= 0) && (yi < in_dim_h)) |
| 276 | { |
| 277 | if((xi >= 0) && (xi < in_dim_w)) |
| 278 | { |
| 279 | in00 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T))); |
| 280 | } |
| 281 | if(((xi + 1) >= 0) && ((xi + 1) < in_dim_w)) |
| 282 | { |
| 283 | in01 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_y)); |
| 284 | } |
| 285 | } |
| 286 | if(((yi + 1) >= 0) && ((yi + 1) < in_dim_h)) |
| 287 | { |
| 288 | if((xi >= 0) && (xi < in_dim_w)) |
| 289 | { |
| 290 | in10 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_z)); |
| 291 | } |
| 292 | if(((xi + 1) >= 0) && ((xi + 1) < in_dim_w)) |
| 293 | { |
| 294 | in11 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + in_stride_y + in_stride_z)); |
| 295 | } |
| 296 | } |
| 297 | auto out0 = static_cast<T>(0); |
| 298 | out0 += in00 * s00_s; |
| 299 | out0 += in01 * s01_s; |
| 300 | out0 += in10 * s10_s; |
| 301 | out0 += in11 * s11_s; |
| 302 | *(reinterpret_cast<T *>(out_ptr + cout * sizeof(T))) = out0; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 307 | } |
| 308 | else if(border_mode == BorderMode::REPLICATE) |
| 309 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 310 | for(int bo = bo_start; bo < bo_end; bo += bo_step) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 311 | { |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 312 | const uint8_t *in_ptr = in.ptr() + bo * in_stride_w; |
| 313 | uint8_t *out_ptr = out.ptr() + bo * out_stride_w; |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 314 | |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 315 | for(int yo = yo_start; yo < yo_end; yo += yo_step) |
| 316 | { |
| 317 | // Floating-point coordinate |
| 318 | const float yi_f = ((yo + sampling_offset) * scale_y - sampling_offset); |
| 319 | // Integer coordinate |
| 320 | const auto yi = static_cast<int>(std::floor(yi_f)); |
| 321 | // Weight for the y coordinate |
| 322 | const auto a1 = (yi_f - static_cast<float>(yi)); |
| 323 | const auto b1 = (1.f - a1); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 324 | |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 325 | const int yi0 = utility::clamp<int>(yi, 0, in_dim_h - 1); |
| 326 | const int yi1 = utility::clamp<int>(yi + 1, 0, in_dim_h - 1); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 327 | |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 328 | const int yi0_offset = yi0 * in_stride_z; |
| 329 | const int yi1_offset = yi1 * in_stride_z; |
| 330 | |
| 331 | const int y_offset = yo * out_stride_z; |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 332 | for(int xo = xo_start; xo < xo_end; xo += xo_step) |
| 333 | { |
| 334 | // Floating-point coordinate |
| 335 | const float xi_f = ((xo + sampling_offset) * scale_x - sampling_offset); |
| 336 | // Integer coordinate |
| 337 | const auto xi = static_cast<int>(std::floor(xi_f)); |
| 338 | // Weight for the x coordinate |
| 339 | const auto a = (xi_f - static_cast<float>(xi)); |
| 340 | const auto b = (1.f - a); |
| 341 | |
| 342 | const auto s00_s = static_cast<T>(b * b1); |
| 343 | const auto s01_s = static_cast<T>(a * b1); |
| 344 | const auto s10_s = static_cast<T>(b * a1); |
| 345 | const auto s11_s = static_cast<T>(a * a1); |
| 346 | |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 347 | const auto s00 = wrapper::vdup_n(s00_s, ExactTagType{}); |
| 348 | const auto s01 = wrapper::vdup_n(s01_s, ExactTagType{}); |
| 349 | const auto s10 = wrapper::vdup_n(s10_s, ExactTagType{}); |
| 350 | const auto s11 = wrapper::vdup_n(s11_s, ExactTagType{}); |
| 351 | |
| 352 | const int xi0 = utility::clamp<int>(xi, 0, in_dim_w - 1); |
| 353 | const int xi1 = utility::clamp<int>(xi + 1, 0, in_dim_w - 1); |
| 354 | |
| 355 | const int xi0_offset = xi0 * in_stride_y; |
| 356 | const int xi1_offset = xi1 * in_stride_y; |
| 357 | |
| 358 | const int offset = xo * out_stride_y + y_offset; |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 359 | |
| 360 | int cout = 0; |
| 361 | for(; cout <= (out_dim_ch - step_cout); cout += step_cout) |
| 362 | { |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 363 | auto in00 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi0_offset + yi0_offset)); |
| 364 | auto in01 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi1_offset + yi0_offset)); |
| 365 | auto in10 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi0_offset + yi1_offset)); |
| 366 | auto in11 = wrapper::vloadq(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi1_offset + yi1_offset)); |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 367 | |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 368 | auto out0 = wrapper::vmul(in00, s00); |
| 369 | out0 = wrapper::vmla(out0, in01, s01); |
| 370 | out0 = wrapper::vmla(out0, in10, s10); |
| 371 | out0 = wrapper::vmla(out0, in11, s11); |
| 372 | wrapper::vstore(reinterpret_cast<T *>(out_ptr + offset + cout * sizeof(T)), out0); |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | for(; cout < out_dim_ch; ++cout) |
| 376 | { |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 377 | T in00 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi0_offset + yi0_offset)); |
| 378 | T in01 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi1_offset + yi0_offset)); |
| 379 | T in10 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi0_offset + yi1_offset)); |
| 380 | T in11 = *(reinterpret_cast<const T *>(in_ptr + cout * sizeof(T) + xi1_offset + yi1_offset)); |
| 381 | |
| 382 | T out0 = in00 * s00_s; |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 383 | out0 += in01 * s01_s; |
| 384 | out0 += in10 * s10_s; |
| 385 | out0 += in11 * s11_s; |
Gunes Bayir | 0eed305 | 2022-09-04 21:00:10 +0100 | [diff] [blame] | 386 | *(reinterpret_cast<T *>(out_ptr + offset + cout * sizeof(T))) = out0; |
Gian Marco Iodice | 8b8405a | 2021-10-01 17:48:02 +0100 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 391 | } |
| 392 | else |
| 393 | { |
| 394 | ARM_COMPUTE_ERROR("Not implemented"); |
| 395 | } |
| 396 | } |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 397 | |
| 398 | template <typename T> |
| 399 | void common_neon_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy, |
| 400 | InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset, |
| 401 | bool align_corners, const Window &window) |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 402 | { |
| 403 | if(policy == InterpolationPolicy::BILINEAR) |
| 404 | { |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 405 | bilinear_neon_scale<T>(src, dst, offsets, dx, dy, border_mode, constant_border_value, sampling_offset, align_corners, window); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 406 | } |
| 407 | else if(policy == InterpolationPolicy::NEAREST_NEIGHBOR) |
| 408 | { |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 409 | nearest_neon_scale<T>(src, dst, offsets, sampling_offset, align_corners, window); |
Sheri Zhang | 23adc4c | 2021-01-05 12:48:45 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } // namespace cpu |
Sheri Zhang | 360f576 | 2021-01-20 12:20:20 +0000 | [diff] [blame] | 413 | } // namespace arm_compute |
| 414 | |
| 415 | #endif /* SRC_CORE_NEON_KERNELS_SCALE_LIST_H */ |