SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_float_8_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, 8>
27*/
28template <>
29class Simd<float, 8> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(float, 8, __m256);
32 Simd(
33 float a,
34 float b,
35 float c = 0.0f,
36 float d = 0.0f,
37 float e = 0.0f,
38 float f = 0.0f,
39 float g = 0.0f,
40 float h = 0.0f)
41 : raw(_mm256_set_ps(h, g, f, e, d, c, b, a)) {} // AVX
42
43 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
44 Simd(U a) : raw(_mm256_set1_ps(a)) {} // AVX
45
46 // Joint two Vec4f into a single Vec8f
47 Simd(Simd<float, 4> const& low, Simd<float, 4> const& high)
48 : raw(_mm256_set_m128(high.raw, low.raw)) {} // AVX
49
50 template <int i>
51 [[nodiscard]] static MOCHI_FORCE_INLINE float Get(Simd v) {
52 static_assert(i >= 0 && i < 8, "Index out of range");
53 return v[i];
54 }
55
56 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
57 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
58#if MOCHI_COMPILER_MSVC
59 return raw.m256_f32[i];
60#else
61 return raw[i];
62#endif
63 }
64
65 template <int iHalf>
66 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<float, 4> GetHalf(Simd a) {
67 static_assert(iHalf == 0 || iHalf == 1);
68 return _mm256_extractf128_ps(a.raw, iHalf); // AVX
69 }
70
71 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
72 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
73#if MOCHI_COMPILER_MSVC
74 auto result = v;
75 result.raw.m256_f32[i] = value;
76 return result;
77#else
78 static constexpr __m256i kMasks[] = {
79 // clang-format off
80 {static_cast<long long>(0x00000000FFFFFFFFLL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL)},
81 {static_cast<long long>(0xFFFFFFFF00000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL)},
82 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x00000000FFFFFFFFLL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL)},
83 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0xFFFFFFFF00000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL)},
84 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x00000000FFFFFFFFLL), static_cast<long long>(0x0000000000000000LL)},
85 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0xFFFFFFFF00000000LL), static_cast<long long>(0x0000000000000000LL)},
86 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x00000000FFFFFFFFLL)},
87 {static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0x0000000000000000LL), static_cast<long long>(0xFFFFFFFF00000000LL)}
88 }; // clang-format on
89 return _mm256_blendv_ps(v.raw, _mm256_set1_ps(value), _mm256_castsi256_ps(kMasks[i])); // AVX
90#endif
91 }
92
93 template <int i>
94 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
95 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
96 return Set(v, i, value);
97 }
98
99 template <int N>
100 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
101 static_assert(N >= 1 && N <= kSize, "Unsupported N");
102 int constexpr kLanes = (1 << N) - 1;
103 return (ToMask(v) & kLanes) == kLanes;
104 }
105
106 template <int N>
107 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
108 static_assert(N >= 1 && N <= kSize, "Unsupported N");
109 int constexpr kLanes = (1 << N) - 1;
110 return (ToMask(v) & kLanes) != 0;
111 }
112
113 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
114 return _mm256_broadcast_ss(p); // AVX
115 }
116
117 template <int i>
118 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
119 return Simd{Get<i>(v)}; // TODO: There is probably a faster way
120 }
121
122 template <int N = kSize>
123 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
124 static_assert(N >= 0 && N <= 8);
125 if constexpr (N == 0) {
126 return Simd::Zero();
127 } else if constexpr (N == 1) {
128 return Simd{*ptr, 0.0f};
129 } else if constexpr (N == 2) {
130 auto const low = _mm_loadl_epi64(reinterpret_cast<__m128i const*>(ptr)); // SSE2
131 return _mm256_zextps128_ps256(_mm_castsi128_ps(low)); // AVX, SSE2
132 } else if constexpr (N == 3) {
133 __m256i mask = _mm256_set_epi32(0, 0, 0, 0, 0, -1, -1, -1); // AVX
134 return _mm256_maskload_ps(ptr, mask); // AVX
135 } else if constexpr (N == 4) {
136 return _mm256_zextps128_ps256(_mm_loadu_ps(ptr)); // AVX, SSE
137 } else if constexpr (N == 5) {
138 __m256i mask = _mm256_set_epi32(0, 0, 0, -1, -1, -1, -1, -1); // AVX
139 return _mm256_maskload_ps(ptr, mask); // AVX
140 } else if constexpr (N == 6) {
141 __m256i mask = _mm256_set_epi32(0, 0, -1, -1, -1, -1, -1, -1); // AVX
142 return _mm256_maskload_ps(ptr, mask); // AVX
143 } else if constexpr (N == 7) {
144 __m256i mask = _mm256_set_epi32(0, -1, -1, -1, -1, -1, -1, -1); // AVX
145 return _mm256_maskload_ps(ptr, mask); // AVX
146 } else if constexpr (N == 8) {
147 return _mm256_loadu_ps(ptr); // AVX
148 }
149 }
150
151 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
152 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
153#if MOCHI_ARCH_X64_AVX512
154 return _mm256_maskz_loadu_ps(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
155#else
156 return _mm256_maskload_ps(ptr, x64_simd::kLoadMasksS8[n]); // AVX
157#endif
158 }
159
160 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int, 8> const& indices) {
161 return _mm256_i32gather_ps(ptr, indices.raw, sizeof(float)); // AVX2
162 }
163
164 template <int N = kSize>
165 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
166 static_assert(N >= 0 && N <= kSize);
167 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
168 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
169 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
170 // go through the stack.
171 using HalfT = Simd<Scalar, 4>;
172 if constexpr (N == 0) {
173 } else if constexpr (N <= 4) {
174 HalfT::Store<N>(ptr, HalfT(_mm256_castps256_ps128(v.raw))); // AVX
175 } else if constexpr (N < kSize) {
176 HalfT::Store<N - 4>(ptr + 4, HalfT(_mm256_extractf128_ps(v.raw, 1))); // AVX
177 HalfT::Store<4>(ptr, HalfT(_mm256_castps256_ps128(v.raw))); // AVX
178 } else {
179 _mm256_storeu_ps(ptr, v.raw); // AVX
180 }
181 }
182
183 template <int kTupleCount = kSize>
184 MOCHI_FORCE_INLINE static void
185 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
186 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
187 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 8);
188 constexpr int kCount1 = Clamp(kTupleCount * 3 - 8, 0, 8);
189 constexpr int kCount2 = Clamp(kTupleCount * 3 - 16, 0, 8);
190
191 // [ 0, 1, 2, 3, 4, 5, 6, 7]
192 auto a = Simd::Load<kCount0>(ptr).raw;
193 // [ 8, 9, 10, 11, 12, 13, 14, 15]
194 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 8).raw;
195 // [16, 17, 18, 19, 20, 21, 22, 23]
196 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 16).raw;
197 Deinterleave3(a, b, c, out0, out1, out2);
198 }
199
200 MOCHI_FORCE_INLINE static void
201 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
202 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
203 int const n = count * 3;
204 auto a = Simd::Load(ptr, superdex::Min(n, 8)).raw;
205 auto b = Simd::Load(n > 8 ? ptr + 8 : ptr, Clamp(n - 8, 0, 8)).raw;
206 auto c = Simd::Load(n > 16 ? ptr + 16 : ptr, Clamp(n - 16, 0, 8)).raw;
207 Deinterleave3(a, b, c, out0, out1, out2);
208 }
209
210 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
211 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
212#if MOCHI_ARCH_X64_AVX512
213 _mm256_mask_storeu_ps(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
214#else
215 // With AVX2, this is faster than masked store for a predictable value of n.
216 // It is much slower for a random value of n.
217 switch (n) { // clang-format off
218 case 1: Store<1>(ptr, v); break;
219 case 2: Store<2>(ptr, v); break;
220 case 3: Store<3>(ptr, v); break;
221 case 4: Store<4>(ptr, v); break;
222 case 5: Store<5>(ptr, v); break;
223 case 6: Store<6>(ptr, v); break;
224 case 7: Store<7>(ptr, v); break;
225 case 8: Store<8>(ptr, v); break;
226 MOCHI_UNLIKELY default: break;
227 } // clang-format on
228#endif
229 }
230
231 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
232 auto mask = ToMask(condition);
233 // Load 8 bytes from the table, then zero-exend to get the shuffle pattern.
234 auto const* tableRow =
235 reinterpret_cast<__m128i const*>(x64_simd::kStoreSelectedShuffleTableS8[mask]);
236 auto pattern = _mm256_cvtepu8_epi32(_mm_loadl_epi64(tableRow));
237 auto packed = _mm256_permutevar8x32_ps(values.raw, pattern);
238 _mm256_storeu_ps(ptr, packed);
239 return _mm_popcnt_u32(mask);
240 }
241
242 template <int kTupleCount = kSize>
243 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
244 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
245 Simd x0, x1, x2;
246 Interleave3(a, b, c, x0, x1, x2);
247 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 8);
248 constexpr int kCount1 = Clamp(kTupleCount * 3 - 8, 0, 8);
249 constexpr int kCount2 = Clamp(kTupleCount * 3 - 16, 0, 8);
250 Simd::Store<kCount0>(ptr, x0);
251 if constexpr (kCount1 > 0) {
252 Simd::Store<kCount1>(ptr + 8, x1);
253 }
254 if constexpr (kCount2 > 0) {
255 Simd::Store<kCount2>(ptr + 16, x2);
256 }
257 }
258
259 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
260 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
261 Simd x0, x1, x2;
262 Interleave3(a, b, c, x0, x1, x2);
263#if MOCHI_ARCH_X64_AVX512
264 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
265 // back into branches. A zero mask stores nothing.
266 uint32_t const bits = (1u << (count * 3)) - 1;
267 _mm256_mask_storeu_ps(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
268 _mm256_mask_storeu_ps(ptr + (count > 2 ? 8 : 0), static_cast<__mmask8>(bits >> 8), x1.raw);
269 _mm256_mask_storeu_ps(ptr + (count > 5 ? 16 : 0), static_cast<__mmask8>(bits >> 16), x2.raw);
270#else
271 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
272 // counts, which clang may turn back into branches. A zero mask stores nothing.
273 auto const n = _mm256_set1_epi32(count * 3);
274 auto const mask0 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7)); // AVX2
275 auto const mask1 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(8, 9, 10, 11, 12, 13, 14, 15));
276 auto const mask2 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(16, 17, 18, 19, 20, 21, 22, 23));
277 _mm256_maskstore_ps(ptr, mask0, x0.raw); // AVX
278 _mm256_maskstore_ps(ptr + (count > 2 ? 8 : 0), mask1, x1.raw);
279 _mm256_maskstore_ps(ptr + (count > 5 ? 16 : 0), mask2, x2.raw);
280#endif
281 }
282
283 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
284 return _mm256_blendv_ps(b.raw, a.raw, mask.raw); // AVX
285 }
286
287 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
288 return _mm256_sqrt_ps(v.raw); // AVX
289 }
290
291 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
292 return _mm256_rcp_ps(v.raw); // AVX
293 }
294
295 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
296 return _mm256_rsqrt_ps(v.raw); // AVX
297 }
298
299 // Broadcast the value -0.0. Use this in bitwise operations to affect just the sign bit.
300 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
301 return _mm256_castsi256_ps(_mm256_set1_epi32(0x80000000)); // AVX, AVX
302 }
303
304 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
305 return _mm256_andnot_ps(SignBitMask().raw, v.raw); // AVX
306 }
307
308 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
309 return _mm256_min_ps(a.raw, b.raw); // AVX
310 }
311
312 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
313 return _mm256_max_ps(a.raw, b.raw); // AVX
314 }
315
316 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
317 return _mm256_floor_ps(a.raw); // AVX
318 }
319
320 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
321 return _mm256_round_ps(v.raw, _MM_FROUND_TO_NEAREST_INT); // AVX
322 }
323
324#if MOCHI_ARCH_X64_SVML
325 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
326 return _mm256_cos_ps(a.raw); // AVX
327 }
328
329 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
330 return _mm256_sin_ps(a.raw); // AVX
331 }
332
333 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
334 return _mm256_tan_ps(a.raw); // AVX
335 }
336
337 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
338 return _mm256_acos_ps(a.raw); // AVX
339 }
340
341 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
342 return _mm256_asin_ps(a.raw); // AVX
343 }
344
345 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
346 return _mm256_atan_ps(a.raw); // AVX
347 }
348
349 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
350 return _mm256_exp_ps(a.raw); // AVX
351 }
352
353 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
354 return _mm256_log_ps(a.raw); // AVX
355 }
356
357 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
358 return _mm256_tanh_ps(a.raw); // AVX
359 }
360#endif // MOCHI_ARCH_X64_SVML
361
362 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
363#if MOCHI_ARCH_X64_FMA
364 return _mm256_fmadd_ps(a.raw, b.raw, c.raw); // FMA
365#else
366 return (a * b) + c;
367#endif
368 }
369
370 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
371#if MOCHI_ARCH_X64_FMA
372 return _mm256_fmsub_ps(a.raw, b.raw, c.raw); // FMA
373#else
374 return (a * b) - c;
375#endif
376 }
377
378 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
379#if MOCHI_ARCH_X64_FMA
380 return _mm256_fnmadd_ps(a.raw, b.raw, c.raw); // FMA
381#else
382 return -(a * b) + c;
383#endif
384 }
385
386 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
387#if MOCHI_ARCH_X64_FMA
388 return _mm256_fnmsub_ps(a.raw, b.raw, c.raw); // FMA
389#else
390 return -(a * b) - c;
391#endif
392 }
393
394 template <int N = 8>
395 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
396 static_assert(N >= 2 && N <= 8, "Unsupported N");
397 using HalfT = Simd<Scalar, 4>;
398 if constexpr (N >= 2 && N <= 4) {
399 return HalfT::HMin<N>(GetHalf<0>(a));
400 } else {
401 auto lo = GetHalf<0>(a);
402 auto hi = GetHalf<1>(a);
403 if constexpr (N != 8) {
404 // Set the hi values we don't want to compare to infinity
405 auto inf = HalfT{std::numeric_limits<Scalar>::infinity()};
406 hi = HalfT::Blend<1, N >= 6, N >= 7, 0>(inf, hi);
407 }
408 return HalfT::HMin<4>(HalfT::Min(lo, hi));
409 }
410 }
411
412 template <int N = 8>
413 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
414 static_assert(N >= 2 && N <= 8, "Unsupported N");
415 using HalfT = Simd<Scalar, 4>;
416 if constexpr (N >= 2 && N <= 4) {
417 return HalfT::HMax<N>(GetHalf<0>(a));
418 } else {
419 auto lo = GetHalf<0>(a);
420 auto hi = GetHalf<1>(a);
421 if constexpr (N != 8) {
422 // Set the hi values we don't want to compare to -infinity
423 auto inf = HalfT{std::numeric_limits<Scalar>::infinity()};
424 hi = HalfT::Blend<1, N >= 6, N >= 7, 0>(-inf, hi);
425 }
426 return HalfT::HMax<4>(HalfT::Max(lo, hi));
427 }
428 }
429
430 template <int N>
431 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
432 static_assert(N >= 2 && N <= 8, "Unsupported N");
433 using HalfT = Simd<float, 4>;
434 if constexpr (N >= 2 && N <= 4) {
435 return HalfT::HSum<N>(GetHalf<0>(a));
436 } else if constexpr (N == 5) {
437 auto tmp = GetHalf<0>(a) + GetHalf<1>(a);
438 return HalfT::Get<0>(tmp) + Get<1>(a) + Get<2>(a) + Get<3>(a);
439 } else if constexpr (N == 6) {
440 auto tmp = GetHalf<0>(a) + GetHalf<1>(a);
441 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp) + Get<2>(a) + Get<3>(a);
442 } else if constexpr (N == 7) {
443 auto tmp = GetHalf<0>(a) + GetHalf<1>(a);
444 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp) + HalfT::Get<2>(tmp) + Get<3>(a);
445 } else if constexpr (N == 8) {
446 // PERF NOTE: Alternatively _mm256_dp_ps could be used to compute the dot product with
447 // Simd{1.0f}. In comparison, this implementation takes 2 extra instructions, but it had ~29%
448 // higher throughput and ~25% lower latency, when benchmarked on an AMD CPU.
449 return HalfT::HSum<4>(GetHalf<0>(a) + GetHalf<1>(a));
450 }
451 }
452
453 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<float, 4> HSumEach(Simd a, Simd b, Simd c, Simd d) {
454 // In each 128-bit half: ab = {a[0] + a[2], a[1] + a[3], b[2] + b[0], b[3] + b[1]}, and cd
455 // likewise.
456 auto const ab = _mm256_add_ps(
457 _mm256_blend_ps(a.raw, b.raw, 0b11001100),
458 _mm256_shuffle_ps(a.raw, b.raw, _MM_SHUFFLE(1, 0, 3, 2))); // AVX
459 auto const cd = _mm256_add_ps(
460 _mm256_blend_ps(c.raw, d.raw, 0b11001100),
461 _mm256_shuffle_ps(c.raw, d.raw, _MM_SHUFFLE(1, 0, 3, 2))); // AVX
462 // In each 128-bit half: {ab[0] + ab[1], ab[2] + ab[3], cd[0] + cd[1], cd[2] + cd[3]}.
463 auto const sums = _mm256_hadd_ps(ab, cd); // AVX
464 return _mm_add_ps(_mm256_castps256_ps128(sums), _mm256_extractf128_ps(sums, 1)); // AVX
465 }
466
467 template <int N>
468 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
469 static_assert(N == 8, "smaller dot products not yet supported on Vec8f");
470#if MOCHI_COMPILER_CLANG
471 auto ab_half = _mm256_dp_ps(a.raw, b.raw, -1); // AVX
472#else
473 auto ab_half = _mm256_dp_ps(a.raw, b.raw, 0xFF); // AVX
474#endif
475 auto lo_ab = _mm256_extractf128_ps(ab_half, 0); // AVX
476 auto hi_ab = _mm256_extractf128_ps(ab_half, 1); // AVX
477 auto my_dot = _mm_add_ps(lo_ab, hi_ab); // SSE
478 return _mm256_set_m128(my_dot, my_dot); // AVX
479 }
480
481 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
482 return _mm256_cmp_ps(this->raw, rhs.raw, _CMP_LT_OQ); // AVX
483 }
484
485 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
486 return _mm256_cmp_ps(this->raw, rhs.raw, _CMP_GT_OQ); // AVX
487 }
488
489 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
490 return _mm256_cmp_ps(this->raw, rhs.raw, _CMP_LE_OQ); // AVX
491 }
492
493 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
494 return _mm256_cmp_ps(this->raw, rhs.raw, _CMP_GE_OQ); // AVX
495 }
496
497 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
498 return _mm256_cmp_ps(a.raw, b.raw, _CMP_EQ_OQ); // AVX
499 }
500
501 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
502 return _mm256_cmp_ps(a.raw, b.raw, _CMP_NEQ_UQ); // AVX
503 }
504
505 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
506 return _mm256_setzero_ps(); // AVX
507 }
508
509 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
510 return ToMask(Equal(*this, rhs)) == 0xFF; // All values equal
511 }
512
513 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
514 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
515 }
516
517 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
518 auto ones = _mm256_castsi256_ps(_mm256_set1_epi32(-1)); // AVX, AVX
519 return _mm256_xor_ps(raw, ones); // AVX
520 }
521
522 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
523 return Zero() - *this;
524 }
525
526 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
527 return _mm256_add_ps(raw, rhs.raw); // AVX
528 }
529
530 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
531 return _mm256_sub_ps(raw, rhs.raw); // AVX
532 }
533
534 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
535 return _mm256_mul_ps(raw, rhs.raw); // AVX
536 }
537
538 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
539 return _mm256_div_ps(raw, rhs.raw); // AVX
540 }
541
542 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
543 return _mm256_and_ps(raw, rhs.raw); // AVX
544 }
545
546 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
547 return _mm256_or_ps(raw, rhs.raw); // AVX
548 }
549
550 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
551 return _mm256_xor_ps(raw, rhs.raw); // AVX
552 }
553
554 private:
555 // One bit per lane, from the lane's sign bit.
556 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
557 return _mm256_movemask_ps(a.raw); // AVX
558 }
559
560 // Splits 24 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
561 MOCHI_FORCE_INLINE static void
562 Deinterleave3(__m256 a, __m256 b, __m256 c, Simd& out0, Simd& out1, Simd& out2) {
563 auto d = _mm256_blend_ps(a, b, 0b10010010); // [0,9,_,3, 12,_,6,15]
564 auto e = _mm256_blend_ps(d, c, 0b00100100); // [0,9,18,3, 12,21,6,15]
565 auto f = _mm256_permute2f128_ps(e, e, 0x01); // [12,21,6,15, 0,9,18,3]
566 auto g = _mm256_blend_ps(e, f, 0b01000100); // [0,9,6,3, 12,21,18,15]
567 out0.raw = _mm256_shuffle_ps(g, g, _MM_SHUFFLE(1, 2, 3, 0)); // [0,3,6,9, 12,15,18,21]
568
569 d = _mm256_blend_ps(a, b, 0b00100100); // [_,1,10,_, 4,13,_,7]
570 e = _mm256_blend_ps(d, c, 0b01001001); // [16,1,10,19 4,13,22,7]
571 f = _mm256_permute2f128_ps(e, e, 0x01); // [4,13,22,7 16,1,10,19]
572 g = _mm256_blend_ps(e, f, 0b10011001); // [4,1,10,7, 16,13,22,19]
573 out1.raw = _mm256_shuffle_ps(g, g, _MM_SHUFFLE(2, 3, 0, 1)); // [1,4,7,10, 13,16,19,22]
574
575 d = _mm256_blend_ps(a, b, 0b01001001); // [8,_,2,11, _,5,14,_]
576 e = _mm256_blend_ps(d, c, 0b10010010); // [8,17,2,11, 20,5,14,23]
577 f = _mm256_permute2f128_ps(e, e, 0x01); // [20,5,14,23, 8,17,2,11]
578 g = _mm256_blend_ps(e, f, 0b00100010); // [8,5,2,11, 20,17,14,23]
579 out2.raw = _mm256_shuffle_ps(g, g, _MM_SHUFFLE(3, 0, 1, 2)); // [2,5,8,11, 14,17,20,23]
580 }
581
582 // Inverse of Deinterleave3: out0, out1, out2 are the 24 interleaved scalars in memory order.
583 MOCHI_FORCE_INLINE static void
584 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
585 // a = [0,3,6,9, 12,15,18,21]
586 // b = [1,4,7,10, 13,16,19,22]
587 // c = [2,5,8,11, 14,17,20,23]
588 auto d = _mm256_shuffle_ps(a.raw, a.raw, _MM_SHUFFLE(1, 2, 3, 0)); // [0,9,6,3, 12,21,18,15]
589 auto e = _mm256_shuffle_ps(b.raw, b.raw, _MM_SHUFFLE(2, 3, 0, 1)); // [4,1,10,7, 16,13,22,19]
590 auto f = _mm256_shuffle_ps(c.raw, c.raw, _MM_SHUFFLE(3, 0, 1, 2)); // [8,5,2,11, 20,17,14,23]
591 auto g = _mm256_blend_ps(d, e, 0b00100010); // [0,1,_,3, 12,13,_,15]
592 g = _mm256_blend_ps(g, f, 0b01000100); // [0,1,2,3, 12,13,14,15]
593 auto h = _mm256_blend_ps(d, e, 0b10011001); // [4,_,6,7, 16,_,18,19]
594 h = _mm256_blend_ps(h, f, 0b00100010); // [4,5,6,7, 16,17,18,19]
595 h = _mm256_permute2f128_ps(h, h, 0x01); // [16,17,18,19, 4,5,6,7]
596 auto i = _mm256_blend_ps(d, e, 0b01000100); // [_,9,10,_, _,21,22,_]
597 i = _mm256_blend_ps(i, f, 0b10011001); // [8,9,10,11, 20,21,22,23]
598 out0 = _mm256_blend_ps(g, h, 0b11110000); // [0,1,2,3, 4,5,6,7]
599 out1 = _mm256_blend_ps(i, g, 0b11110000); // [8,9,10,11, 12,13,14,15]
600 out2 = _mm256_blend_ps(h, i, 0b11110000); // [16,17,18,19, 20,21,22,23]
601 }
602};
603
604} // namespace superdex
605
606#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 > 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)
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)
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Definition simd_inl.h:322
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)