SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_float_4_inl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "x64_simd_inl.h" // for IntelliSense
20
21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
22
23namespace superdex {
24
25/***********************************************************************************************
26 Simd<float, 4>
27*/
28template <>
29class Simd<float, 4> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(float, 4, __m128);
32 Simd(float a, float b, float c = 0.0f, float d = 0.0f) : raw(_mm_set_ps(d, c, b, a)) {} // SSE
33 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
34 Simd(U a) : raw(_mm_set_ps1(a)) {} // SSE
35
36 template <int i>
37 [[nodiscard]] static MOCHI_FORCE_INLINE float Get(Simd v) {
38 static_assert(i >= 0 && i < 4, "Index out of range");
39 return v[i];
40 }
41
42 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
43 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
44#if MOCHI_COMPILER_MSVC
45 return raw.m128_f32[i];
46#else
47 return raw[i];
48#endif
49 }
50
51 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
52 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
53#if MOCHI_COMPILER_MSVC
54 auto result = v;
55 result.raw.m128_f32[i] = value;
56 return result;
57#else
58 static constexpr __m128i kMasks[] = {
59 // clang-format off
60 {static_cast<long long>(0x00000000FFFFFFFFLL), static_cast<long long>(0x0000000000000000LL)},
61 {static_cast<long long>(0xFFFFFFFF00000000LL), static_cast<long long>(0x0000000000000000LL)},
62 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x00000000FFFFFFFFLL)},
63 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0xFFFFFFFF00000000LL)}
64 }; // clang-format on
65 return _mm_blendv_ps(v.raw, _mm_set1_ps(value), _mm_castsi128_ps(kMasks[i])); // SSE4.1
66#endif
67 }
68
69 template <int i>
70 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
71 return _mm_insert_ps(v.raw, _mm_set1_ps(value), i << 4); // SSE4.1
72 }
73
74 [[nodiscard]] static MOCHI_FORCE_INLINE Simd AsPoint(Simd a) {
75 // Replace the 3rd component with an integer that has the same bits as 1.0f.
76 auto araw = _mm_castps_si128(a.raw); // SSE2
77 auto v = _mm_insert_epi32(araw, 0x3f800000, 3); // SSE4.1
78 return _mm_castsi128_ps(v); // SSE2
79 }
80
81 [[nodiscard]] static MOCHI_FORCE_INLINE Simd AsDirection(Simd a) {
82 // Replace the 3rd component with an integer that has the same bits as 0.0f.
83 auto araw = _mm_castps_si128(a.raw); // SSE2
84 auto v = _mm_insert_epi32(araw, 0x00000000, 3); // SSE4.1
85 return _mm_castsi128_ps(v); // SSE2
86 }
87
88 template <int x, int y, int z, int w>
89 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Blend(Simd a, Simd b) {
90 static_assert(
91 x >= 0 && x < 2 && y >= 0 && y < 2 && z >= 0 && z < 2 && w >= 0 && w < 2,
92 "invalid blend index");
93 if constexpr (x == 0 && y == 0 && z == 0 && w == 0) {
94 return a;
95 } else if constexpr (x == 1 && y == 1 && z == 1 && w == 1) {
96 return b;
97 } else {
98 return _mm_blend_ps(a.raw, b.raw, x | (y << 1) | (z << 2) | (w << 3)); // SSE4.1
99 }
100 }
101
102 template <int N>
103 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
104 static_assert(N >= 1 && N <= kSize, "Unsupported N");
105 int constexpr kLanes = (1 << N) - 1;
106 return (ToMask(v) & kLanes) == kLanes;
107 }
108
109 template <int N>
110 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
111 static_assert(N >= 1 && N <= kSize, "Unsupported N");
112 int constexpr kLanes = (1 << N) - 1;
113 return (ToMask(v) & kLanes) != 0;
114 }
115
116 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
117 return _mm_broadcast_ss(p); // AVX
118 }
119
120 template <int i>
121 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
122 return Shuffle<i, i, i, i>(v);
123 }
124
125 template <int N = kSize>
126 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
127 static_assert(N >= 0 && N <= 4);
128 if constexpr (N == 0) {
129 return Simd::Zero();
130 } else if constexpr (N == 1) {
131 return Simd{*ptr, 0.0f};
132 } else if constexpr (N == 2) {
133 return _mm_castsi128_ps(_mm_loadl_epi64(reinterpret_cast<__m128i const*>(ptr))); // SSE2, SSE2
134 } else if constexpr (N == 3) {
135 __m128i mask = _mm_set_epi32(0, -1, -1, -1); // SSE2
136 return _mm_maskload_ps(ptr, mask); // AVX
137 } else if constexpr (N == 4) {
138 return _mm_loadu_ps(ptr); // SSE
139 }
140 }
141
142 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
143 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
144#if MOCHI_ARCH_X64_AVX512
145 return _mm_maskz_loadu_ps(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
146#else
147 return _mm_maskload_ps(ptr, x64_simd::kLoadMasksS4[n]); // AVX
148#endif
149 }
150
151 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int, 4> const& indices) {
152 return _mm_i32gather_ps(ptr, indices.raw, sizeof(float)); // AVX2
153 }
154
155 template <int kTupleCount = kSize>
156 MOCHI_FORCE_INLINE static void
157 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
158 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
159 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
160 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
161 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
162 auto a = Simd::Load<kCount0>(ptr).raw; // [0,1,2,3]
163 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 4).raw; // [4,5,6,7]
164 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 8).raw; // [8,9,10,11]
165 Deinterleave3(a, b, c, out0, out1, out2);
166 }
167
168 MOCHI_FORCE_INLINE static void
169 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
170 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
171 int const n = count * 3;
172 auto a = Simd::Load(ptr, superdex::Min(n, 4)).raw; // [0,1,2,3]
173 auto b = Simd::Load(n > 4 ? ptr + 4 : ptr, Clamp(n - 4, 0, 4)).raw; // [4,5,6,7]
174 auto c = Simd::Load(n > 8 ? ptr + 8 : ptr, Clamp(n - 8, 0, 4)).raw; // [8,9,10,11]
175 Deinterleave3(a, b, c, out0, out1, out2);
176 }
177
178 template <int i>
179 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SetBasisVector() {
180 static_assert(i >= 0 && i <= 3, "Invalid component index");
181 auto zero = _mm_setzero_si128(); // SSE2
182 auto v = _mm_insert_epi32(zero, 0x3f800000, i); // SSE4.1
183 return _mm_castsi128_ps(v); // SSE2
184 }
185
186 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
187 return _mm_blendv_ps(b.raw, a.raw, mask.raw); // SSE4.1
188 }
189
190 // return Simd{v[x], v[y], v[z], v[w]}
191 template <int x = 0, int y = 1, int z = 2, int w = 3>
192 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd v) {
193 static_assert(
194 x >= 0 && x < 4 && y >= 0 && y < 4 && z >= 0 && z < 4 && w >= 0 && w < 4, "Invalid index");
195 if constexpr (x == 0 && y == 1 && z == 2 && w == 3) {
196 return v;
197 } else {
198 return _mm_shuffle_ps(v.raw, v.raw, x | (y << 2) | (z << 4) | (w << 6)); // SSE
199 }
200 }
201 template <int x = 0, int y = 1, int z = 2, int w = 3>
202 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd a, Simd b) {
203 static_assert(
204 x >= 0 && x < 4 && y >= 0 && y < 4 && z >= 0 && z < 4 && w >= 0 && w < 4, "Invalid index");
205 return _mm_shuffle_ps(a.raw, b.raw, x | (y << 2) | (z << 4) | (w << 6)); // SSE
206 }
207
208 template <int N = kSize>
209 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
210 static_assert(N >= 0 && N <= kSize);
211 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
212 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
213 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
214 // go through the stack.
215 if constexpr (N == 0) {
216 } else if constexpr (N == 1) {
217 _mm_store_ss(ptr, v.raw); // SSE
218 } else if constexpr (N == 2) {
219 _mm_storel_epi64(reinterpret_cast<__m128i*>(ptr), _mm_castps_si128(v.raw)); // SSE2
220 } else if constexpr (N == 3) {
221 _mm_store_ss(ptr + 2, _mm_movehl_ps(v.raw, v.raw)); // SSE
222 _mm_storel_epi64(reinterpret_cast<__m128i*>(ptr), _mm_castps_si128(v.raw)); // SSE2
223 } else {
224 _mm_storeu_ps(ptr, v.raw); // SSE
225 }
226 }
227
228 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
229 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
230#if MOCHI_ARCH_X64_AVX512
231 _mm_mask_storeu_ps(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
232#else
233 // With AVX2, this is faster than masked store for a predictable value of n.
234 // It is much slower for a random value of n.
235 switch (n) { // clang-format off
236 case 1: Store<1>(ptr, v); break;
237 case 2: Store<2>(ptr, v); break;
238 case 3: Store<3>(ptr, v); break;
239 case 4: Store<4>(ptr, v); break;
240 MOCHI_UNLIKELY default: break;
241 } // clang-format on
242#endif
243 }
244
245 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
246 auto mask = ToMask(condition);
247 // Load the shuffle pattern from a lookup table.
248 auto const* tableRow =
249 reinterpret_cast<__m128i const*>(x64_simd::kStoreSelectedShuffleTableS4[mask]);
250 auto pattern = _mm_load_si128(tableRow);
251 auto packed = _mm_permutevar_ps(values.raw, pattern);
252 _mm_storeu_ps(ptr, packed);
253 return _mm_popcnt_u32(mask);
254 }
255
256 template <int kTupleCount = kSize>
257 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
258 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
259 Simd x0, x1, x2;
260 Interleave3(a, b, c, x0, x1, x2);
261 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
262 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
263 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
264 Simd::Store<kCount0>(ptr, x0);
265 if constexpr (kCount1 > 0) {
266 Simd::Store<kCount1>(ptr + 4, x1);
267 }
268 if constexpr (kCount2 > 0) {
269 Simd::Store<kCount2>(ptr + 8, x2);
270 }
271 }
272
273 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
274 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
275 Simd x0, x1, x2;
276 Interleave3(a, b, c, x0, x1, x2);
277#if MOCHI_ARCH_X64_AVX512
278 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
279 // back into branches. A zero mask stores nothing.
280 uint32_t const bits = (1u << (count * 3)) - 1;
281 _mm_mask_storeu_ps(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
282 _mm_mask_storeu_ps(ptr + (count > 1 ? 4 : 0), static_cast<__mmask8>(bits >> 4), x1.raw);
283 _mm_mask_storeu_ps(ptr + (count > 2 ? 8 : 0), static_cast<__mmask8>(bits >> 8), x2.raw);
284#else
285 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
286 // counts, which clang may turn back into branches. A zero mask stores nothing.
287 auto const n = _mm_set1_epi32(count * 3);
288 auto const mask0 = _mm_cmpgt_epi32(n, _mm_setr_epi32(0, 1, 2, 3)); // SSE2
289 auto const mask1 = _mm_cmpgt_epi32(n, _mm_setr_epi32(4, 5, 6, 7));
290 auto const mask2 = _mm_cmpgt_epi32(n, _mm_setr_epi32(8, 9, 10, 11));
291 _mm_maskstore_ps(ptr, mask0, x0.raw); // AVX
292 _mm_maskstore_ps(ptr + (count > 1 ? 4 : 0), mask1, x1.raw);
293 _mm_maskstore_ps(ptr + (count > 2 ? 8 : 0), mask2, x2.raw);
294#endif
295 }
296
297 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
298 return _mm_sqrt_ps(v.raw); // SSE
299 }
300
301 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
302 return _mm_rcp_ps(v.raw); // SSE
303 }
304
305 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
306 return _mm_rsqrt_ps(v.raw); // SSE
307 }
308
309 // Broadcast the value -0.0. Use this in bitwise operations to affect just the sign bit.
310 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
311 return _mm_castsi128_ps(_mm_set1_epi32(0x80000000)); // SSE2, SSE2
312 }
313
314 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
315 return _mm_andnot_ps(SignBitMask().raw, v.raw); // SSE
316 }
317
318 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
319 return _mm_min_ps(a.raw, b.raw); // SSE
320 }
321
322 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
323 return _mm_max_ps(a.raw, b.raw); // SSE
324 }
325
326 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
327 return _mm_floor_ps(a.raw); // SSE4.1
328 }
329
330 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
331 return _mm_round_ps(v.raw, _MM_FROUND_TO_NEAREST_INT); // SSE4.1
332 }
333
334#if MOCHI_ARCH_X64_SVML
335 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
336 return _mm_cos_ps(a.raw); // SSE
337 }
338
339 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
340 return _mm_sin_ps(a.raw); // SSE
341 }
342
343 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
344 return _mm_tan_ps(a.raw); // SSE
345 }
346
347 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
348 return _mm_acos_ps(a.raw); // SSE
349 }
350
351 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
352 return _mm_asin_ps(a.raw); // SSE
353 }
354
355 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
356 return _mm_atan_ps(a.raw); // SSE
357 }
358
359 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
360 return _mm_exp_ps(a.raw); // SSE
361 }
362
363 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
364 return _mm_log_ps(a.raw); // SSE
365 }
366
367 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
368 return _mm_tanh_ps(a.raw); // SSE
369 }
370#endif // MOCHI_ARCH_X64_SVML
371
372 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
373#if MOCHI_ARCH_X64_FMA
374 return {_mm_fmadd_ps(a.raw, b.raw, c.raw)}; // FMA
375#else
376 return (a * b) + c;
377#endif
378 }
379
380 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
381#if MOCHI_ARCH_X64_FMA
382 return _mm_fmsub_ps(a.raw, b.raw, c.raw); // FMA
383#else
384 return (a * b) - c;
385#endif
386 }
387
388 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
389#if MOCHI_ARCH_X64_FMA
390 return _mm_fnmadd_ps(a.raw, b.raw, c.raw); // FMA
391#else
392 return -(a * b) + c;
393#endif
394 }
395
396 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
397#if MOCHI_ARCH_X64_FMA
398 return _mm_fnmsub_ps(a.raw, b.raw, c.raw); // FMA
399#else
400 return -(a * b) - c;
401#endif
402 }
403
404 template <int N = 4>
405 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
406 static_assert(N >= 2 && N <= 4, "Unsupported N");
407 if constexpr (N == 2) {
408 return Get<0>(Min(a, Broadcast<1>(a)));
409 } else if constexpr (N == 3) {
410 return Get<0>(Min(Min(a, Broadcast<1>(a)), Broadcast<2>(a)));
411 } else {
412 auto tmp = Min(a, Shuffle<1, 2, 3, 0>(a));
413 return Get<0>(Min(tmp, Simd::Shuffle<2, 3, 0, 1>(tmp)));
414 }
415 }
416
417 template <int N = 4>
418 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
419 static_assert(N >= 2 && N <= 4, "Unsupported N");
420 if constexpr (N == 2) {
421 return Get<0>(Max(a, Broadcast<1>(a)));
422 } else if constexpr (N == 3) {
423 return Get<0>(Max(Max(a, Broadcast<1>(a)), Broadcast<2>(a)));
424 } else {
425 auto tmp = Max(a, Shuffle<1, 2, 3, 0>(a));
426 return Get<0>(Max(tmp, Shuffle<2, 3, 0, 1>(tmp)));
427 }
428 }
429
430 template <int N>
431 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
432 static_assert(N >= 2 && N <= 4, "Unsupported N");
433 // Terms are added in the same order as Simd<double, 4>::HSum<N> for consistency.
434 if constexpr (N == 2) {
435 return Get<0>(a) + Get<1>(a); // a[0] + a[1]
436 } else if constexpr (N == 3) {
437 return (Get<0>(a) + Get<2>(a)) + Get<1>(a); // (a[0] + a[2]) + a[1]
438 } else {
439 // PERF NOTE: Alternatively _mm_dp_ps could be used to compute the dot product with
440 // Simd{1.0f}. In comparison, this implementation takes 2 extra instructions, but it had ~50%
441 // higher throughput and ~45% lower latency, when benchmarked on an AMD CPU.
442 return (Get<0>(a) + Get<2>(a)) + (Get<1>(a) + Get<3>(a)); // (a[0] + a[2]) + (a[1] + a[3])
443 }
444 }
445
446 [[nodiscard]] static MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b, Simd c, Simd d) {
447 // ab = {a[0] + a[2], b[0] + b[2], a[1] + a[3], b[1] + b[3]}, and cd likewise.
448 auto const ab = _mm_add_ps(_mm_unpacklo_ps(a.raw, b.raw), _mm_unpackhi_ps(a.raw, b.raw)); // SSE
449 auto const cd = _mm_add_ps(_mm_unpacklo_ps(c.raw, d.raw), _mm_unpackhi_ps(c.raw, d.raw)); // SSE
450 // {ab[0], ab[1], cd[2], cd[3]} + {ab[2], ab[3], cd[0], cd[1]}.
451 return _mm_add_ps(
452 _mm_blend_ps(ab, cd, 0b1100), // SSE4.1
453 _mm_shuffle_ps(ab, cd, _MM_SHUFFLE(1, 0, 3, 2))); // SSE
454 }
455
456 template <int N>
457 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
458 static_assert(N >= 2 && N <= 4, "Unsupported N");
459 alignas(alignof(Simd)) Scalar buf[4];
460 Store(buf, a);
461 if constexpr (N == 2) {
462 return buf[0] * buf[1];
463 } else if constexpr (N == 3) {
464 return buf[0] * buf[1] * buf[2];
465 } else if constexpr (N == 4) {
466 return buf[0] * buf[1] * buf[2] * buf[3];
467 }
468 }
469
470 template <int N>
471 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
472 static_assert(N >= 2 && N <= 4, "Unsupported N");
473 if constexpr (N == 2) {
474 return _mm_dp_ps(a.raw, b.raw, 0x3F); // SSE4.1
475 } else if constexpr (N == 3) {
476 return _mm_dp_ps(a.raw, b.raw, 0x7F); // SSE4.1
477 } else if constexpr (N == 4) {
478#if MOCHI_COMPILER_CLANG
479 return _mm_dp_ps(a.raw, b.raw, -1); // SSE4.1
480#else
481 return _mm_dp_ps(a.raw, b.raw, 0xFF); // SSE4.1
482#endif
483 }
484 }
485
486 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
487 return _mm_cmplt_ps(this->raw, rhs.raw); // SSE
488 }
489
490 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
491 return _mm_cmpgt_ps(this->raw, rhs.raw); // SSE
492 }
493
494 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
495 return _mm_cmple_ps(this->raw, rhs.raw); // SSE
496 }
497
498 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
499 return _mm_cmpge_ps(this->raw, rhs.raw); // SSE
500 }
501
502 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
503 return _mm_cmpeq_ps(a.raw, b.raw); // SSE
504 }
505
506 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
507 return _mm_cmpneq_ps(a.raw, b.raw); // SSE
508 }
509
510 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
511 return _mm_setzero_ps(); // SSE
512 }
513
514 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
515 return ToMask(Equal(*this, rhs)) == 0xF; // All values equal
516 }
517
518 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
519 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
520 }
521
522 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
523 // _mm_cmpeq_epi32 appears to be the fastest way to fill an SSE register with ones.
524 __m128i dummy{};
525 __m128 ones = _mm_castsi128_ps(_mm_cmpeq_epi32(dummy, dummy)); // SSE2, SSE2
526 return _mm_xor_ps(raw, ones); // SSE
527 }
528
529 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
530 return _mm_xor_ps(raw, SignBitMask().raw); // SSE, SSE
531 }
532
533 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
534 return _mm_add_ps(raw, rhs.raw); // SSE
535 }
536
537 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
538 return _mm_sub_ps(raw, rhs.raw); // SSE
539 }
540
541 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
542 return _mm_mul_ps(raw, rhs.raw); // SSE
543 }
544
545 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
546 return _mm_div_ps(raw, rhs.raw); // SSE
547 }
548
549 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
550 return _mm_and_ps(raw, rhs.raw); // SSE
551 }
552
553 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
554 return _mm_or_ps(raw, rhs.raw); // SSE
555 }
556
557 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
558 return _mm_xor_ps(raw, rhs.raw); // SSE
559 }
560
561 private:
562 // One bit per lane, from the lane's sign bit.
563 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
564 return _mm_movemask_ps(a.raw); // SSE
565 }
566
567 // Splits 12 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
568 MOCHI_FORCE_INLINE static void
569 Deinterleave3(__m128 a, __m128 b, __m128 c, Simd& out0, Simd& out1, Simd& out2) {
570 auto t0 = _mm_blend_ps(a, b, 0b0100); // [0,_,6,3]
571 auto t1 = _mm_blend_ps(t0, c, 0b0010); // [0,9,6,3]
572 out0 = _mm_shuffle_ps(t1, t1, _MM_SHUFFLE(1, 2, 3, 0)); // [0,3,6,9]
573
574 t0 = _mm_blend_ps(a, b, 0b1001); // [4,1,_,7]
575 t1 = _mm_blend_ps(t0, c, 0b0100); // [4,1,10,7]
576 out1 = _mm_shuffle_ps(t1, t1, _MM_SHUFFLE(2, 3, 0, 1)); // [1,4,7,10]
577
578 t0 = _mm_blend_ps(a, b, 0b0010); // [_,5,2,_]
579 t1 = _mm_blend_ps(c, t0, 0b0110); // [8,5,2,11]
580 out2 = _mm_shuffle_ps(t1, t1, _MM_SHUFFLE(3, 0, 1, 2)); // [2,5,8,11]
581 }
582
583 // Inverse of Deinterleave3: out0, out1, out2 are the 12 interleaved scalars in memory order.
584 MOCHI_FORCE_INLINE static void
585 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
586 // a = [0,3,6,9], b = [1,4,7,10], c = [2,5,8,11]
587 auto d = _mm_shuffle_ps(a.raw, a.raw, _MM_SHUFFLE(1, 2, 3, 0)); // [0,9,6,3]
588 auto e = _mm_shuffle_ps(b.raw, b.raw, _MM_SHUFFLE(2, 3, 0, 1)); // [4,1,10,7]
589 auto f = _mm_shuffle_ps(c.raw, c.raw, _MM_SHUFFLE(3, 0, 1, 2)); // [8,5,2,11]
590 out0 = _mm_blend_ps(_mm_blend_ps(d, e, 0b0010), f, 0b0100); // [0,1,2,3]
591 out1 = _mm_blend_ps(_mm_blend_ps(d, e, 0b1001), f, 0b0010); // [4,5,6,7]
592 out2 = _mm_blend_ps(_mm_blend_ps(d, e, 0b0100), f, 0b1001); // [8,9,10,11]
593 }
594};
595
596} // namespace superdex
597
598#endif // MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
Simd operator&(Simd rhs) const
bool operator==(Simd rhs) const
NativeType raw
Definition simd.h:174
Simd operator>(Simd rhs) const
Simd operator*(Simd rhs) const
Simd operator^(Simd rhs) const
Simd operator-() const
Simd operator>=(Simd rhs) const
Simd operator<(Simd rhs) const
bool operator!=(Simd rhs) const
Simd operator|(Simd rhs) const
static constexpr int kSize
Definition simd.h:96
Simd operator+(Simd rhs) const
Simd operator~() const
Simd operator/(Simd rhs) const
Simd operator<=(Simd rhs) const
Scalar operator[](int i) const
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
#define MOCHI_UNLIKELY
#define MOCHI_FORCE_INLINE
T Dot(Simd< T, N > a, Simd< T, N > b)
Definition simd.h:710
constexpr T ACos(T a)
constexpr T NegMulAdd(T a, T b, T c)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
Definition simd_inl.h:287
Simd< T, 2 > HSumEach(Simd< T, 2 > a, Simd< T, 2 > b)
Returns {HSum(a), HSum(b)}.
Definition simd_inl.h:401
V LoadIndexed(typename V::Scalar const *ptr, Simd< I, V::kSize > indices)
Definition simd_inl.h:202
constexpr T const & Min(T const &a, T const &b)
constexpr auto Equal(T const &a, T const &b)
constexpr T Sin(T a)
T HSum(Simd< T, N > a)
Definition simd_inl.h:391
T HMin(Simd< T, N > a)
Definition simd_inl.h:451
bool AllTrue(T const &a)
Definition basic_utils.h:60
constexpr T NegMulSub(T a, T b, T c)
Simd< T, N > Tanh(Simd< T, N > a)
Definition simd_inl.h:783
Simd< T, N > Set(Simd< T, N > a, T value)
Definition simd_inl.h:327
constexpr auto NotEqual(T const &a, T const &b)
constexpr T Exp(T a)
constexpr T Cos(T a)
T HMax(Simd< T, N > a)
Definition simd_inl.h:461
V Broadcast(typename V::Scalar a)
Definition simd_inl.h:117
constexpr T Abs(T a)
Definition basic_utils.h:50
constexpr T MulSub(T a, T b, T c)
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:302
constexpr T Tan(T a)
bool AnyTrue(T const &a)
Definition basic_utils.h:66
constexpr T Select(bool condition, T a, T b)
constexpr T Sqrt(T a)
constexpr T ATan(T a)
Simd< T, N > Ln(Simd< T, N > a)
Definition simd_inl.h:705
T Get(Simd< T, N > v)
Definition simd_inl.h:317
constexpr T Floor(T a)
constexpr T ASin(T a)
constexpr ValT Clamp(ValT value, MinT min, MaxT max)
T HProd(Simd< T, N > a)
Definition simd_inl.h:441
constexpr T const & Max(T const &a, T const &b)
void LoadTransposed(T const *ptr, Simd< T, N > &out0, Simd< T, N > &out1, Simd< T, N > &out2)
Definition simd_inl.h:209
Simd< T, N > FastRound(Simd< T, N > a)
Definition simd_inl.h:486
void StoreTransposed(T *ptr, Simd< T, N > a, Simd< T, N > b, Simd< T, N > c)
Definition simd_inl.h:253
void Store(T *ptr, Simd< T, N > a)
Definition simd_inl.h:221
Simd< T, N > RcpSqrtApprox(Simd< T, N > a)
Definition simd_inl.h:371
constexpr T RcpApprox(T a)
constexpr T MulAdd(T a, T b, T c)
int StoreSelected(T *ptr, Simd< MaskT, N > condition, Simd< T, N > values)
Definition simd_inl.h:233
V Load(typename V::Scalar const *ptr)
Definition simd_inl.h:186
#define MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(T, N, NativeT)