| 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 <cstddef> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <cstdlib> | ||
| 8 | #include <memory> | ||
| 9 | |||
| 10 | #include "kleidicv/config.h" | ||
| 11 | #include "kleidicv/ctypes.h" | ||
| 12 | #include "kleidicv/sve2.h" | ||
| 13 | #include "kleidicv/types.h" | ||
| 14 | #include "kleidicv/utils.h" | ||
| 15 | |||
| 16 | namespace KLEIDICV_TARGET_NAMESPACE { | ||
| 17 | |||
| 18 | // Scharr filtering in both horizontal and vertical directions, horizontal and | ||
| 19 | // vertical derivative approximations are stored interleaved. | ||
| 20 | // | ||
| 21 | // The applied weights for the horizontal approximation, as the kernel is | ||
| 22 | // mirrored both vertically and horizontally during the convolution: | ||
| 23 | // [ -3 0 3 ] [ 3 ] | ||
| 24 | // F = [ -10 0 10 ] = [ 10 ] * [ -1, 0, 1 ] | ||
| 25 | // [ -3 0 3 ] [ 3 ] | ||
| 26 | // | ||
| 27 | // The applied weights for the vertical approximation, as the kernel is mirrored | ||
| 28 | // both vertically and horizontally during the convolution: | ||
| 29 | // [ -3 -10 -3 ] [ -1 ] | ||
| 30 | // F = [ 0, 0, 0 ] = [ 0 ] * [ 3, 10, 3 ] | ||
| 31 | // [ 3 10 3 ] [ 1 ] | ||
| 32 | // | ||
| 33 | class ScharrInterleaved { | ||
| 34 | using SourceType = uint8_t; | ||
| 35 | using SourceVecTraits = VecTraits<SourceType>; | ||
| 36 | using SourceVectorType = typename SourceVecTraits::VectorType; | ||
| 37 | using BufferType = int16_t; | ||
| 38 | using BufferVecTraits = VecTraits<BufferType>; | ||
| 39 | using BufferVectorType = typename BufferVecTraits::VectorType; | ||
| 40 | using DestinationType = int16_t; | ||
| 41 | using DestinationVecTraits = VecTraits<DestinationType>; | ||
| 42 | using DestinationVectorType = typename DestinationVecTraits::VectorType; | ||
| 43 | |||
| 44 | public: | ||
| 45 | 201 | ScharrInterleaved(Rows<int16_t> hori_deriv_buffer, | |
| 46 | Rows<int16_t> vert_deriv_buffer, | ||
| 47 | size_t width) KLEIDICV_STREAMING | ||
| 48 | 201 | : hori_deriv_buffer_(hori_deriv_buffer), | |
| 49 | 201 | vert_deriv_buffer_(vert_deriv_buffer), | |
| 50 | 201 | width_(width) {} | |
| 51 | |||
| 52 | 201 | void process(Rows<const uint8_t> src_rows, Rows<int16_t> dst_rows, | |
| 53 | size_t y_begin, size_t y_end) KLEIDICV_STREAMING { | ||
| 54 |
2/2✓ Branch 0 taken 201 times.
✓ Branch 1 taken 4578 times.
|
4779 | for (size_t i = y_begin; i < y_end; ++i) { |
| 55 | 4578 | process_vertical(src_rows.at(static_cast<ptrdiff_t>(i))); | |
| 56 | 4578 | process_horizontal(dst_rows.at(static_cast<ptrdiff_t>(i))); | |
| 57 | 4578 | } | |
| 58 | 201 | } | |
| 59 | |||
| 60 | private: | ||
| 61 | 4756 | void vertical_vector_path(svbool_t pg, Rows<const uint8_t> src_rows, | |
| 62 | ptrdiff_t index) KLEIDICV_STREAMING { | ||
| 63 | 4756 | SourceVectorType src_0 = svld1(pg, &src_rows.at(0)[index]); | |
| 64 | 4756 | SourceVectorType src_1 = svld1(pg, &src_rows.at(1)[index]); | |
| 65 | 4756 | SourceVectorType src_2 = svld1(pg, &src_rows.at(2)[index]); | |
| 66 | |||
| 67 | // Horizontal derivative approximation | ||
| 68 | 4756 | svuint16_t hori_acc_b = svaddlb(src_0, src_2); | |
| 69 | 4756 | svuint16_t hori_acc_t = svaddlt(src_0, src_2); | |
| 70 | |||
| 71 | 4756 | hori_acc_b = svmul_n_u16_x(pg, hori_acc_b, 3); | |
| 72 | 4756 | hori_acc_t = svmul_n_u16_x(pg, hori_acc_t, 3); | |
| 73 | |||
| 74 | 4756 | hori_acc_b = svmlalb_n_u16(hori_acc_b, src_1, 10); | |
| 75 | 4756 | hori_acc_t = svmlalt_n_u16(hori_acc_t, src_1, 10); | |
| 76 | |||
| 77 | 9512 | svint16x2_t hori_interleaved = | |
| 78 | 4756 | svcreate2(svreinterpret_s16(hori_acc_b), svreinterpret_s16(hori_acc_t)); | |
| 79 | 4756 | svst2(pg, &hori_deriv_buffer_[index], hori_interleaved); | |
| 80 | |||
| 81 | // Vertical derivative approximation | ||
| 82 | 4756 | svuint16_t vert_acc_b = svsublb(src_2, src_0); | |
| 83 | 4756 | svuint16_t vert_acc_t = svsublt(src_2, src_0); | |
| 84 | |||
| 85 | 9512 | svint16x2_t vert_interleaved = | |
| 86 | 4756 | svcreate2(svreinterpret_s16(vert_acc_b), svreinterpret_s16(vert_acc_t)); | |
| 87 | 4756 | svst2(pg, &vert_deriv_buffer_[index], vert_interleaved); | |
| 88 | 4756 | } | |
| 89 | |||
| 90 | 4578 | void process_vertical(Rows<const uint8_t> src_rows) KLEIDICV_STREAMING { | |
| 91 | 9156 | LoopUnroll2 loop{width_ * src_rows.channels(), | |
| 92 | 4578 | SourceVecTraits::num_lanes()}; | |
| 93 | 4578 | svbool_t pg_all = SourceVecTraits::svptrue(); | |
| 94 | |||
| 95 | 4759 | loop.unroll_once([&](ptrdiff_t index) KLEIDICV_STREAMING { | |
| 96 | 181 | vertical_vector_path(pg_all, src_rows, index); | |
| 97 | 181 | }); | |
| 98 | |||
| 99 | 9153 | loop.remaining([&](ptrdiff_t index, ptrdiff_t length) KLEIDICV_STREAMING { | |
| 100 | 4575 | svbool_t pg = SourceVecTraits::svwhilelt(index, length); | |
| 101 | 4575 | vertical_vector_path(pg, src_rows, index); | |
| 102 | 4575 | }); | |
| 103 | 4578 | } | |
| 104 | |||
| 105 | 5194 | void horizontal_vector_path(svbool_t pg, Rows<int16_t> dst_rows, | |
| 106 | ptrdiff_t index) KLEIDICV_STREAMING { | ||
| 107 | // Horizontal derivative approximation | ||
| 108 | 5194 | BufferVectorType hori_buff_0 = svld1(pg, &hori_deriv_buffer_[index]); | |
| 109 | 5194 | BufferVectorType hori_buff_2 = svld1(pg, &hori_deriv_buffer_[index + 2]); | |
| 110 | |||
| 111 | 5194 | DestinationVectorType hori_result = svsub_x(pg, hori_buff_2, hori_buff_0); | |
| 112 | |||
| 113 | // Vertical derivative approximation | ||
| 114 | 5194 | BufferVectorType vert_buff_0 = svld1(pg, &vert_deriv_buffer_[index]); | |
| 115 | 5194 | BufferVectorType vert_buff_1 = svld1(pg, &vert_deriv_buffer_[index + 1]); | |
| 116 | 5194 | BufferVectorType vert_buff_2 = svld1(pg, &vert_deriv_buffer_[index + 2]); | |
| 117 | |||
| 118 | 5194 | DestinationVectorType vert_result = svadd_x(pg, vert_buff_0, vert_buff_2); | |
| 119 | 5194 | vert_result = svmul_n_s16_x(pg, vert_result, 3); | |
| 120 | 5194 | vert_result = svmad_s16_x(pg, vert_buff_1, svdup_n_s16(10), vert_result); | |
| 121 | |||
| 122 | // Store results | ||
| 123 | 5194 | svint16x2_t interleaved_result = svcreate2(hori_result, vert_result); | |
| 124 | 5194 | svst2(pg, &dst_rows.at(0, index)[0], interleaved_result); | |
| 125 | 5194 | } | |
| 126 | |||
| 127 | 4578 | void process_horizontal(Rows<int16_t> dst_rows) KLEIDICV_STREAMING { | |
| 128 | // width is decremented by 2 as the result has less columns. | ||
| 129 | 9156 | LoopUnroll2 loop{(width_ - 2) * hori_deriv_buffer_.channels(), | |
| 130 | 4578 | BufferVecTraits::num_lanes()}; | |
| 131 | 4578 | svbool_t pg_all = BufferVecTraits::svptrue(); | |
| 132 | |||
| 133 | 5262 | loop.unroll_once([&](ptrdiff_t index) KLEIDICV_STREAMING { | |
| 134 | 684 | horizontal_vector_path(pg_all, dst_rows, index); | |
| 135 | 684 | }); | |
| 136 | |||
| 137 | 9088 | loop.remaining([&](ptrdiff_t index, ptrdiff_t length) KLEIDICV_STREAMING { | |
| 138 | 4510 | svbool_t pg = BufferVecTraits::svwhilelt(index, length); | |
| 139 | 4510 | horizontal_vector_path(pg, dst_rows, index); | |
| 140 | 4510 | }); | |
| 141 | 4578 | } | |
| 142 | |||
| 143 | Rows<int16_t> hori_deriv_buffer_; | ||
| 144 | Rows<int16_t> vert_deriv_buffer_; | ||
| 145 | size_t width_; | ||
| 146 | }; // end of class ScharrInterleaved | ||
| 147 | |||
| 148 | class ScharrBufferDeleter { | ||
| 149 | public: | ||
| 150 | 201 | void operator()(void *ptr) const { std::free(ptr); } | |
| 151 | }; | ||
| 152 | |||
| 153 | 219 | static kleidicv_error_t kleidicv_scharr_interleaved_stripe_s16_u8_sc( | |
| 154 | const uint8_t *src, size_t src_stride, size_t src_width, size_t src_height, | ||
| 155 | size_t src_channels, int16_t *dst, size_t dst_stride, size_t y_begin, | ||
| 156 | size_t y_end) KLEIDICV_STREAMING { | ||
| 157 | // Does not include checks for whether the operation is implemented. | ||
| 158 | // This must be done earlier, by scharr_interleaved_is_implemented. | ||
| 159 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 216 times.
|
219 | CHECK_POINTER_AND_STRIDE(src, src_stride, src_height); |
| 160 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 210 times.
|
216 | CHECK_POINTER_AND_STRIDE(dst, dst_stride, src_height); |
| 161 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 207 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 204 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 204 times.
|
210 | CHECK_IMAGE_SIZE(src_width, src_height); |
| 162 | |||
| 163 | // Allocating more elements because in case of SVE interleaving stores are | ||
| 164 | // governed by one predicate. For example, if a predicate requires 7 uint8_t | ||
| 165 | // elements and an algorithm performs widening to 16 bits, the resulting | ||
| 166 | // interleaving store will still be governed by the same predicate, thus | ||
| 167 | // storing 8 elements. Choosing '3' to account for svst4(). | ||
| 168 | 204 | size_t buffer_stride = ((src_width * src_channels) + 3) * sizeof(int16_t); | |
| 169 | // Buffer has two rows, one for the horizontal derivative approximation, one | ||
| 170 | // for the vertical one. | ||
| 171 | 204 | size_t buffer_height = 2; | |
| 172 | // Memory is allocated with malloc to avoid its initialization. | ||
| 173 | 204 | void *allocation = std::malloc(buffer_stride * buffer_height); | |
| 174 | |||
| 175 |
2/2✓ Branch 0 taken 201 times.
✓ Branch 1 taken 3 times.
|
204 | if (!allocation) { |
| 176 | 3 | return KLEIDICV_ERROR_ALLOCATION; | |
| 177 | } | ||
| 178 | |||
| 179 | 402 | std::unique_ptr<int16_t, ScharrBufferDeleter> buffer( | |
| 180 | 201 | reinterpret_cast<int16_t *>(allocation)); | |
| 181 | |||
| 182 | 201 | Rows<const uint8_t> src_rows{src, src_stride, src_channels}; | |
| 183 | |||
| 184 | // Result is treated as it has double the channel number compared to the | ||
| 185 | // input. | ||
| 186 | 201 | Rows<int16_t> dst_rows{dst, dst_stride, src_channels * 2}; | |
| 187 | |||
| 188 | 201 | Rows<int16_t> hori_deriv_buffer{buffer.get(), buffer_stride, src_channels}; | |
| 189 | |||
| 190 | 402 | int16_t *vert_deriv_ptr = reinterpret_cast<int16_t *>( | |
| 191 | 201 | reinterpret_cast<uint8_t *>(buffer.get()) + buffer_stride); | |
| 192 | 201 | Rows<int16_t> vert_deriv_buffer{vert_deriv_ptr, buffer_stride, src_channels}; | |
| 193 | |||
| 194 | 402 | ScharrInterleaved(hori_deriv_buffer, vert_deriv_buffer, src_width) | |
| 195 | 201 | .process(src_rows, dst_rows, y_begin, y_end); | |
| 196 | |||
| 197 | 201 | return KLEIDICV_OK; | |
| 198 | 219 | } | |
| 199 | } // namespace KLEIDICV_TARGET_NAMESPACE | ||
| 200 |