forked from timblechmann/nova-simd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simd_memory.hpp
499 lines (417 loc) · 12.2 KB
/
simd_memory.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// templated memory simd functions
// Copyright (C) 2010 Tim Blechmann <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef SIMD_MEMORY_HPP
#define SIMD_MEMORY_HPP
#include <cassert>
#include <cstring>
#include "vec.hpp"
#if defined(__GNUC__) && defined(NDEBUG)
#define always_inline inline __attribute__((always_inline))
#else
#define always_inline inline
#endif
namespace nova {
template <typename F>
inline void zerovec(F * dest, unsigned int n)
{
std::memset(dest, 0, n*sizeof(F));
}
template <typename F>
inline void setvec(F * dest, F f, unsigned int n)
{
assert(n);
do
*dest++ = f;
while (--n);
}
namespace detail
{
template <bool aligned, typename F>
inline void store_aligned(vec<F> const & value, F * dest)
{
if (aligned)
value.store_aligned(dest);
else
value.store(dest);
}
template <typename F, unsigned int n, bool aligned>
struct setvec
{
static const int offset = vec<F>::size;
static always_inline void mp_iteration(F * dst, vec<F> const & val)
{
store_aligned<aligned>(val, dst);
setvec<F, n-offset, aligned>::mp_iteration(dst+offset, val);
}
};
template <typename F, bool aligned>
struct setvec<F, 0, aligned>
{
static always_inline void mp_iteration(F * dst, vec<F> const & val)
{}
};
template <typename F, bool aligned>
inline void setvec_simd(F * dest, vec<F> const & val, unsigned int n)
{
const unsigned int offset = vec<F>::objects_per_cacheline;
unsigned int unroll = n / offset;
do
{
setvec<F, offset, aligned>::mp_iteration(dest, val);
dest += offset;
}
while (--unroll);
}
} /* namespace detail */
template <typename F>
inline void zerovec_simd(F * dest, unsigned int n)
{
vec<F> zero; zero.clear();
detail::setvec_simd<F, true>(dest, zero, n);
}
template <unsigned int n, typename F>
inline void zerovec_simd(F *dest)
{
vec<F> zero; zero.clear();
detail::setvec<F, n, true>::mp_iteration(dest, zero);
}
template <typename F>
inline void zerovec_na_simd(F * dest, unsigned int n)
{
vec<F> zero; zero.clear();
detail::setvec_simd<F, false>(dest, zero, n);
}
template <unsigned int n, typename F>
inline void zerovec_na_simd(F *dest)
{
vec<F> zero; zero.clear();
detail::setvec<F, n, false>::mp_iteration(dest, zero);
}
template <typename F>
inline void setvec_simd(F * dest, F f, unsigned int n)
{
vec<F> val(f);
detail::setvec_simd<F, true>(dest, val, n);
}
template <unsigned int n, typename F>
inline void setvec_simd(F *dest, F f)
{
vec<F> val(f);
detail::setvec<F, n, true>::mp_iteration(dest, val);
}
template <typename F>
inline void setvec_na_simd(F * dest, F f, unsigned int n)
{
vec<F> val(f);
detail::setvec_simd<F, false>(dest, val, n);
}
template <unsigned int n, typename F>
inline void setvec_na_simd(F *dest, F f)
{
vec<F> val(f);
detail::setvec<F, n, false>::mp_iteration(dest, val);
}
namespace detail
{
template <typename F, unsigned int n>
struct set_ramp
{
static const int offset = vec<F>::size;
static always_inline void slope_mp_iteration(F * dst, vec<F> & vbase, vec<F> const & vslope)
{
vbase.store_aligned(dst);
vbase += vslope;
set_ramp<F, n-offset>::slope_mp_iteration(dst+offset, vbase, vslope);
}
static always_inline void exp_mp_iteration(F * dst, vec<F> & vbase, vec<F> const & vcurve)
{
vbase.store_aligned(dst);
vbase *= vcurve;
set_ramp<F, n-offset>::exp_mp_iteration(dst+offset, vbase, vcurve);
}
};
template <typename F>
struct set_ramp<F, 0>
{
static always_inline void slope_mp_iteration(F * dst, vec<F> & vbase, vec<F> const & vslope)
{}
static always_inline void exp_mp_iteration(F * dst, vec<F> & vbase, vec<F> const & curve)
{}
};
} /* namespace detail */
template <typename F>
inline void set_slope_vec(F * dest, F f, F slope, unsigned int n)
{
assert(n);
do {
*dest++ = f; f += slope;
} while (--n);
}
template <typename F>
inline void set_slope_vec_simd(F * dest, F f, F slope, unsigned int n)
{
vec<F> vbase, vslope;
vbase.set_slope(f, slope);
vslope.set_vec(vec<F>::size * slope);
unsigned int unroll = n / vec<F>::objects_per_cacheline;
do
{
detail::set_ramp<F, vec<F>::objects_per_cacheline>::slope_mp_iteration(dest, vbase, vslope);
dest += vec<F>::objects_per_cacheline;
} while(--unroll);
}
template <typename F>
inline void set_exp_vec(F * dest, F f, F curve, unsigned int n)
{
assert(n);
do {
*dest++ = f; f *= curve;
} while (--n);
}
namespace detail {
template <int Exponent, typename Type>
class pow_i
{
public:
static Type run(Type const & base)
{
return base * pow_i<Exponent - 1, Type>::run(base);
}
};
template <typename Type>
class pow_i<1, Type>
{
public:
static Type run(Type const & base)
{
return base;
}
};
template <size_t Exponent, typename Type>
Type ipow(Type const & base)
{
return pow_i<Exponent, Type>::run(base);
}
}
template <typename F>
inline void set_exp_vec_simd(F * dest, F f, F curve, unsigned int n)
{
vec<F> vbase, vcurve(detail::ipow<vec<F>::size, F>(curve));
vbase.set_exp(f, curve);
unsigned int unroll = n / vec<F>::objects_per_cacheline;
do
{
detail::set_ramp<F, vec<F>::objects_per_cacheline>::exp_mp_iteration(dest, vbase, vcurve);
dest += vec<F>::objects_per_cacheline;
} while(--unroll);
}
template <typename F>
inline void copyvec(F * dest, const F * src, unsigned int n)
{
std::memcpy(dest, src, n*sizeof(F));
}
namespace detail {
template <typename F, bool src_aligned, bool dst_aligned, unsigned int n>
struct copyvec
{
static const int offset = vec<F>::size;
static always_inline void mp_iteration(F * dst, const F * src)
{
vec<F> val;
if (src_aligned)
val.load_aligned(src);
else
val.load(src);
mp_iteration(dst, src + offset, val);
}
static always_inline void mp_iteration(F * dst, const F * src, vec<F> const & loaded_value)
{
vec<F> val;
if (src_aligned)
val.load_aligned(src);
else
val.load(src);
store_aligned<dst_aligned>(loaded_value, dst);
copyvec<F, src_aligned, dst_aligned, n-offset>::mp_iteration(dst+offset, src+offset, val);
}
};
template <typename F, bool src_aligned, bool dst_aligned>
struct copyvec<F, src_aligned, dst_aligned, 0>
{
static always_inline void mp_iteration(F * dst, const F * src, vec<F> loaded_value)
{}
};
}
#define COPYVEC_FUNCTION(name, src_aligned, dst_aligned) \
template <typename F> \
inline void name##_simd(F * dest, const F * src, unsigned int n) \
{ \
const int per_loop = vec<F>::objects_per_cacheline; \
n /= per_loop; \
do \
{ \
detail::copyvec<F, src_aligned, dst_aligned, per_loop>::mp_iteration(dest, src); \
dest += per_loop; src += per_loop; \
} \
while (--n); \
} \
\
template <unsigned int n, typename F> \
inline void name##_simd(F * dest, const F * src) \
{ \
detail::copyvec<F, src_aligned, dst_aligned, n>::mp_iteration(dest, src); \
}
COPYVEC_FUNCTION(copyvec_aa, true, true)
COPYVEC_FUNCTION(copyvec_na, false, true)
COPYVEC_FUNCTION(copyvec_an, true, false)
COPYVEC_FUNCTION(copyvec_nn, false, false)
template <typename F>
inline void copyvec_simd(F * dest, const F * src, unsigned int n)
{
copyvec_aa_simd(dest, src, n);
}
template <unsigned int n, typename F>
inline void copyvec_simd(F * dest, const F * src)
{
copyvec_aa_simd<n, F>(dest, src);
}
template <typename F>
inline void addvec(F * out, const F * in, unsigned int n)
{
do {
*out++ += *in++;
} while (--n);
}
template <typename F>
inline void addvec(F * out, const F in, unsigned int n)
{
do {
*out++ += in;
} while (--n);
}
template <typename F>
inline void addvec(F * out, const F in, const F slope, unsigned int n)
{
do {
*out++ += in; in += slope;
} while (--n);
}
namespace detail
{
template <typename F, unsigned int n>
struct addvec
{
static const int offset = vec<F>::size;
static always_inline void mp_iteration(F * dst, const F * src)
{
vec<F> v1, v2;
v1.load_aligned(dst);
v2.load_aligned(src);
v1 += v2;
v1.store_aligned(dst);
addvec<F, n-offset>::mp_iteration(dst+offset, src+offset);
}
static always_inline void mp_iteration(F * dst, vec<F> const & in)
{
vec<F> v1;
v1.load_aligned(dst);
v1 += in;
v1.store_aligned(dst);
addvec<F, n-offset>::mp_iteration(dst+offset, in);
}
static always_inline void mp_iteration(F * dst, vec<F> & in, vec<F> const & vslope)
{
vec<F> v1;
v1.load_aligned(dst);
v1 += in;
v1.store_aligned(dst);
in += vslope;
addvec<F, n-offset>::mp_iteration(dst+offset, in, vslope);
}
};
template <typename F>
struct addvec<F, 0>
{
static always_inline void mp_iteration(F * dst, const F * src)
{}
static always_inline void mp_iteration(F * dst, vec<F> const & in)
{}
static always_inline void mp_iteration(F * dst, vec<F> & in, vec<F> const & vslope)
{}
};
}
template <typename F>
inline void addvec_simd(F * out, const F * in, unsigned int n)
{
const int per_loop = vec<F>::objects_per_cacheline;
n /= per_loop;
do
{
detail::addvec<F, per_loop>::mp_iteration(out, in);
out += per_loop; in += per_loop;
}
while (--n);
}
template <typename F>
inline void addvec_simd(F * out, const F in, unsigned int n)
{
const int per_loop = vec<F>::objects_per_cacheline;
vec<F> vin(in);
n /= per_loop;
do
{
detail::addvec<F, per_loop>::mp_iteration(out, vin);
out += per_loop;
}
while (--n);
}
template <typename F>
inline void addvec_simd(F * out, const F in, const F slope, unsigned int n)
{
const int per_loop = vec<F>::objects_per_cacheline;
vec<F> vin; vin.set_slope(in, slope);
vec<F> vslope; vslope.set(slope * vec<F>::size);
n /= per_loop;
do
{
detail::addvec<F, per_loop>::mp_iteration(out, vin, vslope);
out += per_loop;
}
while (--n);
}
template <unsigned int n, typename F>
inline void addvec_simd(F * out, const F * in)
{
detail::addvec<F, n>::mp_iteration(out, in);
}
template <unsigned int n, typename F>
inline void addvec_simd(F * out, const F in)
{
vec<F> vin(in);
detail::addvec<F, n>::mp_iteration(out, vin);
}
template <unsigned int n, typename F>
inline void addvec_simd(F * out, const F in, const F slope)
{
vec<F> vin; vin.set_slope(in, slope);
vec<F> vslope; vslope.set(slope * vec<F>::size);
detail::addvec<F, n>::mp_iteration(out, vin, vslope);
}
} /* namespace nova */
#undef always_inline
#endif /* SIMD_MEMORY_HPP */