| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2024 - 2025 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
| 2 | // | ||
| 3 | // SPDX-License-Identifier: Apache-2.0 | ||
| 4 | |||
| 5 | #include <cassert> | ||
| 6 | |||
| 7 | #include "kleidicv/ctypes.h" | ||
| 8 | #include "kleidicv/neon.h" | ||
| 9 | #include "kleidicv/transform/remap.h" | ||
| 10 | #include "transform_neon.h" | ||
| 11 | |||
| 12 | namespace kleidicv::neon { | ||
| 13 | |||
| 14 | template <typename ScalarType, bool IsLarge, size_t Channels, | ||
| 15 | kleidicv_border_type_t Border> | ||
| 16 | 1680 | void remap_f32_nearest(uint32x4_t v_xmax, uint32x4_t v_ymax, | |
| 17 | uint32x4_t v_src_stride, Rows<const ScalarType> src_rows, | ||
| 18 | Columns<ScalarType> dst, size_t dst_width, | ||
| 19 | Columns<const float> mapx, Columns<const float> mapy, | ||
| 20 | const size_t kStep, const ScalarType *border_values) { | ||
| 21 | 1680 | LoopUnroll2<TryToAvoidTailLoop> loop{dst_width, kStep}; | |
| 22 | 8864 | loop.unroll_once([&](size_t x) { | |
| 23 | 7184 | transform_pixels<ScalarType, IsLarge, Channels, Border>( | |
| 24 | 7184 | vld1q_f32(&mapx[x]), vld1q_f32(&mapy[x]), v_xmax, v_ymax, v_src_stride, | |
| 25 | 7184 | src_rows, dst.at(x), border_values); | |
| 26 | 7184 | }); | |
| 27 | 1680 | } | |
| 28 | |||
| 29 | template <typename ScalarType, bool IsLarge, kleidicv_border_type_t Border, | ||
| 30 | size_t Channels> | ||
| 31 | 24528 | void remap_f32_linear(uint32x4_t v_xmax, uint32x4_t v_ymax, | |
| 32 | uint32x4_t v_src_stride, Rows<const ScalarType> src_rows, | ||
| 33 | Columns<ScalarType> dst, size_t dst_width, | ||
| 34 | Columns<const float> mapx, Columns<const float> mapy, | ||
| 35 | const size_t kStep, const ScalarType *border_values) { | ||
| 36 | 93728 | auto load_xy = [&](size_t x) { | |
| 37 | 69200 | return FloatVectorPair{vld1q_f32(&mapx[x]), vld1q_f32(&mapy[x])}; | |
| 38 | }; | ||
| 39 | |||
| 40 | 58768 | auto vector_path_1ch = [&](size_t x) { | |
| 41 | 34240 | float32x4_t a, b, c, d, xfrac, yfrac; | |
| 42 | if constexpr (Border == KLEIDICV_BORDER_TYPE_REPLICATE) { | ||
| 43 | 17120 | load_quad_pixels_replicate<ScalarType, IsLarge>( | |
| 44 | 17120 | load_xy(x), v_xmax, v_ymax, v_src_stride, src_rows, xfrac, yfrac, a, | |
| 45 | b, c, d); | ||
| 46 | } else { | ||
| 47 | static_assert(Border == KLEIDICV_BORDER_TYPE_CONSTANT); | ||
| 48 | 17120 | load_quad_pixels_constant<ScalarType, IsLarge>( | |
| 49 | 17120 | load_xy(x), v_xmax, v_ymax, v_src_stride, border_values, src_rows, | |
| 50 | xfrac, yfrac, a, b, c, d); | ||
| 51 | } | ||
| 52 | 68480 | return lerp_2d(xfrac, yfrac, a, b, c, d); | |
| 53 | 34240 | }; | |
| 54 | |||
| 55 | 59488 | auto vector_path_2ch = [&](size_t x) { | |
| 56 | 34960 | float32x4x2_t a, b, c, d; | |
| 57 | 34960 | float32x4_t xfrac, yfrac; | |
| 58 | if constexpr (Border == KLEIDICV_BORDER_TYPE_REPLICATE) { | ||
| 59 | 17480 | load_quad_pixels_replicate_2ch<ScalarType, IsLarge>( | |
| 60 | 17480 | load_xy(x), v_xmax, v_ymax, v_src_stride, src_rows, xfrac, yfrac, a, | |
| 61 | b, c, d); | ||
| 62 | } else { | ||
| 63 | static_assert(Border == KLEIDICV_BORDER_TYPE_CONSTANT); | ||
| 64 | 17480 | load_quad_pixels_constant_2ch<ScalarType, IsLarge>( | |
| 65 | 17480 | load_xy(x), v_xmax, v_ymax, v_src_stride, border_values, src_rows, | |
| 66 | xfrac, yfrac, a, b, c, d); | ||
| 67 | } | ||
| 68 | 34960 | float32x4_t xfrac_low = vzip1q_f32(xfrac, xfrac); | |
| 69 | 34960 | float32x4_t yfrac_low = vzip1q_f32(yfrac, yfrac); | |
| 70 | 34960 | float32x4_t xfrac_high = vzip2q_f32(xfrac, xfrac); | |
| 71 | 34960 | float32x4_t yfrac_high = vzip2q_f32(yfrac, yfrac); | |
| 72 | |||
| 73 | 69920 | uint32x4_t result_low = | |
| 74 | 34960 | lerp_2d(xfrac_low, yfrac_low, a.val[0], b.val[0], c.val[0], d.val[0]); | |
| 75 | 69920 | uint32x4_t result_high = | |
| 76 | 34960 | lerp_2d(xfrac_high, yfrac_high, a.val[1], b.val[1], c.val[1], d.val[1]); | |
| 77 | 69920 | return vuzp1q_u16(result_low, result_high); | |
| 78 | 34960 | }; | |
| 79 | |||
| 80 | 24528 | LoopUnroll2<TryToAvoidTailLoop> loop{dst_width, kStep}; | |
| 81 | |||
| 82 | if constexpr (Channels == 1) { | ||
| 83 | if constexpr (std::is_same<ScalarType, uint8_t>::value) { | ||
| 84 | 7696 | loop.unroll_four_times([&](size_t x) { | |
| 85 | 1452 | ScalarType *p_dst = &dst[x]; | |
| 86 | 1452 | uint32x4_t res0 = vector_path_1ch(x); | |
| 87 | 1452 | x += kStep; | |
| 88 | 1452 | uint32x4_t res1 = vector_path_1ch(x); | |
| 89 | 1452 | uint16x8_t result16_0 = vuzp1q_u16(res0, res1); | |
| 90 | |||
| 91 | 1452 | x += kStep; | |
| 92 | 1452 | res0 = vector_path_1ch(x); | |
| 93 | 1452 | x += kStep; | |
| 94 | 1452 | res1 = vector_path_1ch(x); | |
| 95 | 1452 | uint16x8_t result16_1 = vuzp1q_u16(res0, res1); | |
| 96 | |||
| 97 | 1452 | vst1q_u8(p_dst, vuzp1q_u8(result16_0, result16_1)); | |
| 98 | 1452 | }); | |
| 99 | 18096 | loop.unroll_once([&](size_t x) { | |
| 100 | 11852 | uint8x16_t result = vreinterpretq_u8_u32(vector_path_1ch(x)); | |
| 101 | 11852 | dst[x] = vgetq_lane_u8(result, 0); | |
| 102 | 11852 | dst[x + 1] = vgetq_lane_u8(result, 4); | |
| 103 | 11852 | dst[x + 2] = vgetq_lane_u8(result, 8); | |
| 104 | 11852 | dst[x + 3] = vgetq_lane_u8(result, 12); | |
| 105 | 11852 | }); | |
| 106 | } | ||
| 107 | if constexpr (std::is_same<ScalarType, uint16_t>::value) { | ||
| 108 | 10040 | loop.unroll_twice([&](size_t x) { | |
| 109 | 4036 | ScalarType *p_dst = dst.ptr_at(x); | |
| 110 | 4036 | uint32x4_t res0 = vector_path_1ch(x); | |
| 111 | 4036 | x += kStep; | |
| 112 | 4036 | uint32x4_t res1 = vector_path_1ch(x); | |
| 113 | 4036 | vst1q_u16(p_dst, vuzp1q_u16(res0, res1)); | |
| 114 | 4036 | }); | |
| 115 | 14512 | loop.unroll_once([&](size_t x) { | |
| 116 | 8508 | uint32x4_t result = vector_path_1ch(x); | |
| 117 | 8508 | uint16x4_t result16 = vget_low_u16(vuzp1q_u16(result, result)); | |
| 118 | 8508 | vst1_u16(dst.ptr_at(x), result16); | |
| 119 | 8508 | }); | |
| 120 | } | ||
| 121 | } | ||
| 122 | if constexpr (Channels == 2) { | ||
| 123 | if constexpr (std::is_same<ScalarType, uint8_t>::value) { | ||
| 124 | 10940 | loop.unroll_twice([&](size_t x) { | |
| 125 | 4680 | ScalarType *p_dst = dst.ptr_at(x); | |
| 126 | 4680 | uint16x8_t result16_0 = vector_path_2ch(x); | |
| 127 | 4680 | x += kStep; | |
| 128 | 4680 | uint16x8_t result16_1 = vector_path_2ch(x); | |
| 129 | 4680 | vst1q_u8(p_dst, vuzp1q_u8(vreinterpretq_u8_u16(result16_0), | |
| 130 | vreinterpretq_u8_u16(result16_1))); | ||
| 131 | 4680 | }); | |
| 132 | 15040 | loop.unroll_once([&](size_t x) { | |
| 133 | 8780 | uint16x8_t result = vector_path_2ch(x); | |
| 134 | 8780 | vst1_u8(dst.ptr_at(x), vmovn_u16(result)); | |
| 135 | 8780 | }); | |
| 136 | } | ||
| 137 | if constexpr (std::is_same<ScalarType, uint16_t>::value) { | ||
| 138 | 22840 | loop.unroll_once([&](size_t x) { | |
| 139 | 16820 | uint16x8_t result = vector_path_2ch(x); | |
| 140 | 16820 | vst1q_u16(dst.ptr_at(x), result); | |
| 141 | 16820 | }); | |
| 142 | } | ||
| 143 | } | ||
| 144 | 24528 | } | |
| 145 | |||
| 146 | template <typename ScalarType, bool IsLarge, | ||
| 147 | kleidicv_interpolation_type_t Inter, kleidicv_border_type_t Border, | ||
| 148 | size_t Channels> | ||
| 149 | 1168 | void transform_operation(Rows<const ScalarType> src_rows, size_t src_width, | |
| 150 | size_t src_height, const ScalarType *border_values, | ||
| 151 | Rows<ScalarType> dst_rows, size_t dst_width, | ||
| 152 | size_t y_begin, size_t y_end, | ||
| 153 | Rows<const float> mapx_rows, | ||
| 154 | Rows<const float> mapy_rows) { | ||
| 155 | 2336 | uint32x4_t v_src_element_stride = vdupq_n_u32( | |
| 156 | 1168 | static_cast<uint32_t>(src_rows.stride() / sizeof(ScalarType))); | |
| 157 | 1168 | uint32x4_t v_xmax = vdupq_n_u32(static_cast<uint32_t>(src_width - 1)); | |
| 158 | 1168 | uint32x4_t v_ymax = vdupq_n_u32(static_cast<uint32_t>(src_height - 1)); | |
| 159 | 1168 | const size_t kStep = VecTraits<float>::num_lanes(); | |
| 160 | |||
| 161 |
64/64✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 8 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 8 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 8 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 8 times.
✓ Branch 17 taken 258 times.
✓ Branch 18 taken 10 times.
✓ Branch 19 taken 266 times.
✓ Branch 20 taken 8 times.
✓ Branch 21 taken 258 times.
✓ Branch 22 taken 10 times.
✓ Branch 23 taken 266 times.
✓ Branch 24 taken 132 times.
✓ Branch 25 taken 3114 times.
✓ Branch 26 taken 132 times.
✓ Branch 27 taken 3122 times.
✓ Branch 28 taken 132 times.
✓ Branch 29 taken 3114 times.
✓ Branch 30 taken 132 times.
✓ Branch 31 taken 3122 times.
✓ Branch 32 taken 2 times.
✓ Branch 33 taken 8 times.
✓ Branch 34 taken 2 times.
✓ Branch 35 taken 8 times.
✓ Branch 36 taken 2 times.
✓ Branch 37 taken 8 times.
✓ Branch 38 taken 2 times.
✓ Branch 39 taken 8 times.
✓ Branch 40 taken 4 times.
✓ Branch 41 taken 8 times.
✓ Branch 42 taken 2 times.
✓ Branch 43 taken 8 times.
✓ Branch 44 taken 4 times.
✓ Branch 45 taken 8 times.
✓ Branch 46 taken 2 times.
✓ Branch 47 taken 8 times.
✓ Branch 48 taken 8 times.
✓ Branch 49 taken 138 times.
✓ Branch 50 taken 10 times.
✓ Branch 51 taken 146 times.
✓ Branch 52 taken 8 times.
✓ Branch 53 taken 138 times.
✓ Branch 54 taken 10 times.
✓ Branch 55 taken 146 times.
✓ Branch 56 taken 132 times.
✓ Branch 57 taken 2994 times.
✓ Branch 58 taken 132 times.
✓ Branch 59 taken 3002 times.
✓ Branch 60 taken 132 times.
✓ Branch 61 taken 2994 times.
✓ Branch 62 taken 132 times.
✓ Branch 63 taken 3002 times.
|
27376 | for (size_t y = y_begin; y < y_end; ++y) { |
| 162 | if constexpr (Inter == KLEIDICV_INTERPOLATION_NEAREST) { | ||
| 163 | 1680 | remap_f32_nearest<ScalarType, IsLarge, Channels, Border>( | |
| 164 | 1680 | v_xmax, v_ymax, v_src_element_stride, src_rows, dst_rows.as_columns(), | |
| 165 | 1680 | dst_width, mapx_rows.as_columns(), mapy_rows.as_columns(), kStep, | |
| 166 | 1680 | border_values); | |
| 167 | } else { | ||
| 168 | static_assert(Inter == KLEIDICV_INTERPOLATION_LINEAR); | ||
| 169 | 24528 | remap_f32_linear<ScalarType, IsLarge, Border, Channels>( | |
| 170 | 24528 | v_xmax, v_ymax, v_src_element_stride, src_rows, dst_rows.as_columns(), | |
| 171 | 24528 | dst_width, mapx_rows.as_columns(), mapy_rows.as_columns(), kStep, | |
| 172 | 24528 | border_values); | |
| 173 | } | ||
| 174 | 26208 | ++mapx_rows; | |
| 175 | 26208 | ++mapy_rows; | |
| 176 | 26208 | ++dst_rows; | |
| 177 | 26208 | } | |
| 178 | 1168 | } | |
| 179 | |||
| 180 | // Most of the complexity comes from parameter checking. | ||
| 181 | // NOLINTBEGIN(readability-function-cognitive-complexity) | ||
| 182 | template <typename T> | ||
| 183 | 1276 | kleidicv_error_t remap_f32(const T *src, size_t src_stride, size_t src_width, | |
| 184 | size_t src_height, T *dst, size_t dst_stride, | ||
| 185 | size_t dst_width, size_t dst_height, size_t channels, | ||
| 186 | const float *mapx, size_t mapx_stride, | ||
| 187 | const float *mapy, size_t mapy_stride, | ||
| 188 | kleidicv_interpolation_type_t interpolation, | ||
| 189 | kleidicv_border_type_t border_type, | ||
| 190 | const T *border_value) { | ||
| 191 |
8/8✓ Branch 0 taken 4 times.
✓ Branch 1 taken 632 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 632 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 632 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 632 times.
|
1276 | CHECK_POINTER_AND_STRIDE(src, src_stride, src_height); |
| 192 |
8/8✓ Branch 0 taken 4 times.
✓ Branch 1 taken 628 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 628 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 626 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 626 times.
|
1264 | CHECK_POINTER_AND_STRIDE(dst, dst_stride, dst_height); |
| 193 |
8/8✓ Branch 0 taken 6 times.
✓ Branch 1 taken 622 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 622 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 622 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 622 times.
|
1254 | CHECK_POINTER_AND_STRIDE(mapx, mapx_stride, dst_height); |
| 194 |
8/8✓ Branch 0 taken 6 times.
✓ Branch 1 taken 616 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 616 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 616 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 616 times.
|
1244 | CHECK_POINTER_AND_STRIDE(mapy, mapy_stride, dst_height); |
| 195 |
12/12✓ Branch 0 taken 2 times.
✓ Branch 1 taken 614 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 612 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 612 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 614 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 612 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 612 times.
|
1232 | CHECK_IMAGE_SIZE(src_width, src_height); |
| 196 |
12/12✓ Branch 0 taken 2 times.
✓ Branch 1 taken 610 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 608 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 608 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 610 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 608 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 608 times.
|
1224 | CHECK_IMAGE_SIZE(dst_width, dst_height); |
| 197 |
8/8✓ Branch 0 taken 298 times.
✓ Branch 1 taken 310 times.
✓ Branch 2 taken 294 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 298 times.
✓ Branch 5 taken 310 times.
✓ Branch 6 taken 294 times.
✓ Branch 7 taken 4 times.
|
1216 | if (border_type == KLEIDICV_BORDER_TYPE_CONSTANT && nullptr == border_value) { |
| 198 | 8 | return KLEIDICV_ERROR_NULL_POINTER; | |
| 199 | } | ||
| 200 | |||
| 201 |
8/8✓ Branch 0 taken 584 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 584 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 584 times.
✓ Branch 5 taken 20 times.
✓ Branch 6 taken 584 times.
✓ Branch 7 taken 20 times.
|
2416 | if (!remap_f32_is_implemented<T>(src_stride, src_width, src_height, dst_width, |
| 202 | 1208 | dst_height, border_type, channels, | |
| 203 | 1208 | interpolation)) { | |
| 204 | 40 | return KLEIDICV_ERROR_NOT_IMPLEMENTED; | |
| 205 | } | ||
| 206 | |||
| 207 | 1168 | Rows<const T> src_rows{src, src_stride, channels}; | |
| 208 | 1168 | Rows<const float> mapx_rows{mapx, mapx_stride, 1}; | |
| 209 | 1168 | Rows<const float> mapy_rows{mapy, mapy_stride, 1}; | |
| 210 | 1168 | Rows<T> dst_rows{dst, dst_stride, channels}; | |
| 211 | 1168 | Rectangle rect{dst_width, dst_height}; | |
| 212 | |||
| 213 | 2336 | transform_operation<T>(is_image_large(src_rows, src_height), interpolation, | |
| 214 | border_type, channels, src_rows, src_width, src_height, | ||
| 215 | 1168 | border_value, dst_rows, dst_width, 0, dst_height, | |
| 216 | mapx_rows, mapy_rows); | ||
| 217 | |||
| 218 | 1168 | return KLEIDICV_OK; | |
| 219 | 1276 | } | |
| 220 | // NOLINTEND(readability-function-cognitive-complexity) | ||
| 221 | |||
| 222 | #define KLEIDICV_INSTANTIATE_TEMPLATE_REMAP_F32(type) \ | ||
| 223 | template KLEIDICV_TARGET_FN_ATTRS kleidicv_error_t remap_f32<type>( \ | ||
| 224 | const type *src, size_t src_stride, size_t src_width, size_t src_height, \ | ||
| 225 | type *dst, size_t dst_stride, size_t dst_width, size_t dst_height, \ | ||
| 226 | size_t channels, const float *mapx, size_t mapx_stride, \ | ||
| 227 | const float *mapy, size_t mapy_stride, \ | ||
| 228 | kleidicv_interpolation_type_t interpolation, \ | ||
| 229 | kleidicv_border_type_t border_type, const type *border_value) | ||
| 230 | |||
| 231 | KLEIDICV_INSTANTIATE_TEMPLATE_REMAP_F32(uint8_t); | ||
| 232 | KLEIDICV_INSTANTIATE_TEMPLATE_REMAP_F32(uint16_t); | ||
| 233 | |||
| 234 | } // namespace kleidicv::neon | ||
| 235 |