-
Notifications
You must be signed in to change notification settings - Fork 7
/
TableReader.hpp
401 lines (316 loc) · 13.5 KB
/
TableReader.hpp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#ifndef TABLEREADER_HPP
#define TABLEREADER_HPP
#include "SIMDSupport.hpp"
#include "Interpolation.hpp"
#include <algorithm>
// Enumeration of edge types
enum class EdgeMode { ZeroPad, Extend, Wrap, Fold, Mirror, Extrapolate };
// Base class for table fetchers
// Implementations
// - Must provide: T operator()(intptr_t idx) - which does the fetching of values
// - Adaptors may also provide: template <class U, V> void split(U position, intptr_t& idx, V& fract, int N)
// - which generates the idx and fractional interpolation values and may additionally constrain them
// - intptr_t limit() - which should return the highest valid position for bounds etc.
// - void prepare(InterpType interpolation) - which prepares the table (e.g. for extrapolation) if necessary
template <class T>
struct table_fetcher
{
typedef T fetch_type;
table_fetcher(intptr_t length, double scale_val) : size(length), scale(scale_val) {}
template <class U, class V>
void split(U position, intptr_t& idx, V& fract, int /* N */)
{
idx = static_cast<intptr_t>(std::floor(position));
fract = static_cast<V>(position - static_cast<V>(idx));
}
intptr_t limit() { return size - 1; }
void prepare(InterpType /* interpolation */) {}
const intptr_t size;
const double scale;
};
// Adaptors to add edge functionality
template <class T>
struct table_fetcher_zeropad : T
{
table_fetcher_zeropad(const T& base) : T(base) {}
typename T::fetch_type operator()(intptr_t idx)
{
return (idx < 0 || idx >= T::size) ? typename T::fetch_type(0) : T::operator()(idx);
}
};
template <class T>
struct table_fetcher_extend : T
{
table_fetcher_extend(const T& base) : T(base) {}
typename T::fetch_type operator()(intptr_t idx)
{
return T::operator()(std::min(std::max(idx, static_cast<intptr_t>(0)), T::size - 1));
}
};
template <class T>
struct table_fetcher_wrap : T
{
table_fetcher_wrap(const T& base) : T(base) {}
typename T::fetch_type operator()(intptr_t idx)
{
idx = idx < 0 ? T::size - 1 + ((idx + 1) % T::size) : idx % T::size;
return T::operator()(idx);
}
intptr_t limit() { return T::size; }
};
template <class T>
struct table_fetcher_fold : T
{
table_fetcher_fold(const T& base)
: T(base), fold_size(T::size > 1 ? (T::size - 1) * 2 : 1) {}
typename T::fetch_type operator()(intptr_t idx)
{
idx = std::abs(idx) % fold_size;
idx = idx > T::size - 1 ? fold_size - idx : idx;
return T::operator()(idx);
}
intptr_t fold_size;
};
template <class T>
struct table_fetcher_mirror : T
{
table_fetcher_mirror(const T& base) : T(base) {}
typename T::fetch_type operator()(intptr_t idx)
{
idx = (idx < 0 ? -(idx + 1) : idx) % (T::size * 2);
idx = idx > T::size - 1 ? ((T::size * 2) - 1) - idx : idx;
return T::operator()(idx);
}
};
template <class T>
struct table_fetcher_extrapolate : T
{
table_fetcher_extrapolate(const T& base) : T(base) {}
typename T::fetch_type operator()(intptr_t idx)
{
return (idx >= 0 && idx < T::size) ? T::operator()(idx) : (idx < 0 ? ends[0] : ends[1]);
}
template <class U, class V>
void split(U position, intptr_t& idx, V& fract, int N)
{
U constrained = std::max(std::min(position, U(T::size - (N ? 2 : 1))), U(0));
idx = static_cast<intptr_t>(std::floor(constrained));
fract = static_cast<V>(position - static_cast<V>(idx));
}
void prepare(InterpType interpolation)
{
using fetch_type = typename T::fetch_type;
auto beg = [&](intptr_t idx) { return T::operator()(idx); };
auto end = [&](intptr_t idx) { return T::operator()(T::size - (idx + 1)); };
if (T::size >= 4 && (interpolation != InterpType::None) && (interpolation != InterpType::Linear))
{
ends[0] = cubic_lagrange_interp<fetch_type>()(fetch_type(-2), beg(0), beg(1), beg(2), beg(3));
ends[1] = cubic_lagrange_interp<fetch_type>()(fetch_type(-2), end(0), end(1), end(2), end(3));
}
else if (T::size >= 2)
{
ends[0] = linear_interp<fetch_type>()(fetch_type(-1), beg(0), beg(1));
ends[1] = linear_interp<fetch_type>()(fetch_type(-1), end(0), end(1));
}
else
ends[0] = ends[1] = (T::size > 0 ? T::operator()(0) : fetch_type(0));
}
typename T::fetch_type ends[2];
};
// Adaptor to take a fetcher and bound the input (can be added to the above edge behaviours)
template <class T>
struct table_fetcher_bound : T
{
table_fetcher_bound(const T& base) : T(base) {}
template <class U, class V>
void split(U position, intptr_t& idx, V& fract, int N)
{
position = std::max(std::min(position, U(T::limit())), U(0));
T::split(position, idx, fract, N);
}
};
// Generic interpolation readers
template <class T, class U, class V, class Table, typename Interp>
struct interp_2_reader
{
interp_2_reader(Table fetcher) : fetch(fetcher) {}
T operator()(const V*& positions)
{
typename T::scalar_type fract_array[T::size];
typename U::scalar_type array[T::size * 2];
for (int i = 0; i < T::size; i++)
{
intptr_t idx;
fetch.split(*positions++, idx, fract_array[i], 2);
array[i] = fetch(idx + 0);
array[i + T::size] = fetch(idx + 1);
}
const T y0 = U(array);
const T y1 = U(array + T::size);
return interpolate(T(fract_array), y0, y1);
}
Table fetch;
Interp interpolate;
};
template <class T, class U, class V, class Table, typename Interp>
struct interp_4_reader
{
interp_4_reader(Table fetcher) : fetch(fetcher) {}
T operator()(const V*& positions)
{
typename T::scalar_type fract_array[T::size];
typename U::scalar_type array[T::size * 4];
for (int i = 0; i < T::size; i++)
{
intptr_t idx;
fetch.split(*positions++, idx, fract_array[i], 4);
array[i] = fetch(idx - 1);
array[i + T::size] = fetch(idx + 0);
array[i + T::size * 2] = fetch(idx + 1);
array[i + T::size * 3] = fetch(idx + 2);
}
const T y0 = U(array);
const T y1 = U(array + T::size);
const T y2 = U(array + (T::size * 2));
const T y3 = U(array + (T::size * 3));
return interpolate(T(fract_array), y0, y1, y2, y3);
}
Table fetch;
Interp interpolate;
};
// Readers with specific interpolation types
template <class T, class U, class V, class Table>
struct no_interp_reader
{
no_interp_reader(Table fetcher) : fetch(fetcher) {}
T operator()(const V*& positions)
{
typename U::scalar_type array[T::size];
for (int i = 0; i < T::size; i++)
{
typename T::scalar_type fract;
intptr_t idx;
fetch.split(*positions++, idx, fract, 0);
array[i] = fetch(idx);
}
return U(array);
}
Table fetch;
};
template <class T, class U, class V, class Table>
struct linear_reader : public interp_2_reader<T, U, V, Table, linear_interp<T>>
{
linear_reader(Table fetcher) : interp_2_reader<T, U, V, Table, linear_interp<T>>(fetcher) {}
};
template <class T, class U, class V, class Table>
struct cubic_bspline_reader : public interp_4_reader<T, U, V, Table, cubic_bspline_interp<T>>
{
cubic_bspline_reader(Table fetcher) : interp_4_reader<T, U, V, Table, cubic_bspline_interp<T>>(fetcher) {}
};
template <class T, class U, class V, class Table>
struct cubic_hermite_reader : public interp_4_reader<T, U, V, Table, cubic_hermite_interp<T>>
{
cubic_hermite_reader(Table fetcher) : interp_4_reader<T, U, V, Table, cubic_hermite_interp<T>>(fetcher) {}
};
template <class T, class U, class V, class Table>
struct cubic_lagrange_reader : public interp_4_reader<T, U, V, Table, cubic_lagrange_interp<T>>
{
cubic_lagrange_reader(Table fetcher) : interp_4_reader<T, U, V, Table, cubic_lagrange_interp<T>>(fetcher) {}
};
// Reading loop
template <class T, class U, class V, class Table, template <class W, class X, class Y, class Tb> class Reader>
void table_read_loop(Table fetcher, typename T::scalar_type *out, const V *positions, intptr_t n_samps, double mul)
{
Reader<T, U, V, Table> reader(fetcher);
T *v_out = reinterpret_cast<T *>(out);
T scale = static_cast<typename U::scalar_type>(mul * reader.fetch.scale);
for (intptr_t i = 0; i < (n_samps / T::size); i++)
*v_out++ = scale * reader(positions);
}
// Template to determine vector/scalar types
template <template <class T, class U, class V, class Tb> class Reader, class Table, class W, class X>
void table_read(Table fetcher, W *out, const X *positions, intptr_t n_samps, double mul)
{
typedef typename Table::fetch_type fetch_type;
const int vec_size = SIMDLimits<W>::max_size;
intptr_t n_vsample = (n_samps / vec_size) * vec_size;
table_read_loop<SIMDType<W, vec_size>, SIMDType<fetch_type, vec_size>, X, Table, Reader>(fetcher, out, positions, n_vsample, mul);
table_read_loop<SIMDType<W, 1>, SIMDType<fetch_type, 1>, X, Table, Reader>(fetcher, out + n_vsample, positions + n_vsample, n_samps - n_vsample, mul);
}
// Main reading call that switches between different types of interpolation
template <class T, class U, class Table>
void table_read(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp)
{
fetcher.prepare(interp);
switch(interp)
{
case InterpType::None: table_read<no_interp_reader>(fetcher, out, positions, n_samps, mul); break;
case InterpType::Linear: table_read<linear_reader>(fetcher, out, positions, n_samps, mul); break;
case InterpType::CubicHermite: table_read<cubic_hermite_reader>(fetcher, out,positions, n_samps, mul); break;
case InterpType::CubicLagrange: table_read<cubic_lagrange_reader>(fetcher, out, positions, n_samps, mul); break;
case InterpType::CubicBSpline: table_read<cubic_bspline_reader>(fetcher, out, positions, n_samps, mul); break;
}
}
// Reading calls to add adaptors to the basic fetcher
template <class T, class U, class Table>
void table_read_optional_bound(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
if (bound)
{
table_fetcher_bound<Table> fetch(fetcher);
table_read(fetch, out, positions, n_samps, mul, interp);
}
else
table_read(fetcher, out, positions, n_samps, mul, interp);
}
template <class T, class U, class Table>
void table_read_zeropad(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_zeropad<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
template <class T, class U, class Table>
void table_read_extend(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_extend<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
template <class T, class U, class Table>
void table_read_wrap(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_wrap<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
template <class T, class U, class Table>
void table_read_fold(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_fold<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
template <class T, class U, class Table>
void table_read_mirror(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_mirror<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
template <class T, class U, class Table>
void table_read_extrapolate(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, bool bound)
{
table_fetcher_extrapolate<Table> fetch(fetcher);
table_read_optional_bound(fetch, out, positions, n_samps, mul, interp, bound);
}
// Main read call for variable edge behaviour
template <class T, class U, class Table>
void table_read_edges(Table fetcher, T *out, const U *positions, intptr_t n_samps, T mul, InterpType interp, EdgeMode edges, bool bound)
{
switch (edges)
{
case EdgeMode::ZeroPad: table_read_zeropad(fetcher, out, positions, n_samps, mul, interp, bound); break;
case EdgeMode::Extend: table_read_extend(fetcher, out, positions, n_samps, mul, interp, bound); break;
case EdgeMode::Wrap: table_read_wrap(fetcher, out, positions, n_samps, mul, interp, bound); break;
case EdgeMode::Fold: table_read_fold(fetcher, out, positions, n_samps, mul, interp, bound); break;
case EdgeMode::Mirror: table_read_mirror(fetcher, out, positions, n_samps, mul, interp, bound); break;
case EdgeMode::Extrapolate: table_read_extrapolate(fetcher, out, positions, n_samps, mul, interp, bound); break;
}
}
#endif /* TableReader_h */