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 | 134 | ScharrInterleaved(Rows<int16_t> hori_deriv_buffer, | |
46 | Rows<int16_t> vert_deriv_buffer, | ||
47 | size_t width) KLEIDICV_STREAMING | ||
48 | 134 | : hori_deriv_buffer_(hori_deriv_buffer), | |
49 | 134 | vert_deriv_buffer_(vert_deriv_buffer), | |
50 | 134 | width_(width) {} | |
51 | |||
52 | 134 | 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 134 times.
✓ Branch 1 taken 3020 times.
|
3154 | for (size_t i = y_begin; i < y_end; ++i) { |
55 | 3020 | process_vertical(src_rows.at(static_cast<ptrdiff_t>(i))); | |
56 | 3020 | process_horizontal(dst_rows.at(static_cast<ptrdiff_t>(i))); | |
57 | 3020 | } | |
58 | 134 | } | |
59 | |||
60 | private: | ||
61 | 3184 | void vertical_vector_path(svbool_t pg, Rows<const uint8_t> src_rows, | |
62 | ptrdiff_t index) KLEIDICV_STREAMING { | ||
63 | 3184 | SourceVectorType src_0 = svld1(pg, &src_rows.at(0)[index]); | |
64 | 3184 | SourceVectorType src_1 = svld1(pg, &src_rows.at(1)[index]); | |
65 | 3184 | SourceVectorType src_2 = svld1(pg, &src_rows.at(2)[index]); | |
66 | |||
67 | // Horizontal derivative approximation | ||
68 | 3184 | svuint16_t hori_acc_b = svaddlb(src_0, src_2); | |
69 | 3184 | svuint16_t hori_acc_t = svaddlt(src_0, src_2); | |
70 | |||
71 | 3184 | hori_acc_b = svmul_n_u16_x(pg, hori_acc_b, 3); | |
72 | 3184 | hori_acc_t = svmul_n_u16_x(pg, hori_acc_t, 3); | |
73 | |||
74 | 3184 | hori_acc_b = svmlalb_n_u16(hori_acc_b, src_1, 10); | |
75 | 3184 | hori_acc_t = svmlalt_n_u16(hori_acc_t, src_1, 10); | |
76 | |||
77 | 6368 | svint16x2_t hori_interleaved = | |
78 | 3184 | svcreate2(svreinterpret_s16(hori_acc_b), svreinterpret_s16(hori_acc_t)); | |
79 | 3184 | svst2(pg, &hori_deriv_buffer_[index], hori_interleaved); | |
80 | |||
81 | // Vertical derivative approximation | ||
82 | 3184 | svuint16_t vert_acc_b = svsublb(src_2, src_0); | |
83 | 3184 | svuint16_t vert_acc_t = svsublt(src_2, src_0); | |
84 | |||
85 | 6368 | svint16x2_t vert_interleaved = | |
86 | 3184 | svcreate2(svreinterpret_s16(vert_acc_b), svreinterpret_s16(vert_acc_t)); | |
87 | 3184 | svst2(pg, &vert_deriv_buffer_[index], vert_interleaved); | |
88 | 3184 | } | |
89 | |||
90 | 3020 | void process_vertical(Rows<const uint8_t> src_rows) KLEIDICV_STREAMING { | |
91 | 6040 | LoopUnroll2 loop{width_ * src_rows.channels(), | |
92 | 3020 | SourceVecTraits::num_lanes()}; | |
93 | 3020 | svbool_t pg_all = SourceVecTraits::svptrue(); | |
94 | |||
95 | 3186 | loop.unroll_once([&](ptrdiff_t index) KLEIDICV_STREAMING { | |
96 | 166 | vertical_vector_path(pg_all, src_rows, index); | |
97 | 166 | }); | |
98 | |||
99 | 6038 | loop.remaining([&](ptrdiff_t index, ptrdiff_t length) KLEIDICV_STREAMING { | |
100 | 3018 | svbool_t pg = SourceVecTraits::svwhilelt(index, length); | |
101 | 3018 | vertical_vector_path(pg, src_rows, index); | |
102 | 3018 | }); | |
103 | 3020 | } | |
104 | |||
105 | 3544 | void horizontal_vector_path(svbool_t pg, Rows<int16_t> dst_rows, | |
106 | ptrdiff_t index) KLEIDICV_STREAMING { | ||
107 | // Horizontal derivative approximation | ||
108 | 3544 | BufferVectorType hori_buff_0 = svld1(pg, &hori_deriv_buffer_[index]); | |
109 | 3544 | BufferVectorType hori_buff_2 = svld1(pg, &hori_deriv_buffer_[index + 2]); | |
110 | |||
111 | 3544 | DestinationVectorType hori_result = svsub_x(pg, hori_buff_2, hori_buff_0); | |
112 | |||
113 | // Vertical derivative approximation | ||
114 | 3544 | BufferVectorType vert_buff_0 = svld1(pg, &vert_deriv_buffer_[index]); | |
115 | 3544 | BufferVectorType vert_buff_1 = svld1(pg, &vert_deriv_buffer_[index + 1]); | |
116 | 3544 | BufferVectorType vert_buff_2 = svld1(pg, &vert_deriv_buffer_[index + 2]); | |
117 | |||
118 | 3544 | DestinationVectorType vert_result = svadd_x(pg, vert_buff_0, vert_buff_2); | |
119 | 3544 | vert_result = svmul_n_s16_x(pg, vert_result, 3); | |
120 | 3544 | vert_result = svmad_s16_x(pg, vert_buff_1, svdup_n_s16(10), vert_result); | |
121 | |||
122 | // Store results | ||
123 | 3544 | svint16x2_t interleaved_result = svcreate2(hori_result, vert_result); | |
124 | 3544 | svst2(pg, &dst_rows.at(0, index)[0], interleaved_result); | |
125 | 3544 | } | |
126 | |||
127 | 3020 | void process_horizontal(Rows<int16_t> dst_rows) KLEIDICV_STREAMING { | |
128 | // width is decremented by 2 as the result has less columns. | ||
129 | 6040 | LoopUnroll2 loop{(width_ - 2) * hori_deriv_buffer_.channels(), | |
130 | 3020 | BufferVecTraits::num_lanes()}; | |
131 | 3020 | svbool_t pg_all = BufferVecTraits::svptrue(); | |
132 | |||
133 | 3612 | loop.unroll_once([&](ptrdiff_t index) KLEIDICV_STREAMING { | |
134 | 592 | horizontal_vector_path(pg_all, dst_rows, index); | |
135 | 592 | }); | |
136 | |||
137 | 5972 | loop.remaining([&](ptrdiff_t index, ptrdiff_t length) KLEIDICV_STREAMING { | |
138 | 2952 | svbool_t pg = BufferVecTraits::svwhilelt(index, length); | |
139 | 2952 | horizontal_vector_path(pg, dst_rows, index); | |
140 | 2952 | }); | |
141 | 3020 | } | |
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 | 134 | void operator()(void *ptr) const { std::free(ptr); } | |
151 | }; | ||
152 | |||
153 | 146 | 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 2 times.
✓ Branch 1 taken 144 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 144 times.
|
146 | CHECK_POINTER_AND_STRIDE(src, src_stride, src_height); |
160 |
4/4✓ Branch 0 taken 4 times.
✓ Branch 1 taken 140 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 140 times.
|
144 | CHECK_POINTER_AND_STRIDE(dst, dst_stride, src_height); |
161 |
6/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 136 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 136 times.
|
140 | 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 | 136 | 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 | 136 | size_t buffer_height = 2; | |
172 | // Memory is allocated with malloc to avoid its initialization. | ||
173 | 136 | void *allocation = std::malloc(buffer_stride * buffer_height); | |
174 | |||
175 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 2 times.
|
136 | if (!allocation) { |
176 | 2 | return KLEIDICV_ERROR_ALLOCATION; | |
177 | } | ||
178 | |||
179 | 268 | std::unique_ptr<int16_t, ScharrBufferDeleter> buffer( | |
180 | 134 | reinterpret_cast<int16_t *>(allocation)); | |
181 | |||
182 | 134 | 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 | 134 | Rows<int16_t> dst_rows{dst, dst_stride, src_channels * 2}; | |
187 | |||
188 | 134 | Rows<int16_t> hori_deriv_buffer{buffer.get(), buffer_stride, src_channels}; | |
189 | |||
190 | 268 | int16_t *vert_deriv_ptr = reinterpret_cast<int16_t *>( | |
191 | 134 | reinterpret_cast<uint8_t *>(buffer.get()) + buffer_stride); | |
192 | 134 | Rows<int16_t> vert_deriv_buffer{vert_deriv_ptr, buffer_stride, src_channels}; | |
193 | |||
194 | 268 | ScharrInterleaved(hori_deriv_buffer, vert_deriv_buffer, src_width) | |
195 | 134 | .process(src_rows, dst_rows, y_begin, y_end); | |
196 | |||
197 | 134 | return KLEIDICV_OK; | |
198 | 146 | } | |
199 | } // namespace KLEIDICV_TARGET_NAMESPACE | ||
200 |