forked from msupernaw/ATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIMD.hpp
545 lines (451 loc) · 14.1 KB
/
SIMD.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* File: SIMD.hpp
* Author: matthewsupernaw
*
* Created on July 2, 2015, 7:01 AM
*/
#ifndef SIMD_HPP
#define SIMD_HPP
#include <iostream>
#if (defined(_M_AMD64) || defined(_M_X64) || defined(__amd64)) && ! defined(__x86_64__)
#define __x86_64__ 1
#endif
// Find sse instruction set from compiler macros if SSE_INSTR_SET not defined
// Note: Not all compilers define these macros automatically
#ifndef SSE_INSTR_SET
#if defined ( __AVX2__ )
#define SSE_INSTR_SET 8
#elif defined ( __AVX__ )
#define SSE_INSTR_SET 7
#elif defined ( __SSE4_2__ )
#define SSE_INSTR_SET 6
#elif defined ( __SSE4_1__ )
#define SSE_INSTR_SET 5
#elif defined ( __SSSE3__ )
#define SSE_INSTR_SET 4
#elif defined ( __SSE3__ )
#define SSE_INSTR_SET 3
#elif defined ( __SSE2__ ) || defined ( __x86_64__ )
#define SSE_INSTR_SET 2
#elif defined ( __SSE__ )
#define SSE_INSTR_SET 1
#elif defined ( _M_IX86_FP ) // Defined in MS compiler on 32bits system. 1: SSE, 2: SSE2
#define SSE_INSTR_SET _M_IX86_FP
#else
#define SSE_INSTR_SET 0
#endif // instruction set defines
#endif // SSE_INSTR_SET
#if SSE_INSTR_SET > 7 // AVX2 and later
#ifdef __GNUC__
#include <x86intrin.h> // x86intrin.h includes header files for whatever instruction
// sets are specified on the compiler command line, such as:
// xopintrin.h, fma4intrin.h
#else
#include <immintrin.h> // MS version of immintrin.h covers AVX, AVX2 and FMA3
#endif // __GNUC__
#elif SSE_INSTR_SET == 7
#include <immintrin.h> // AVX
#elif SSE_INSTR_SET == 6
#include <nmmintrin.h> // SSE4.2
#elif SSE_INSTR_SET == 5
#include <smmintrin.h> // SSE4.1
#elif SSE_INSTR_SET == 4
#include <tmmintrin.h> // SSSE3
#elif SSE_INSTR_SET == 3
#include <pmmintrin.h> // SSE3
#elif SSE_INSTR_SET == 2
#include <emmintrin.h> // SSE2
#elif SSE_INSTR_SET == 1
#include <xmmintrin.h> // SSE
#endif
#if SSE_INSTR_SET > 7
#define USE_AVX
#endif
#ifndef USE_AVX
#if SSE_INSTR_SET > 0
#define USE_SSE
#endif
#endif
#include <memory>
//
//namespace util {
//
// /**
// * STL-compliant allocator that allocates aligned memory.
// * @tparam T Type of the element to allocate.
// * @tparam Alignment Alignment of the allocation, e.g. 16.
// * @ingroup AlignedAllocator
// */
// template <class T, size_t Alignment>
// struct aligned_allocator
// : public std::allocator<T> // Inherit construct(), destruct() etc.
// {
//#if 0
// typedef size_t size_type;
// typedef ptrdiff_t difference_type;
// typedef T* pointer;
// typedef const T* const_pointer;
// typedef T& reference;
// typedef const T& const_reference;
// typedef T value_type;
//#endif
// typedef typename std::allocator<T>::size_type size_type;
// typedef typename std::allocator<T>::pointer pointer;
// typedef typename std::allocator<T>::const_pointer const_pointer;
//
// /// Defines an aligned allocator suitable for allocating elements of type
// /// @c U.
//
// template <class U>
// struct rebind {
// typedef aligned_allocator<U, Alignment> other;
// };
//
// /// Default-constructs an allocator.
//
// aligned_allocator() throw () {
// }
//
// /// Copy-constructs an allocator.
//
// aligned_allocator(const aligned_allocator & other) throw ()
// : std::allocator<T > (other) {
// }
//
// /// Convert-constructs an allocator.
//
// template <class U >
// aligned_allocator(const aligned_allocator<U, Alignment>&) throw () {
// }
//
// /// Destroys an allocator.
//
// ~aligned_allocator() throw () {
// }
//
// /// Allocates @c n elements of type @c T, aligned to a multiple of
// /// @c Alignment.
//
// pointer allocate(size_type n) {
// return allocate(n, const_pointer(0));
// }
//
// /// Allocates @c n elements of type @c T, aligned to a multiple of
// /// @c Alignment.
//
// pointer allocate(size_type n, const_pointer /* hint */) {
// void *p;
//#ifndef _WIN32
// if (posix_memalign(&p, Alignment, n * sizeof (T)) != 0)
// p = NULL;
//#else
// p = _aligned_malloc(n * sizeof (T), Alignment);
//#endif
// if (!p)
// throw std::bad_alloc();
// return static_cast<pointer> (p);
// }
//
// /// Frees the memory previously allocated by an aligned allocator.
//
// void deallocate(pointer p, size_type /* n */) {
//#ifndef _WIN32
// free(p);
//#else
// _aligned_free(p);
//#endif
// }
// };
//
// /**
// * Checks whether two aligned allocators are equal. Two allocators are equal
// * if the memory allocated using one allocator can be deallocated by the other.
// * @returns Always @c true.
// * @ingroup AlignedAllocator
// */
// template <class T1, size_t A1, class T2, size_t A2>
// bool operator ==(const aligned_allocator<T1, A1> &, const aligned_allocator<T2, A2> &) {
// return true;
// }
//
// /**
// * Checks whether two aligned allocators are not equal. Two allocators are equal
// * if the memory allocated using one allocator can be deallocated by the other.
// * @returns Always @c false.
// * @ingroup AlignedAllocator
// */
// template <class T1, size_t A1, class T2, size_t A2>
// bool operator !=(const aligned_allocator<T1, A1> &, const aligned_allocator<T2, A2> &) {
// return false;
// }
//
//} // namespace util
//
namespace simd {
class vector4f;
class vector2d;
template <class T>
struct simd_traits {
typedef T type;
static const size_t size = 1;
static const bool simd_available = false;
};
#ifdef USE_SSE
template <>
struct simd_traits<float> {
typedef vector4f type;
static const size_t size = 4;
static const bool simd_available = true;
};
template <>
struct simd_traits<double> {
typedef vector2d type;
static const size_t size = 2;
static const bool simd_available = true;
};
#elif USE_AVX
template <>
struct simd_traits<float> {
typedef vector8f type;
static const size_t size = 8;
};
template <>
struct simd_traits<double> {
typedef vector4d type;
static const size_t size = 4;
};
#endif
template <class X>
struct simd_vector_traits {
typedef X value_type;
};
template <>
struct simd_vector_traits<vector4f> {
typedef float value_type;
};
template <>
struct simd_vector_traits<vector2d> {
typedef double value_type;
};
template <class X>
class simd_vector {
public:
typedef typename simd_vector_traits<X>::value_type value_type;
// downcast operators so we can call methods in the inheriting classes
inline X& operator()() {
return *static_cast<X*> (this);
}
inline const X& operator()() const {
return *static_cast<const X*> (this);
}
// Additional assignment operators
inline X& operator+=(const X& rhs) {
(*this)() = (*this)() + rhs;
return (*this)();
}
inline X& operator+=(const value_type& rhs) {
(*this)() = (*this)() + X(rhs);
return (*this)();
}
inline X& operator-=(const X& rhs) {
(*this)() = (*this)() - rhs;
return (*this)();
}
inline X& operator-=(const value_type& rhs) {
(*this)() = (*this)() - X(rhs);
return (*this)();
}
inline X& operator*=(const X& rhs) {
(*this)() = (*this)() * rhs;
return (*this)();
}
inline X& operator*=(const value_type& rhs) {
(*this)() = (*this)() * X(rhs);
return (*this)();
}
inline X& operator/=(const X& rhs) {
(*this)() = (*this)() / rhs;
return (*this)();
}
inline X& operator/=(const value_type& rhs) {
(*this)() = (*this)() / X(rhs);
return (*this)();
}
// Increment operators
inline X operator++(int) {
X tmp = (*this)();
(*this) += value_type(1);
return tmp;
}
inline X& operator++() {
(*this)() += value_type(1);
return (*this)();
}
inline X operator--(int) {
X tmp = (*this)();
(*this) -= value_type(1);
return tmp;
}
inline X& operator--() {
(*this)() -= value_type(1);
return (*this)();
}
protected:
// Ensure only inheriting classes can instantiate / copy / assign simd_vector.
// Avoids incomplete copy / assignment from client code.
inline simd_vector() {
}
inline ~simd_vector() {
}
inline simd_vector(const simd_vector&) {
}
inline simd_vector& operator=(const simd_vector&) {
return *this;
}
};
template <class X>
inline const simd_vector<X> operator+(const simd_vector<X>& lhs,
const typename simd_vector_traits<X>::type& rhs) {
return lhs() + X(rhs);
}
template <class X>
inline const simd_vector<X> operator+(const typename simd_vector_traits<X>::type& lhs,
const simd_vector<X>& rhs) {
return X(lhs) + rhs();
}
template <class X>
inline const simd_vector<X> operator-(const simd_vector<X>& lhs,
const typename simd_vector_traits<X>::type& rhs) {
return lhs() - X(rhs);
}
template <class X>
inline const simd_vector<X> operator-(const typename simd_vector_traits<X>::type& lhs,
const simd_vector<X>& rhs) {
return X(lhs) - rhs();
}
template <class X>
inline const simd_vector<X> operator*(const simd_vector<X>& lhs,
const typename simd_vector_traits<X>::type& rhs) {
return lhs() * X(rhs);
}
template <class X>
inline const simd_vector<X> operator*(const typename simd_vector_traits<X>::type& lhs,
const simd_vector<X>& rhs) {
return X(lhs) * rhs();
}
template <class X>
inline const simd_vector<X> operator/(const simd_vector<X>& lhs,
const typename simd_vector_traits<X>::type& rhs) {
return lhs() / X(rhs);
}
template <class X>
inline const simd_vector<X> operator/(const typename simd_vector_traits<X>::type& lhs,
const simd_vector<X>& rhs) {
return X(lhs) / rhs();
}
class vector4f : public simd_vector<vector4f> {
public:
inline vector4f() {
}
inline vector4f(float f) : m_value(_mm_set1_ps(f)) {
}
inline vector4f(float f0, float f1, float f2, float f3) : m_value(_mm_setr_ps(f0, f1, f2, f3)) {
}
inline vector4f(const __m128& rhs) : m_value(rhs) {
}
inline vector4f& operator=(const __m128& rhs) {
m_value = rhs;
return *this;
}
inline operator __m128() const {
return m_value;
}
inline vector4f& load_a(const float* src) {
m_value = _mm_load_ps(src);
return *this;
}
inline vector4f& load_u(const float* src) {
m_value = _mm_loadu_ps(src);
return *this;
}
inline void store_a(float* dst) const {
_mm_store_ps(dst, m_value);
}
inline void store_u(float* dst) const {
_mm_storeu_ps(dst, m_value);
}
private:
__m128 m_value;
};
inline const vector4f operator+(const vector4f& lhs, const vector4f& rhs) {
return _mm_add_ps(lhs, rhs);
}
inline const vector4f operator-(const vector4f& lhs, const vector4f& rhs) {
return _mm_sub_ps(lhs, rhs);
}
inline const vector4f operator*(const vector4f& lhs, const vector4f& rhs) {
return _mm_mul_ps(lhs, rhs);
}
inline const vector4f operator/(const vector4f& lhs, const vector4f& rhs) {
return _mm_div_ps(lhs, rhs);
}
std::ostream& operator<<(std::ostream& out, vector4f& v) {
float result [4];
_mm_store_ps(result, v);
out << "{" << result[0] << "," << result[1] << "," << result[2] << "," << result[3] << "}";
return out;
}
class vector2d : public simd_vector<vector2d> {
public:
inline vector2d() {
}
inline vector2d(double f) : m_value(_mm_set1_pd(f)) {
}
inline vector2d(double f0, double f1) : m_value(_mm_setr_pd(f0, f1)) {
}
inline vector2d(const __m128d& rhs) : m_value(rhs) {
}
inline vector2d& operator=(const __m128d& rhs) {
m_value = rhs;
return *this;
}
inline operator __m128d() const {
return m_value;
}
inline vector2d& load_a(const double* src) {
m_value = _mm_load_pd(src);
return *this;
}
inline vector2d& load_u(const double* src) {
m_value = _mm_loadu_pd(src);
return *this;
}
inline void store_a(double* dst) const {
_mm_store_pd(dst, m_value);
}
inline void store_u(double* dst) const {
_mm_storeu_pd(dst, m_value);
}
private:
__m128d m_value;
};
inline const vector2d operator+(const vector2d& lhs, const vector2d& rhs) {
return _mm_add_pd(lhs, rhs);
}
inline const vector2d operator-(const vector2d& lhs, const vector2d& rhs) {
return _mm_sub_pd(lhs, rhs);
}
inline const vector2d operator*(const vector2d& lhs, const vector2d& rhs) {
return _mm_mul_pd(lhs, rhs);
}
inline const vector2d operator/(const vector2d& lhs, const vector2d& rhs) {
return _mm_div_pd(lhs, rhs);
}
std::ostream& operator<<(std::ostream& out, vector2d& v) {
double result [2];
_mm_store_pd(result, v);
out << "{" << result[0] << "," << result[1] << "}";
return out;
}
}
#endif /* SIMD_HPP */