-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy paththreshold.cc
More file actions
334 lines (307 loc) · 12 KB
/
Copy paththreshold.cc
File metadata and controls
334 lines (307 loc) · 12 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//===- threshold.cc ----------------------------------------------*- C++
//-*-===//
//
// Copyright (C) 2022 Advanced Micro Devices, Inc.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#define NOCPP
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define REL_WRITE 0
#define REL_READ 1
enum _threshold_type {
XF_THRESHOLD_TYPE_BINARY = 0,
XF_THRESHOLD_TYPE_BINARY_INV = 1,
XF_THRESHOLD_TYPE_TRUNC = 2,
XF_THRESHOLD_TYPE_TOZERO = 3,
XF_THRESHOLD_TYPE_TOZERO_INV = 4,
};
// #define THRESH_TYPE XF_THRESHOLD_TYPE_BINARY
#include "../aie_kernel_utils.h"
#include <aie_api/aie.hpp>
template <typename T, int N>
__attribute__((noinline)) void
threshold_aie(T *img_in, T *img_out, const int32_t img_width,
const int32_t img_height, const T &thresh_val, const T &max_val,
const uint8_t thresholdType) {
::aie::vector<T, N> constants;
::aie::vector<T, N> data_out;
::aie::mask<N> temp_val;
constants[0] = 0; // updating constant zero_val value
constants[1] = thresh_val; // updating constant threshold value
constants[2] = max_val; // updating constant max_val value
switch (thresholdType) {
case XF_THRESHOLD_TYPE_TRUNC:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
data_out = ::aie::min(constants[1], data_buf1);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_BINARY:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(constants[1], data_buf1);
data_out = ::aie::select(constants[0], constants[2], temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_BINARY_INV:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(constants[1], data_buf1);
data_out = ::aie::select(constants[2], constants[0], temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_TOZERO:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(constants[1], data_buf1);
data_out = ::aie::select(constants[0], data_buf1, temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_TOZERO_INV:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(constants[1], data_buf1);
data_out = ::aie::select(data_buf1, constants[0], temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
default:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
data_out = ::aie::min(constants[1], data_buf1);
::aie::store_v(img_out, data_out);
img_out += N;
}
}
}
template <typename T, int N>
__attribute__((noinline)) void threshold4Ch_aie(
T *img_in, T *img_out, const int32_t img_width, const int32_t img_height,
const T &thresh_val1, const T &thresh_val2, const T &thresh_val3,
const T &thresh_val4, const T &max_val1, const T &max_val2,
const T &max_val3, const T &max_val4, const uint8_t thresholdType) {
::aie::vector<T, N> constants;
::aie::vector<T, N> data_out;
::aie::mask<N> temp_val;
// constants[0] = 0; // updating constant zero_val value
// constants[1] = thresh_val; // updating constant threshold value
// constants[2] = max_val; // updating constant max_val value
::aie::vector<T, N> mask_zeros = ::aie::zeros<T, N>();
::aie::vector<T, N> mask_thresh;
::aie::vector<T, N> mask_max;
for (int i = 0; i < N / 4; i++) {
mask_thresh[i * 4] = thresh_val1;
mask_thresh[i * 4 + 1] = thresh_val2;
mask_thresh[i * 4 + 2] = thresh_val3;
mask_thresh[i * 4 + 3] = thresh_val4;
mask_max[i * 4] = max_val1;
mask_max[i * 4 + 1] = max_val2;
mask_max[i * 4 + 2] = max_val3;
mask_max[i * 4 + 3] = max_val4;
}
switch (thresholdType) {
case XF_THRESHOLD_TYPE_TRUNC:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
data_out = ::aie::min(mask_thresh, data_buf1);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_BINARY:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(mask_thresh, data_buf1);
data_out = ::aie::select(mask_zeros, mask_max, temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_BINARY_INV:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(mask_thresh, data_buf1);
data_out = ::aie::select(mask_max, mask_zeros, temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_TOZERO:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(mask_thresh, data_buf1);
data_out = ::aie::select(mask_zeros, data_buf1, temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
case XF_THRESHOLD_TYPE_TOZERO_INV:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
temp_val = ::aie::lt(mask_thresh, data_buf1);
data_out = ::aie::select(data_buf1, mask_zeros, temp_val);
::aie::store_v(img_out, data_out);
img_out += N;
}
break;
default:
AIE_PREPARE_FOR_PIPELINING
AIE_LOOP_MIN_ITERATION_COUNT(14)
for (int j = 0; j < (img_height * img_width);
j += N) // 16x samples per loop
{
::aie::vector<T, N> data_buf1 =
::aie::load_v(img_in); // in:00++15|_________|_________|_________
img_in += N;
data_out = ::aie::min(mask_thresh, data_buf1);
::aie::store_v(img_out, data_out);
img_out += N;
}
}
}
extern "C" {
#if BIT_WIDTH == 8
void threshold(uint8_t *img_in, uint8_t *img_out, int32_t thresh_val,
int32_t max_val, int32_t img_width, int32_t img_height) {
threshold_aie<uint8_t, 64>(img_in, img_out, img_width, img_height, thresh_val,
max_val, XF_THRESHOLD_TYPE_BINARY);
}
void thresholdTile(uint8_t *in, uint8_t *out, int32_t tileHeight,
int32_t tileWidth, uint8_t thresholdValue, uint8_t maxValue,
uint8_t thresholdType) {
threshold_aie<uint8_t, 64>(in, out, tileWidth, tileHeight, thresholdValue,
maxValue, thresholdType);
}
void thresholdLine(uint8_t *in, uint8_t *out, int32_t lineWidth,
uint8_t thresholdValue, uint8_t maxValue,
uint8_t thresholdType) {
threshold_aie<uint8_t, 64>(in, out, lineWidth, 1, thresholdValue, maxValue,
thresholdType);
}
void threshold4ChLine(uint8_t *in, uint8_t *out, int32_t lineWidth,
uint8_t thresholdValue1, uint8_t thresholdValue2,
uint8_t thresholdValue3, uint8_t thresholdValue4,
uint8_t maxValue1, uint8_t maxValue2, uint8_t maxValue3,
uint8_t maxValue4, uint8_t thresholdType) {
threshold4Ch_aie<uint8_t, 64>(in, out, lineWidth, 1, thresholdValue1,
thresholdValue2, thresholdValue3,
thresholdValue4, maxValue1, maxValue2,
maxValue3, maxValue4, thresholdType);
}
#elif BIT_WIDTH == 16
void threshold(int16_t *img_in, int16_t *img_out, int32_t thresh_val,
int32_t max_val, int32_t img_width, int32_t img_height) {
threshold_aie<int16_t, 32>(img_in, img_out, img_width, img_height, thresh_val,
max_va, XF_THRESHOLD_TYPE_BINARY);
}
void thresholdTile(int16_t *in, int16_t *out, int32_t tileHeight,
int32_t tileWidth, int16_t thresholdValue, int16_t maxValue,
uint8_t thresholdType) {
threshold_aie<int16_t, 32>(in, out, tileWidth, tileHeight, thresholdValue,
maxValue),
thresholdType;
}
void thresholdLine(int16_t *in, int16_t *out, int32_t lineWidth,
int16_t thresholdValue, int16_t maxValue,
uint8_t thresholdType) {
threshold_aie<int16_t, 32>(in, out, lineWidth, 1, thresholdValue, maxValue,
thresholdType);
}
#else // 32
void threshold(int32_t *img_in, int32_t *img_out, int32_t thresh_val,
int32_t max_val, int32_t img_width, int32_t img_height) {
threshold_aie<int32_t, 16>(img_in, img_out, img_width, img_height, thresh_val,
max_val, XF_THRESHOLD_TYPE_BINARY);
}
void thresholdTile(int32_t *in, int32_t *out, int32_t tileHeight,
int32_t tileWidth, int32_t thresholdValue, int32_t maxValue,
uint8_t thresholdType) {
threshold_aie<int32_t, 16>(in, out, tileWidth, tileHeight, thresholdValue,
maxValue, thresholdType);
}
void thresholdLine(int32_t *in, int32_t *out, int32_t lineWidth,
int32_t thresholdValue, int32_t maxValue,
uint8_t thresholdType) {
threshold_aie<int32_t, 16>(in, out, lineWidth, 1, thresholdValue, maxValue,
thresholdType);
}
#endif
} // extern "C"