-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintrintypes.h
More file actions
executable file
·179 lines (140 loc) · 7.37 KB
/
intrintypes.h
File metadata and controls
executable file
·179 lines (140 loc) · 7.37 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
/*************************************************************************************
cpl - cross-platform library - v. 0.1.0.
Copyright (C) 2016 Janus Lynggaard Thorborg (www.jthorborg.com)
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 3 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. If not, see <http://www.gnu.org/licenses/>.
See \licenses\ for additional details on licenses associated with this program.
**************************************************************************************
file:intrintypes.h
Emulations of vector-types as classes, with template arguments being
the instruction set and fundamental type. Thus, with nice compiler-optimizations,
these encapsulating classes should compile down to only the raw instruction,
so your app supports all the new fancy instructions without you having to
rewrite/duplicate your code all the time.
*************************************************************************************/
#ifndef INTRINTYPES_H
#define INTRINTYPES_H
#include "Types.h"
namespace cpl
{
namespace simd
{
enum class iset
{
avx,
avx2,
sse2
};
template<iset instructionSet, typename scalar>
class var;
template<iset instructionSet, class scalar>
struct rank_of
{
static const std::size_t value = var<instructionSet, scalar>::elements;
};
/*
The general example of what the class var should implement.
I'm too lazy to implement all the architechtures right now, adding them as i need them..
Feel free to send them to me.
*/
template<>
struct var<iset::avx, float>
{
typedef Types::v8sf val_type;
typedef var<iset::avx, float> type;
typedef float scalar;
static const std::size_t size = sizeof(val_type);
static const std::size_t elements = sizeof(val_type) / sizeof(scalar);
type & operator =(const type & right) { value = right.value; return *this; }
type & operator =(val_type right) { value = right; return *this; }
var(val_type val) : value(val) {}
var() {};
type operator * (type right) { return _mm256_mul_ps(value, right); }
type operator + (type right) { return _mm256_mul_ps(value, right); }
type operator - (type right) { return _mm256_sub_ps(value, right); }
type operator / (type right) { return _mm256_div_ps(value, right); }
type operator & (type right) { return _mm256_and_ps(value, right); }
type operator | (type right) { return _mm256_or_ps(value, right); }
type & operator *= (type right) { value = _mm256_mul_ps(value, right); return *this; }
type & operator += (type right) { value = _mm256_mul_ps(value, right); return *this; }
type & operator -= (type right) { value = _mm256_sub_ps(value, right); return *this; }
type & operator /= (type right) { value = _mm256_div_ps(value, right); return *this; }
type & operator &= (type right) { value = _mm256_and_ps(value, right); return *this; }
type & operator |= (type right) { value = _mm256_or_ps(value, right); return *this; }
type operator == (type right) { return _mm256_cmp_ps(value, right, _CMP_EQ_UQ); }
type operator < (type right) { return _mm256_cmp_ps(value, right, _CMP_LT_OQ); }
operator val_type() { return value; }
static val_type fma(val_type a, val_type b, val_type c) { return a * b + c; }
val_type value;
};
template<>
struct var<iset::avx2, float>
{
typedef Types::v8sf val_type;
typedef var<iset::avx2, float> type;
typedef float scalar;
static const std::size_t size = sizeof(val_type);
static const std::size_t elements = sizeof(val_type) / sizeof(scalar);
type & operator =(const type & right) { value = right.value; return *this; }
type & operator =(val_type right) { value = right; return *this; }
var(val_type val) : value(val) {}
var() {};
type operator * (type right) { return _mm256_mul_ps(value, right); }
type operator + (type right) { return _mm256_mul_ps(value, right); }
type operator - (type right) { return _mm256_sub_ps(value, right); }
type operator / (type right) { return _mm256_div_ps(value, right); }
type operator & (type right) { return _mm256_and_ps(value, right); }
type operator | (type right) { return _mm256_or_ps(value, right); }
type & operator *= (type right) { value = _mm256_mul_ps(value, right); return *this; }
type & operator += (type right) { value = _mm256_mul_ps(value, right); return *this; }
type & operator -= (type right) { value = _mm256_sub_ps(value, right); return *this; }
type & operator /= (type right) { value = _mm256_div_ps(value, right); return *this; }
type & operator &= (type right) { value = _mm256_and_ps(value, right); return *this; }
type & operator |= (type right) { value = _mm256_or_ps(value, right); return *this; }
type operator == (type right) { return _mm256_cmp_ps(value, right, _CMP_EQ_UQ); }
type operator < (type right) { return _mm256_cmp_ps(value, right, _CMP_LT_OQ); }
operator val_type() { return value; }
static val_type fma(val_type a, val_type b, val_type c) { /*return _mm256_fmadd(a,b,c);*/ return a; }
val_type value;
};
template<>
struct var<iset::avx, double>
{
typedef Types::v4sd val_type;
typedef var<iset::avx, double> type;
typedef double scalar;
static const std::size_t size = sizeof(val_type);
static const std::size_t elements = sizeof(val_type) / sizeof(scalar);
type & operator =(const type & right) { value = right.value; return *this; }
type & operator =(val_type right) { value = right; return *this; }
var(val_type val) : value(val) {}
var() {};
type operator * (type right) { return _mm256_mul_pd(value, right); }
type operator + (type right) { return _mm256_mul_pd(value, right); }
type operator - (type right) { return _mm256_sub_pd(value, right); }
type operator / (type right) { return _mm256_div_pd(value, right); }
type operator & (type right) { return _mm256_and_pd(value, right); }
type operator | (type right) { return _mm256_or_pd(value, right); }
type & operator *= (type right) { value = _mm256_mul_pd(value, right); return *this; }
type & operator += (type right) { value = _mm256_mul_pd(value, right); return *this; }
type & operator -= (type right) { value = _mm256_sub_pd(value, right); return *this; }
type & operator /= (type right) { value = _mm256_div_pd(value, right); return *this; }
type & operator &= (type right) { value = _mm256_and_pd(value, right); return *this; }
type & operator |= (type right) { value = _mm256_or_pd(value, right); return *this; }
type operator == (type right) { return _mm256_cmp_pd(value, right, _CMP_EQ_UQ); }
type operator < (type right) { return _mm256_cmp_pd(value, right, _CMP_LT_OQ); }
operator val_type() { return value; }
static val_type fma(val_type a, val_type b, val_type c) { return a * b + c; }
val_type value;
};
} // simd
}; // cpl
#endif