forked from dillonhuff/clockwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_compute_units.h
346 lines (285 loc) · 7.46 KB
/
common_compute_units.h
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
335
336
337
338
339
340
341
342
343
344
345
346
#pragma once
#include "hw_classes.h"
static inline
float to_float(const hw_uint<32>& in) {
int i = in.to_int();
void* ip = (void*)(&i);
float* f = (float*) ip;
return (*f);
}
static inline
hw_uint<32> to_bits(const float& f) {
int* ip = (int*) ((void*)(&f));
return *ip;
}
//static inline
//hw_uint<32> jacobi2d_compute(const hw_uint<32*5>& in) {
//hw_uint<32> v0 = in.extract<0, 31>();
//hw_uint<32> v1 = in.extract<32, 63>();
//hw_uint<32> v2 = in.extract<64, 95>();
//hw_uint<32> v3 = in.extract<96, 127>();
//hw_uint<32> v4 = in.extract<128, 159>();
//return to_bits((to_float(v0) + to_float(v1) + to_float(v2) + to_float(v3) + to_float(v4)) * ((float) 0.2));
//}
static inline
hw_uint<32> heat3d_compute(const hw_uint<32*9>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
hw_uint<32> v3 = in.extract<96, 127>();
hw_uint<32> v4 = in.extract<128, 159>();
hw_uint<32> v5 = in.extract<32*5, 32*6 - 1>();
hw_uint<32> v6 = in.extract<32*6, 32*7 - 1>();
hw_uint<32> v7 = in.extract<32*7, 32*8 - 1>();
hw_uint<32> v8 = in.extract<32*8, 32*9 - 1>();
return (v0 + v1 + v2) / 8 +
(v3 + v4 + v5) / 8 +
(v6 + v7 + v8) / 8;
}
static inline
hw_uint<32> jacobi2d_compute(const hw_uint<32*5>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
hw_uint<32> v3 = in.extract<96, 127>();
hw_uint<32> v4 = in.extract<128, 159>();
return (((v0) + (v1) + (v2) + (v3) + (v4)) / 5);
}
template<typename T>
static inline
T plus_one(const T& val) {
T one(1);
return val + one;
}
template<typename T>
static inline
T id(const T& val) {
return val;
}
static inline
int max_zero(const int& val) {
return max(val, 0);
}
template<typename T>
static inline
T merge_exposures(T& bright, T& dark, T& bw, T& dw) {
return (bw*bright) + (dw*dark);
}
template<typename T>
static inline
T scale_exposure(T& src) {
return 3*src;
}
template<typename T>
static inline
T exposure_weight(T& src) {
return 2.0*src;
}
template<typename T>
static inline
T f_divide(T& src, T& a0) {
return src / a0;
}
template<typename T>
static inline
T add(T& src, T& a0) {
return src + a0;
}
template<typename T>
static inline
T diff(T& src, T& a0) {
return src - a0;
}
static inline
hw_uint<32> diff_b(const hw_uint<32*2>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
return diff(v0, v1);
}
template<typename T>
static inline
T inc(T& src, T& a0) {
return src + a0;
}
//int inc(int& src, int& a0) {
//return src + a0;
//}
int fma(int& src, int& a0, int& a1) {
return src + a0*a1;
}
hw_uint<32> set_zero_32() {
return hw_uint<32>(0);
}
int set_zero() {
return 0;
}
static inline
hw_uint<32> blury_comp(hw_uint<32*3>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
return v0 + v1 + v2;
}
static inline
hw_uint<32> blurx_comp(hw_uint<32*3>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
return v0 + v1 + v2;
}
static inline
int conv_1_3(hw_uint<32*3>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
return v0 + v1 + v2;
}
static inline
hw_uint<32> avg(hw_uint<32*3>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
return (v0 + v1 + v2) / 3;
}
static inline
int conv_1_3_32(hw_uint<32*3>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
return v0 + v1 + v2;
}
static inline
int conv_1_3_16(hw_uint<16*3>& in) {
hw_uint<16> v0 = in.extract<0, 15>();
hw_uint<16> v1 = in.extract<16, 31>();
hw_uint<16> v2 = in.extract<32, 47>();
return v0 + v1 + v2;
}
static inline
hw_uint<32> blend_levels(hw_uint<32>& v0,
hw_uint<32>& v1,
hw_uint<32>& v2,
hw_uint<32>& v3
) {
return (v0 + v1 + v2 + v3) / 4;
}
static inline
hw_uint<32> blur_3_32(hw_uint<32*3>& in) {
return conv_1_3_32(in) / 3;
}
static inline
hw_uint<16> blur_3(hw_uint<16*3>& in) {
return conv_1_3_16(in) / 3;
}
static inline
int blur_27(hw_uint<16*27>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
hw_uint<32> v3 = in.extract<96, 127>();
hw_uint<32> v4 = in.extract<128, 159>();
hw_uint<32> v5 = in.extract<160, 191>();
hw_uint<32> v6 = in.extract<192, 223>();
hw_uint<32> v7 = in.extract<224, 255>();
hw_uint<32> v8 = in.extract<256, 287>();
return v0 + v1 + v2 +
v3 + v4 + v5 +
v6 + v7 + v8;
}
static inline
hw_uint<32> conv_3_3(hw_uint<32*9>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
hw_uint<32> v3 = in.extract<96, 127>();
hw_uint<32> v4 = in.extract<128, 159>();
hw_uint<32> v5 = in.extract<160, 191>();
hw_uint<32> v6 = in.extract<192, 223>();
hw_uint<32> v7 = in.extract<224, 255>();
hw_uint<32> v8 = in.extract<256, 287>();
//cout << "v0 = " << v0.to_int() << endl;
//cout << "v3 = " << v3.to_int() << endl;
//cout << "v6 = " << v6.to_int() << endl;
//assert(false);
return (v0 + v1 + v2 +
v3 + v4 + v5 +
v6 + v7 + v8).to_int();
}
static inline
hw_uint<32> reduce_gauss(hw_uint<32*9>& in) {
auto res = conv_3_3(in);
return res / 9;
}
static inline
hw_uint<32> mag_cu(hw_uint<32>& mx,
hw_uint<32>& my) {
return 65535 - (mx*mx + my*my);
}
static inline
hw_uint<32> sobel_my(hw_uint<192>& a) {
return 0;
}
static inline
hw_uint<32> sobel_mx(hw_uint<192>& a) {
//auto m1m1 = a.get<32, 0>();
//auto m10 = a.get<32, 1>();
//auto m10 = a.get<32, 2>();
//auto 1m1 = a.get<32, 3>();
//auto z1 = a.get<32, 4>();
//auto oneone = a.get<32, 5>();
hw_uint<32> r0 = a.get<32, 0>() - a.get<32, 1>();
hw_uint<32> r1 = 3*(a.get<32, 2>() - a.get<32, 3>());
hw_uint<32> r2 = a.get<32, 4>() - a.get<32, 5>();
return r0 + r1 + r2;
}
static inline
hw_uint<32> mag_dn2(hw_uint<32>& diff_d_0_c__0_value,
hw_uint<32>& diff_l_0_c__0_value,
hw_uint<32>& diff_r_0_c__0_value,
hw_uint<32>& diff_u_0_c__0_value) {
return diff_d_0_c__0_value +
diff_l_0_c__0_value +
diff_r_0_c__0_value +
diff_u_0_c__0_value;
}
static inline
hw_uint<64> id_unroll(hw_uint<64>& v) {
return v;
}
static inline
hw_uint<64> conv_3_3_unroll(hw_uint<384>& in) {
hw_uint<32> v0 = in.extract<0, 31>();
hw_uint<32> v1 = in.extract<32, 63>();
hw_uint<32> v2 = in.extract<64, 95>();
hw_uint<32> v3 = in.extract<96, 127>();
hw_uint<32> v4 = in.extract<128, 159>();
hw_uint<32> v5 = in.extract<160, 191>();
hw_uint<32> v6 = in.extract<192, 223>();
hw_uint<32> v7 = in.extract<224, 255>();
hw_uint<32> v8 = in.extract<256, 287>();
return (v0 + v1 + v2 +
v3 + v4 + v5 +
v6 + v7 + v8).to_int();
}
static inline
hw_uint<32> comp_r0(hw_uint<32>& a, hw_uint<32>& b) {
return a + b;
}
static inline
hw_uint<32> r1_comp(hw_uint<32>& a) {
return a*2;
}
static inline
hw_uint<32>
out_comp_dn2d(hw_uint<32>& a, hw_uint<32>& b, hw_uint<128>& c, hw_uint<128>& d) {
return a + b + c + d;
}
static inline
hw_uint<32>
contrived(hw_uint<32*3>& in, hw_uint<32*2>& b) {
return in.extract<0, 31>() +
in.extract<32, 63>() +
in.extract<64, 95>() +
b.extract<0, 31>() +
b.extract<32, 63>();
}