Repository navigation
Expand file tree
/
Copy pathGaussian2DFilter.cpp
More file actions
260 lines (225 loc) · 9.14 KB
/
Copy pathGaussian2DFilter.cpp
File metadata and controls
260 lines (225 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright 2025-2026 Board of Regents of the University of Wisconsin System
// SPDX-License-Identifier: BSD-2-Clause
// Implementation of fast 2D Gaussian filtering according to:
// Young IT, van Vliet LJ, 1995, Signal Processing 44:139-151.
// Recursive implementation of the Gaussian filter.
// https://doi.org/10.1016/0165-1684(95)00020-E
#ifdef USE_HIGHWAY_DYNAMIC_DISPATCH
#undef HWY_TARGET_INCLUDE
#define HWY_TARGET_INCLUDE "Gaussian2DFilter.cpp"
#include <hwy/foreach_target.h> // IWYU pragma: keep
#endif
#include "Gaussian2DFilter.h"
#include <array>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#ifdef USE_HIGHWAY_SIMD
#include <hwy/highway.h>
HWY_BEFORE_NAMESPACE();
namespace gaussian_internal::HWY_NAMESPACE {
namespace hn = hwy::HWY_NAMESPACE;
inline void ForwardFilterVerticalSIMD(float *data, std::size_t width,
std::size_t height, float B,
const std::array<float, 3> &bp) {
const hn::ScalableTag<float> d;
const std::size_t N = hn::Lanes(d);
const auto vec_B = hn::Set(d, B);
const auto vec_b1p = hn::Set(d, bp[0]);
const auto vec_b2p = hn::Set(d, bp[1]);
const auto vec_b3p = hn::Set(d, bp[2]);
// Processes N adjacent columns simultaneously
for (std::size_t i = 0; i + N <= width; i += N) {
float *p0 = data + i;
if (height < 1)
return;
auto row_curr = hn::LoadU(d, p0);
// Replicate the 3 rows before the border.
auto row_prev1 = row_curr;
auto row_prev2 = row_curr;
auto row_prev3 = row_curr;
for (std::size_t j = 1; j < height; ++j) {
float *const p = p0 + j * width;
row_prev3 = row_prev2;
row_prev2 = row_prev1;
row_prev1 = row_curr;
row_curr = hn::LoadU(d, p);
const auto result = hn::MulAdd(
vec_B, row_curr,
hn::MulAdd(vec_b1p, row_prev1,
hn::MulAdd(vec_b2p, row_prev2,
hn::Mul(vec_b3p, row_prev3))));
hn::StoreU(result, d, p);
row_curr = result;
}
}
const std::size_t remainder_start = (width / N) * N;
for (std::size_t i = remainder_start; i < width; ++i) {
ForwardFilter<float>(data + i, height, B, bp, width);
}
}
inline void BackwardFilterVerticalSIMD(float *data, std::size_t width,
std::size_t height, float B,
const std::array<float, 3> &bp) {
const hn::ScalableTag<float> d;
const std::size_t N = hn::Lanes(d);
const auto vec_B = hn::Set(d, B);
const auto vec_b1p = hn::Set(d, bp[0]);
const auto vec_b2p = hn::Set(d, bp[1]);
const auto vec_b3p = hn::Set(d, bp[2]);
// Process N adjacent columns simultaneously
for (std::size_t i = 0; i + N <= width; i += N) {
float *p0 = data + i;
if (height < 1)
return;
auto row_curr = hn::LoadU(d, p0 + (height - 1) * width);
// Replicate the 3 rows after the border.
auto row_next1 = row_curr;
auto row_next2 = row_curr;
auto row_next3 = row_curr;
for (std::size_t j = height - 2;; --j) {
float *const p = p0 + j * width;
row_next3 = row_next2;
row_next2 = row_next1;
row_next1 = row_curr;
row_curr = hn::LoadU(d, p);
const auto result = hn::MulAdd(
vec_B, row_curr,
hn::MulAdd(vec_b1p, row_next1,
hn::MulAdd(vec_b2p, row_next2,
hn::Mul(vec_b3p, row_next3))));
hn::StoreU(result, d, p);
row_curr = result;
if (j == 0) {
break; // Exit before underflow
}
}
}
const std::size_t remainder_start = (width / N) * N;
for (std::size_t i = remainder_start; i < width; ++i) {
BackwardFilter<float>(data + i, height, B, bp, width);
}
}
inline void ForwardFilterHorizontalSIMD(float *data, std::size_t width,
std::size_t height, float B,
const std::array<float, 3> &bp) {
const hn::ScalableTag<float> d;
const hn::ScalableTag<std::int32_t> di;
const std::size_t N = hn::Lanes(d);
const auto vec_B = hn::Set(d, B);
const auto vec_b1p = hn::Set(d, bp[0]);
const auto vec_b2p = hn::Set(d, bp[1]);
const auto vec_b3p = hn::Set(d, bp[2]);
// Process N consecutive rows simultaneously
for (std::size_t j = 0; j + N <= height; j += N) {
const auto idx0 = hn::Iota(di, static_cast<std::int32_t>(j)) *
hn::Set(di, static_cast<std::int32_t>(width));
if (width < 1)
return;
auto col_curr = hn::GatherIndex(d, data, idx0);
// Replicate the 3 columns before the border.
auto col_prev1 = col_curr;
auto col_prev2 = col_curr;
auto col_prev3 = col_curr;
for (std::size_t i = 1; i < width; ++i) {
const auto idx = idx0 + hn::Set(di, static_cast<std::int32_t>(i));
col_prev3 = col_prev2;
col_prev2 = col_prev1;
col_prev1 = col_curr;
col_curr = hn::GatherIndex(d, data, idx);
const auto result = hn::MulAdd(
vec_B, col_curr,
hn::MulAdd(vec_b1p, col_prev1,
hn::MulAdd(vec_b2p, col_prev2,
hn::Mul(vec_b3p, col_prev3))));
hn::ScatterIndex(result, d, data, idx);
col_curr = result;
}
}
const std::size_t remainder_start = (height / N) * N;
for (std::size_t j = remainder_start; j < height; ++j) {
ForwardFilter<float, 1>(data + j * width, width, B, bp);
}
}
inline void BackwardFilterHorizontalSIMD(float *data, std::size_t width,
std::size_t height, float B,
const std::array<float, 3> &bp) {
const hn::ScalableTag<float> d;
const hn::ScalableTag<std::int32_t> di;
const std::size_t N = hn::Lanes(d);
const auto vec_B = hn::Set(d, B);
const auto vec_b1p = hn::Set(d, bp[0]);
const auto vec_b2p = hn::Set(d, bp[1]);
const auto vec_b3p = hn::Set(d, bp[2]);
// Process N consecutive rows simultaneously
for (std::size_t j = 0; j + N <= height; j += N) {
const auto idx0 = hn::Iota(di, static_cast<std::int32_t>(j)) *
hn::Set(di, static_cast<std::int32_t>(width));
if (width < 1)
return;
auto col_curr = hn::GatherIndex(
d, data, idx0 + hn::Set(di, static_cast<std::int32_t>(width - 1)));
// Replicate the 3 columns after the border.
auto col_next1 = col_curr;
auto col_next2 = col_curr;
auto col_next3 = col_curr;
for (std::size_t i = width - 2;; --i) {
const auto idx = idx0 + hn::Set(di, static_cast<std::int32_t>(i));
col_next3 = col_next2;
col_next2 = col_next1;
col_next1 = col_curr;
col_curr = hn::GatherIndex(d, data, idx);
const auto result = hn::MulAdd(
vec_B, col_curr,
hn::MulAdd(vec_b1p, col_next1,
hn::MulAdd(vec_b2p, col_next2,
hn::Mul(vec_b3p, col_next3))));
hn::ScatterIndex(result, d, data, idx);
col_curr = result;
if (i == 0)
break; // Exit before underflow
}
}
const std::size_t remainder_start = (height / N) * N;
for (std::size_t j = remainder_start; j < height; ++j) {
BackwardFilter<float, 1>(data + j * width, width, B, bp);
}
}
inline void FastGaussian2DSIMDImpl(float *data, std::size_t width,
std::size_t height, float sigma) {
const auto bp = b_predivided(b(q(sigma)));
const auto theB = B(bp);
ForwardFilterHorizontalSIMD(data, width, height, theB, bp);
BackwardFilterHorizontalSIMD(data, width, height, theB, bp);
ForwardFilterVerticalSIMD(data, width, height, theB, bp);
BackwardFilterVerticalSIMD(data, width, height, theB, bp);
}
} // namespace gaussian_internal::HWY_NAMESPACE
HWY_AFTER_NAMESPACE();
#if HWY_ONCE
namespace gaussian_internal {
#ifdef USE_HIGHWAY_DYNAMIC_DISPATCH
HWY_EXPORT(FastGaussian2DSIMDImpl);
#endif
void FastGaussian2DSIMD(float *data, std::size_t width, std::size_t height,
float sigma) {
#ifdef USE_HIGHWAY_DYNAMIC_DISPATCH
HWY_DYNAMIC_DISPATCH(FastGaussian2DSIMDImpl)(data, width, height, sigma);
#else
HWY_STATIC_DISPATCH(FastGaussian2DSIMDImpl)(data, width, height, sigma);
#endif
}
} // namespace gaussian_internal
#endif // HWY_ONCE
#endif // USE_HIGHWAY_SIMD
#if defined(USE_HIGHWAY_SIMD) == HWY_ONCE
// Entry point
void FastGaussian2D(float *data, std::size_t width, std::size_t height,
float sigma) {
#ifdef USE_HIGHWAY_SIMD
return gaussian_internal::FastGaussian2DSIMD(data, width, height, sigma);
#endif
gaussian_internal::FastGaussian2DScalar(data, width, height, sigma);
}
#endif