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
|
#pragma once
#include <Havok/Common/Base/Types/hkBaseTypes.h>
#include <Havok/Common/Base/Math/Vector/hkVector4fComparison.h>
#ifdef __aarch64__
#include <arm_neon.h>
#define HK_SIMD_FLOAT32_AARCH64_NEON
#else
#include <cmath>
#endif
class hkSimdFloat32 {
public:
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
using Storage = __attribute__((vector_size(2 * sizeof(float)))) float;
#else
using Storage = __attribute__((vector_size(4 * sizeof(float)))) float;
#endif
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,modernize-use-equals-default)
hkSimdFloat32() {}
// NOLINTNEXTLINE(google-explicit-constructor)
hkSimdFloat32(const Storage& x) { m_real = x; }
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
// NOLINTNEXTLINE(google-explicit-constructor)
hkSimdFloat32(const float& x) { m_real = vdup_n_f32(x); }
#else
hkSimdFloat32(float x) : m_real{x, x, x, x} {} // NOLINT(google-explicit-constructor)
#endif
hkFloat32 val() const { return m_real[0]; }
operator float() const { return val(); } // NOLINT(google-explicit-constructor)
template <int Constant>
HK_FORCE_INLINE static hkSimdFloat32 getConstant();
HK_FORCE_INLINE hkSimdFloat32 operator-() const;
HK_FORCE_INLINE hkSimdFloat32 operator+(hkSimdFloat32Parameter r) const;
HK_FORCE_INLINE hkSimdFloat32 operator-(hkSimdFloat32Parameter r) const;
HK_FORCE_INLINE hkSimdFloat32 operator*(hkSimdFloat32Parameter r) const;
HK_FORCE_INLINE hkSimdFloat32 operator/(hkSimdFloat32Parameter r) const;
HK_FORCE_INLINE void add(hkSimdFloat32Parameter a);
HK_FORCE_INLINE void sub(hkSimdFloat32Parameter a);
HK_FORCE_INLINE void mul(hkSimdFloat32Parameter a);
HK_FORCE_INLINE void div(hkSimdFloat32Parameter a);
HK_FORCE_INLINE void addMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void subMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void setAdd(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void setSub(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void setMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void setDiv(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b);
HK_FORCE_INLINE void setAddMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b,
hkSimdFloat32Parameter c);
HK_FORCE_INLINE void setSubMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b,
hkSimdFloat32Parameter c);
void setAbs(hkSimdFloat32Parameter x);
HK_FORCE_INLINE void setReciprocal(hkSimdFloat32Parameter a);
HK_FORCE_INLINE hkSimdFloat32 reciprocal() const;
/// Returns the square root (or zero if the value is negative).
HK_FORCE_INLINE hkSimdFloat32 sqrt() const;
/// Returns the square root. Does not check if the value is negative.
HK_FORCE_INLINE hkSimdFloat32 sqrtUnsafe() const;
/// Return the inverse square root (or zero if the value is negative or zero).
HK_FORCE_INLINE hkSimdFloat32 sqrtInverse() const;
/// Return the inverse square root. Does not check if the value is negative or zero.
HK_FORCE_INLINE hkSimdFloat32 sqrtInverseUnsafe() const;
HK_FORCE_INLINE hkBool32 isLess(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isLessEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isGreater(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isGreaterEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isNotEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkBool32 isLessZero() const;
HK_FORCE_INLINE hkBool32 isLessEqualZero() const;
HK_FORCE_INLINE hkBool32 isGreaterZero() const;
HK_FORCE_INLINE hkBool32 isGreaterEqualZero() const;
HK_FORCE_INLINE hkBool32 isEqualZero() const;
HK_FORCE_INLINE hkBool32 isNotEqualZero() const;
HK_FORCE_INLINE hkVector4fComparison less(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison lessEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison greater(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison greaterEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison equal(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison notEqual(hkSimdFloat32Parameter a) const;
HK_FORCE_INLINE hkVector4fComparison lessZero() const;
HK_FORCE_INLINE hkVector4fComparison lessEqualZero() const;
HK_FORCE_INLINE hkVector4fComparison greaterZero() const;
HK_FORCE_INLINE hkVector4fComparison greaterEqualZero() const;
HK_FORCE_INLINE hkVector4fComparison equalZero() const;
HK_FORCE_INLINE hkVector4fComparison notEqualZero() const;
HK_FORCE_INLINE m128 toQuad() const;
HK_FORCE_INLINE void load(const float* out);
HK_FORCE_INLINE void store(float* out) const;
Storage m_real;
private:
static hkVector4fComparison toComparison(const Storage& v) {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
return hkVector4fComparison::convert(vcombine_f32(v, v));
#else
return hkVector4fComparison::convert(v);
#endif
}
};
template <int Constant>
inline hkSimdFloat32 hkSimdFloat32::getConstant() {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
return vget_low_f32(g_vectorfConstants[Constant]);
#else
return g_vectorfConstants[Constant][0];
#endif
}
inline hkSimdFloat32 hkSimdFloat32::operator-() const {
return -m_real;
}
inline hkSimdFloat32 hkSimdFloat32::operator+(hkSimdFloat32Parameter r) const {
return m_real + r.m_real;
}
inline hkSimdFloat32 hkSimdFloat32::operator-(hkSimdFloat32Parameter r) const {
return m_real - r.m_real;
}
inline hkSimdFloat32 hkSimdFloat32::operator*(hkSimdFloat32Parameter r) const {
return m_real * r.m_real;
}
inline hkSimdFloat32 hkSimdFloat32::operator/(hkSimdFloat32Parameter r) const {
return m_real / r.m_real;
}
inline void hkSimdFloat32::add(hkSimdFloat32Parameter a) {
setAdd(*this, a);
}
inline void hkSimdFloat32::sub(hkSimdFloat32Parameter a) {
setSub(*this, a);
}
inline void hkSimdFloat32::mul(hkSimdFloat32Parameter a) {
setMul(*this, a);
}
inline void hkSimdFloat32::div(hkSimdFloat32Parameter a) {
setDiv(*this, a);
}
inline void hkSimdFloat32::addMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
setAddMul(*this, a, b);
}
inline void hkSimdFloat32::subMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
setSubMul(*this, a, b);
}
inline void hkSimdFloat32::setAdd(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
m_real = a.m_real + b.m_real;
}
inline void hkSimdFloat32::setSub(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
m_real = a.m_real - b.m_real;
}
inline void hkSimdFloat32::setMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
m_real = a.m_real * b.m_real;
}
inline void hkSimdFloat32::setDiv(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b) {
m_real = a.m_real / b.m_real;
}
inline void hkSimdFloat32::setAddMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b,
hkSimdFloat32Parameter c) {
m_real = a.m_real + b.m_real * c.m_real;
}
inline void hkSimdFloat32::setSubMul(hkSimdFloat32Parameter a, hkSimdFloat32Parameter b,
hkSimdFloat32Parameter c) {
m_real = a.m_real - b.m_real * c.m_real;
}
inline void hkSimdFloat32::setAbs(hkSimdFloat32Parameter x) {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
m_real = vabs_f32(m_real);
#else
for (int i = 0; i < 4; ++i)
m_real[i] = std::abs(x.m_real[i]);
#endif
}
inline void hkSimdFloat32::setReciprocal(hkSimdFloat32Parameter a) {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
auto r0 = vrecpe_f32(a.m_real);
auto r1 = vmul_f32(r0, vrecps_f32(a.m_real, r0));
auto r2 = vmul_f32(r1, vrecps_f32(a.m_real, r1));
m_real = r2;
#else
for (int i = 0; i < 4; ++i)
m_real[i] = 1.0f / a.m_real[i];
#endif
}
inline hkSimdFloat32 hkSimdFloat32::reciprocal() const {
hkSimdFloat32 r;
r.setReciprocal(*this);
return r;
}
inline hkSimdFloat32 hkSimdFloat32::sqrt() const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
const auto leqZero = vclez_f32(m_real);
const auto inv = sqrtInverseUnsafe().m_real;
const auto r = m_real * inv;
return vbic_u32(r, leqZero);
#else
hkSimdFloat32 result;
for (int i = 0; i < 4; ++i)
result.m_real[i] = m_real[i] >= 0.0f ? std::sqrt(m_real[i]) : 0.0f;
return result;
#endif
}
inline hkSimdFloat32 hkSimdFloat32::sqrtUnsafe() const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
const auto inv = sqrtInverseUnsafe();
return m_real * inv.m_real;
#else
hkSimdFloat32 result;
for (int i = 0; i < 4; ++i)
result.m_real[i] = std::sqrt(m_real[i]);
return result;
#endif
}
inline hkSimdFloat32 hkSimdFloat32::sqrtInverse() const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
const auto leqZero = vclez_f32(m_real);
const auto inv = sqrtInverseUnsafe().m_real;
return vbic_u32(inv, leqZero);
#else
hkSimdFloat32 result;
for (int i = 0; i < 4; ++i)
result.m_real[i] = m_real[i] > 0.0f ? (1.0f / std::sqrt(m_real[i])) : 0.0f;
return result;
#endif
}
inline hkSimdFloat32 hkSimdFloat32::sqrtInverseUnsafe() const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
auto r0 = vrsqrte_f32(m_real);
r0 = vrsqrts_f32(m_real, r0 * r0) * r0;
r0 = vrsqrts_f32(m_real, r0 * r0) * r0;
return r0;
#else
hkSimdFloat32 result;
for (int i = 0; i < 4; ++i)
result.m_real[i] = 1.0f / std::sqrt(m_real[i]);
return result;
#endif
}
inline hkBool32 hkSimdFloat32::isLess(hkSimdFloat32Parameter a) const {
auto cmp = m_real < a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isLessEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real <= a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isGreater(hkSimdFloat32Parameter a) const {
auto cmp = m_real > a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isGreaterEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real >= a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real == a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isNotEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real != a.m_real;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isLessZero() const {
auto cmp = m_real < 0.0;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isLessEqualZero() const {
auto cmp = m_real <= 0.0;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isGreaterZero() const {
auto cmp = m_real > 0.0;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isGreaterEqualZero() const {
auto cmp = m_real >= 0.0;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isEqualZero() const {
auto cmp = m_real == 0.0;
return cmp[0];
}
inline hkBool32 hkSimdFloat32::isNotEqualZero() const {
auto cmp = m_real != 0.0;
return cmp[0];
}
inline hkVector4fComparison hkSimdFloat32::less(hkSimdFloat32Parameter a) const {
auto cmp = m_real < a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::lessEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real <= a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::greater(hkSimdFloat32Parameter a) const {
auto cmp = m_real > a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::greaterEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real >= a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::equal(hkSimdFloat32Parameter a) const {
auto cmp = m_real == a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::notEqual(hkSimdFloat32Parameter a) const {
auto cmp = m_real != a.m_real;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::lessZero() const {
auto cmp = m_real < 0.0;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::lessEqualZero() const {
auto cmp = m_real <= 0.0;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::greaterZero() const {
auto cmp = m_real > 0.0;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::greaterEqualZero() const {
auto cmp = m_real >= 0.0;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::equalZero() const {
auto cmp = m_real == 0.0;
return toComparison(cmp);
}
inline hkVector4fComparison hkSimdFloat32::notEqualZero() const {
auto cmp = m_real != 0.0;
return toComparison(cmp);
}
inline m128 hkSimdFloat32::toQuad() const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
return vcombine_f32(m_real, m_real);
#else
return m_real;
#endif
}
inline void hkSimdFloat32::load(const float* out) {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
m_real = vld1_dup_f32(out);
#else
*this = *out;
#endif
}
inline void hkSimdFloat32::store(float* out) const {
#ifdef HK_SIMD_FLOAT32_AARCH64_NEON
vst1_lane_f32(out, m_real, 0);
#else
*out = val();
#endif
}
|