-
Notifications
You must be signed in to change notification settings - Fork 0
/
vclass_simdtype.h
254 lines (198 loc) · 4.92 KB
/
vclass_simdtype.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
#ifndef __CLASS_SIMDTYPE__
#define __CLASS_SIMDTYPE__
namespace VCLASS_SIMDTYPE
{
///////////////////////////////////////////
// SIMD CLASS (Same as VCLASS)
///////////////////////////////////////////
class simd_type
{
public:
inline simd_type() {}
inline simd_type(float *pVec)
: xyzw(_mm_load_ps(pVec))
{ }
inline simd_type(float f)
: xyzw(_mm_set_ps1(f))
{ }
inline simd_type(const __m128& qword)
: xyzw(qword)
{ }
inline simd_type(float x, float y, float z, float w)
: xyzw(_mm_set_ps(x, y, z, w))
{ }
inline simd_type(const simd_type& copy)
: xyzw(copy.xyzw)
{ }
inline simd_type& operator= (const simd_type& copy)
{
xyzw = copy.xyzw;
return *this;
}
inline simd_type& operator+=(const simd_type &rhs)
{
xyzw = _mm_add_ps(xyzw, rhs.xyzw);
return *this;
}
inline simd_type& operator-=(const simd_type &rhs)
{
xyzw = _mm_sub_ps(xyzw, rhs.xyzw);
return *this;
}
inline simd_type& operator*=(const simd_type &rhs)
{
xyzw = _mm_mul_ps(xyzw, rhs.xyzw);
return *this;
}
inline simd_type operator+(const simd_type &rhs) const
{
return simd_type(_mm_add_ps(xyzw, rhs.xyzw));
}
inline simd_type operator*(const simd_type &rhs) const
{
return simd_type(_mm_mul_ps(xyzw, rhs.xyzw));
}
inline simd_type operator-(const simd_type &rhs) const
{
return simd_type(_mm_sub_ps(xyzw, rhs.xyzw));
}
inline simd_type operator/(const simd_type &rhs) const
{
return simd_type(_mm_div_ps(xyzw, rhs.xyzw));
}
inline void Store(float *pVec) const
{
_mm_store_ps(pVec, xyzw);
}
inline void Bc()
{
xyzw = _mm_shuffle_ps(xyzw, xyzw, _MM_SHUFFLE(3,3,3,3));
}
static inline simd_type Dot(const simd_type& va, const simd_type& vb)
{
const __m128 t0 = _mm_mul_ps(va.xyzw, vb.xyzw);
const __m128 t1 = _mm_shuffle_ps(t0, t0, _MM_SHUFFLE(1,0,3,2));
const __m128 t2 = _mm_add_ps(t0, t1);
const __m128 t3 = _mm_shuffle_ps(t2, t2, _MM_SHUFFLE(2,3,0,1));
return simd_type(_mm_add_ps(t3, t2));
}
static inline simd_type Sqrt(const simd_type& va)
{
return simd_type(_mm_sqrt_ps(va.xyzw));
}
static inline simd_type VAdd(const simd_type& va, const simd_type& vb)
{
return simd_type(_mm_add_ps(va.xyzw, vb.xyzw));
}
static inline simd_type VSub(const simd_type& va, const simd_type& vb)
{
return simd_type(_mm_sub_ps(va.xyzw, vb.xyzw));
}
static inline simd_type VMul(const simd_type& va, const simd_type& vb)
{
return simd_type(_mm_mul_ps(va.xyzw, vb.xyzw));
}
static inline void GetX(float *p, const simd_type& v)
{
_mm_store_ss(p, v.xyzw);
}
private:
__m128 xyzw;
};
///////////////////////////////////////////
// Vec4
///////////////////////////////////////////
template <typename Real, typename Rep>
class vector4
{
public:
inline vector4() { }
inline vector4(Real *pVec)
: _rep(pVec)
{ }
inline vector4(Real f)
: _rep(f)
{ }
inline vector4(Real x, Real y, Real z, Real w)
: _rep(x, y, z, w)
{ }
inline vector4(const simd_type& rep)
: _rep(rep)
{ }
inline vector4(const vector4& copy)
: _rep(copy._rep)
{ }
inline vector4& operator= (const vector4& copy)
{
_rep = copy._rep;
return *this;
}
inline vector4& operator+= (const vector4& rhs)
{
_rep += rhs._rep;
return *this;
}
inline vector4& operator-= (const vector4& rhs)
{
_rep -= rhs._rep;
return *this;
}
inline vector4& operator*= (const vector4& rhs)
{
_rep *= rhs._rep;
return *this;
}
inline vector4 operator+ (const vector4& rhs) const
{
return vector4(_rep + rhs._rep);
}
inline vector4 operator- (const vector4& rhs) const
{
return vector4(_rep - rhs._rep);
}
inline vector4 operator* (const vector4& rhs) const
{
return vector4(_rep * rhs._rep);
}
inline vector4 operator/ (const vector4& rhs) const
{
return vector4(_rep / rhs._rep);
}
inline void Store(Real *pVec) const
{
_rep.Store(pVec);
}
inline void Bc()
{
_rep.Bc();
}
static inline vector4 Dot(const vector4& va, const vector4& vb)
{
return vector4(simd_type::Dot(va._rep, vb._rep));
}
static inline vector4 Sqrt(const vector4& va)
{
return vector4(simd_type::Sqrt(va._rep));
}
static inline vector4 VAdd(const vector4& va, const vector4& vb)
{
return vector4(simd_type::VAdd(va._rep, vb._rep));
}
static inline vector4 VSub(const vector4& va, const vector4& vb)
{
return vector4(simd_type::VSub(va._rep, vb._rep));
}
static inline vector4 VMul(const vector4& va, const vector4& vb)
{
return vector4(simd_type::VMul(va._rep, vb._rep));
}
static inline void GetX(Real *p, const vector4& v)
{
simd_type::GetX(p, v._rep);
}
private:
Rep _rep;
} ;
typedef vector4<float, simd_type> Vec4;
}
#endif