SuperDex Physics C++ API
Loading...
Searching...
No Matches
simd_composite_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 "../simd.h" // for IntelliSense
20
21#include <type_traits>
22#include <utility>
23
24namespace superdex {
25
26namespace details {
27
28template <class T, int N, int Candidate>
29[[nodiscard]] constexpr int FindCompositeFirstSize() {
30 if constexpr (Candidate <= 0) {
31 return 0;
32 } else if constexpr (Candidate >= N) {
33 return FindCompositeFirstSize<T, N, Candidate / 2>();
34 } else if constexpr (
36 Simd<T, N - Candidate>::kIsSupported) {
37 return Candidate;
38 } else {
39 return FindCompositeFirstSize<T, N, Candidate / 2>();
40 }
41}
42
43template <class T, int N>
44inline constexpr int kCompositeFirstSize = FindCompositeFirstSize<T, N, kSimdDefaultSize<T>>();
45
46template <class V, size_t... Is>
47[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE V MakeSimdSequence(std::index_sequence<Is...>) {
48 return V{static_cast<typename V::Scalar>(Is)...};
49}
50
51} // namespace details
52
53/**
54 Simd partial specialization for larger values of N.
55 Supports any N as long as it can be composed of smaller supported Simd objects.
56*/
57template <class T, int N>
58class Simd<T, N, std::enable_if_t<(details::kCompositeFirstSize<T, N> > 0), SimdConcept>> {
59 public:
60 using Scalar = T;
61
62 // Data storage is split into two parts
64 using First = NativeType; // Largest native size that leaves a supported remainder
65 using Second = Simd<T, N - First::kSize>; // What's left
68
69 static constexpr int kSize = N;
70 static constexpr int kSizeFirst = First::kSize;
71 static constexpr int kSizeSecond = Second::kSize;
73 static constexpr bool kIsComposite = true;
75 static_assert(
77 "Inconsistent SIMD emulation flags");
78
79 // Default construct (may be uninitialized)
81
82 // Copy construct
84
85 // Construct from Simd parts
86 MOCHI_ANY MOCHI_FORCE_INLINE Simd(First const& p0, Second const& p1) : first(p0), second(p1) {}
87
88 // Construct from halves, independent of this type's composite tree shape.
89 template <
90 class Half,
92 (IsSimd<Half> && std::is_same_v<Scalar, typename Half::Scalar> &&
93 (Half::kSize * 2 == kSize)))>
94 MOCHI_ANY MOCHI_FORCE_INLINE Simd(Half const& a, Half const& b) {
95 if constexpr (Half::kIsComposite && Second::kIsComposite) {
96 using SecondTail = typename Second::Second;
97 if constexpr (SecondTail::kIsComposite) {
98 if constexpr (
99 std::is_same_v<First, typename Half::First> &&
100 std::is_same_v<typename Second::First, typename Half::Second> &&
101 std::is_same_v<typename SecondTail::First, typename Half::First> &&
102 std::is_same_v<typename SecondTail::Second, typename Half::Second>) {
103 first = a.first;
104 second.first = a.second;
105 second.second.first = b.first;
106 second.second.second = b.second;
107 return;
108 }
109 }
110 }
111 alignas(First) Scalar values[kSize];
112 Half::Store(values, a);
113 Half::Store(values + Half::kSize, b);
114 first = First::Load(values);
115 second = Second::Load(values + kSizeFirst);
116 }
117
118 // Construct by broadcasting a scalar
119 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
121
122 // Construct from 2 scalars. Any unspecified values are zero.
124
125 // Construct from 3 or 4 scalars. Any unspecified values are zero.
127 if constexpr (kSizeFirst == 2) {
128 first = First{a, b};
129 second = Second{c, d};
130 } else {
131 first = First{a, b, c, d};
132 second = Second{};
133 }
134 }
135
136 // Construct from 5 or 6 scalars. Any unspecified values are zero.
139 if constexpr (kSizeFirst == 2) {
140 first = First{a, b};
141 second = Second{c, d, e, f};
142 } else if constexpr (kSizeFirst == 4) {
143 first = First{a, b, c, d};
144 second = Second{e, f};
145 } else {
146 first = First{a, b, c, d, e, f};
147 second = {};
148 }
149 }
150
151 // Construct from 7 scalars. Any unspecified values are zero.
154 if constexpr (kSizeFirst == 2) {
155 first = First{a, b};
156 second = Second{c, d, e, f, g};
157 } else if constexpr (kSizeFirst == 4) {
158 first = First{a, b, c, d};
159 second = Second{e, f, g};
160 } else {
161 first = First{a, b, c, d, e, f, g};
162 second = {};
163 }
164 }
165
166 // Construct from 8 or more scalars. Any unspecified values are zero.
167 template <
168 class... MoreScalars,
170 (sizeof...(MoreScalars) + 8 <= kSize) &&
171 (kSizeFirst != 16 || sizeof...(MoreScalars) + 8 <= 16) &&
172 (std::is_convertible_v<MoreScalars, Scalar> && ...))>
174 Scalar a,
175 Scalar b,
176 Scalar c,
177 Scalar d,
178 Scalar e,
179 Scalar f,
180 Scalar g,
181 Scalar h,
182 MoreScalars... args) {
183 if constexpr (kSizeFirst == 2) {
184 first = First{a, b};
185 second = Second{c, d, e, f, g, h, args...};
186 } else if constexpr (kSizeFirst == 4) {
187 first = First{a, b, c, d};
188 second = Second{e, f, g, h, args...};
189 } else if constexpr (kSizeFirst == 8) {
190 first = First{a, b, c, d, e, f, g, h};
191 static constexpr auto kNumArgs = sizeof...(args);
192 if constexpr (kNumArgs == 0) {
193 second = Second{};
194 } else if constexpr (kNumArgs == 1) {
195 second = Second{args..., Scalar(0)};
196 } else {
197 second = Second{args...};
198 }
199 } else if constexpr (sizeof...(args) + 8 <= kSizeFirst) {
200 first = First{a, b, c, d, e, f, g, h, args...};
201 second = {};
202 } else {
203 alignas(First) Scalar lanes[kSize] = {a, b, c, d, e, f, g, h, static_cast<Scalar>(args)...};
204 first = First::Load(lanes);
205 second = Second::Load(lanes + kSizeFirst);
206 }
207 }
208
209 // Construct from 16 or more scalars. Any unspecified values are zero.
210 template <
211 class... MoreScalars,
213 (kSizeFirst == 16) && (sizeof...(MoreScalars) + 16 <= kSize) &&
214 (std::is_convertible_v<MoreScalars, Scalar> && ...))>
216 Scalar a,
217 Scalar b,
218 Scalar c,
219 Scalar d,
220 Scalar e,
221 Scalar f,
222 Scalar g,
223 Scalar h,
224 Scalar i,
225 Scalar j,
226 Scalar k,
227 Scalar l,
228 Scalar m,
229 Scalar n,
230 Scalar o,
231 Scalar p,
232 MoreScalars... args) {
233 first = First{a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p};
234 static constexpr auto kNumArgs = sizeof...(args);
235 if constexpr (kNumArgs == 0) {
236 second = Second{};
237 } else if constexpr (kNumArgs == 1) {
238 second = Second{args..., Scalar(0)};
239 } else {
240 second = Second{args...};
241 }
242 }
243
244 MOCHI_ANY MOCHI_FORCE_INLINE static constexpr size_t size() {
245 return kSize;
246 }
247
248 template <int i>
249 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar Get(Simd a) {
250 static_assert(i >= 0 && i < kSize, "Index out of range");
251 return a[i];
252 }
253
254 template <int i>
255 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE auto GetHalf(Simd a) {
256 using Half = Simd<Scalar, N / 2>;
257 static_assert(i == 0 || i == 1, "Half index out of range");
258 static_assert(N % 2 == 0, "Vector size must be even");
259 static_assert(Half::kIsSupported);
260 if constexpr (kSizeFirst == kSizeSecond) {
261 if constexpr (i == 0) {
262 return a.first;
263 } else {
264 return a.second;
265 }
266 } else {
267 if constexpr (Half::kIsComposite && Second::kIsComposite) {
268 using SecondTail = typename Second::Second;
269 if constexpr (SecondTail::kIsComposite) {
270 if constexpr (
271 std::is_same_v<First, typename Half::First> &&
272 std::is_same_v<typename Second::First, typename Half::Second> &&
273 std::is_same_v<typename SecondTail::First, typename Half::First> &&
274 std::is_same_v<typename SecondTail::Second, typename Half::Second>) {
275 if constexpr (i == 0) {
276 return Half{a.first, a.second.first};
277 } else {
278 return Half{a.second.second.first, a.second.second.second};
279 }
280 }
281 }
282 }
283 alignas(First) Scalar values[kSize];
284 Store(values, a);
285 return Half::Load(values + i * Half::kSize);
286 }
287 }
288
289 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Set(Simd a, int i, Scalar value) {
290 if (i < kSizeFirst) {
291 return {First::Set(a.first, i, value), a.second};
292 } else {
293 return {a.first, Second::Set(a.second, i - kSizeFirst, value)};
294 }
295 }
296
297 template <int i>
298 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Set(Simd a, Scalar value) {
299 if constexpr (i < kSizeFirst) {
300 return {First::template Set<i>(a.first, value), a.second};
301 } else {
302 return {a.first, Second::template Set<i - kSizeFirst>(a.second, value)};
303 }
304 }
305
306 template <int i>
308 static_assert(kSize == 4, "Unsupported size");
309 return Set<i>(Zero(), Scalar(1));
310 }
311
312 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Sequence() {
313 if constexpr (IsHalf<Scalar>) {
314 return details::MakeSimdSequence<Simd>(std::make_index_sequence<kSize>{});
315 } else {
316 alignas(First) Scalar constexpr kSequence[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
317 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
318 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
319 static_assert(First::kSize <= 32, "Vector size is too large");
320 auto first = First::Load(kSequence);
322 if constexpr (std::is_same_v<First, Second>) {
323 second = first;
324 } else if constexpr (Second::kIsComposite) {
325 second = Second::Sequence();
326 } else {
327 static_assert(Second::kSize <= 32, "Vector size is too large");
328 second = Second::Load(kSequence);
329 }
330 return Simd{first, second + Second{static_cast<T>(First::kSize)}};
331 }
332 }
333
334 template <int x = 0, int y = 1, int z = 2, int w = 3>
335 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Shuffle(Simd a, Simd b) {
336 static_assert(kSize == 4, "Unsupported size");
337 return {Get<x>(a), Get<y>(a), Get<z>(b), Get<w>(b)}; // Not optimized
338 }
339
340 template <int x = 0, int y = 1, int z = 2, int w = 3>
341 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Shuffle(Simd a) {
342 static_assert(kSize == 4, "Unsupported size");
343 return Shuffle<x, y, z, w>(a, a);
344 }
345
346 template <int SZ = kSize>
347 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE bool AllTrue(Simd a) {
348 static_assert(SZ <= kSize);
349 if constexpr (SZ <= kSizeFirst) {
350 return First::template AllTrue<SZ>(a.first);
351 } else {
352 return First::template AllTrue<kSizeFirst>(a.first) &&
353 Second::template AllTrue<SZ - kSizeFirst>(a.second);
354 }
355 }
356
357 template <int SZ = kSize>
358 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE bool AnyTrue(Simd a) {
359 if constexpr (SZ <= kSizeFirst) {
360 return First::template AnyTrue<SZ>(a.first);
361 } else {
362 return First::template AnyTrue<kSizeFirst>(a.first) ||
363 Second::template AnyTrue<SZ - kSizeFirst>(a.second);
364 }
365 }
366
368 static_assert(kSize == 4, "Unsupported size");
369 return {a.first, Second::template Set<1>(a.second, Scalar(0))};
370 }
371
372 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd AsPoint(Simd a) {
373 static_assert(kSize == 4, "Unsupported size");
374 return {a.first, Second::template Set<1>(a.second, Scalar(1))};
375 }
376
377 template <int x, int y, int z, int w>
378 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Blend(Simd a, Simd b) {
379 static_assert(kSize == 4, "Unsupported size");
380 return {
381 First::template Blend<x, y>(a.first, b.first),
382 Second::template Blend<z, w>(a.second, b.second)};
383 }
384
385 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
386 return {First::Broadcast(p), Second::Broadcast(p)};
387 }
388
390 return {First{a}, Second{a}};
391 }
392
393 template <int i>
395 if constexpr (kSizeFirst == kSizeSecond) {
396 if constexpr (i < kSizeFirst) {
397 auto x = First::template Broadcast<i>(a.first);
398 return {x, x};
399 } else {
400 auto x = Second::template Broadcast<i - kSizeFirst>(a.second);
401 return {x, x};
402 }
403 } else {
404 Scalar s = Get<i>(a); // Not optimized
405 return {First{s}, Second{s}};
406 }
407 }
408
409 template <int SZ = kSize>
410 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Load(T const* ptr) {
411 if constexpr (SZ <= kSizeFirst) {
412 return {First::template Load<SZ>(ptr), Second{}};
413 } else {
414 return {
415 First::template Load<kSizeFirst>(ptr),
416 Second::template Load<SZ - kSizeFirst>(ptr + kSizeFirst)};
417 }
418 }
419
420 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
421 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
422 if (n <= kSizeFirst) {
423 return {First::Load(ptr, n), Second{}};
424 } else {
425 return {First::Load(ptr), Second::Load(ptr + kSizeFirst, n - kSizeFirst)};
426 }
427 }
428
429 template <typename IntT, MOCHI_CONCEPT(std::is_integral_v<IntT>)>
430 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd
431 LoadIndexed(Scalar const* ptr, Simd<IntT, kSize> const& indices) {
432 using IMatch = std::conditional_t<sizeof(Scalar) == 4, int, int64_t>; // int same size as Scalar
433 using IVec = Simd<IMatch, kSize>;
434 static_assert(IVec::kIsComposite && sizeof(IVec) == sizeof(Simd));
435 auto matchingIndices = StaticCast<IVec>(indices); // maybe no change
436 return {
437 First::LoadIndexed(ptr, matchingIndices.first),
438 Second::LoadIndexed(ptr, matchingIndices.second)};
439 }
440
441 template <int kTupleCount = kSize, class... OutputVectors>
443 Scalar const* ptr,
444 OutputVectors&... out) {
445 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
446 if constexpr (kTupleCount <= kSizeFirst) {
447 First::template LoadTransposed<kTupleCount>(ptr, out.first...);
448 ((out.second = {}), ...); // Zero-fill the second part
449 } else {
450 First::template LoadTransposed<kSizeFirst>(ptr, out.first...);
451 Second::template LoadTransposed<kTupleCount - kSizeFirst>(
452 ptr + sizeof...(OutputVectors) * kSizeFirst, out.second...);
453 }
454 }
455
456 static MOCHI_ANY MOCHI_FORCE_INLINE void
457 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
458 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
459 if (count <= kSizeFirst) {
460 First::LoadTransposed(ptr, out0.first, out1.first, out2.first, count);
461 out0.second = out1.second = out2.second = Second{};
462 } else {
463 First::LoadTransposed(ptr, out0.first, out1.first, out2.first);
464 Second::LoadTransposed(
465 ptr + 3 * kSizeFirst, out0.second, out1.second, out2.second, count - kSizeFirst);
466 }
467 }
468
469 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
470 return {
471 First::Select(mask.first, a.first, b.first),
472 Second::Select(mask.second, a.second, b.second)};
473 }
474
475 template <int kShift>
477 static_assert(kShift >= 0 && kShift < (8 * sizeof(T)), "Shift amount out-of-range");
478 if constexpr (kShift == 0) {
479 return a;
480 } else {
481 return {
482 First::template ShiftRight<kShift>(a.first),
483 Second::template ShiftRight<kShift>(a.second)};
484 }
485 }
486
487 template <int SZ = kSize>
489 if constexpr (SZ <= kSizeFirst) {
490 First::template Store<SZ>(ptr, a.first);
491 } else {
492 First::template Store<kSizeFirst>(ptr, a.first);
493 Second::template Store<SZ - kSizeFirst>(ptr + kSizeFirst, a.second);
494 }
495 }
496
497 static MOCHI_ANY MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
498 // Hopefully the compiler can figure out the best thing to do here.
499 // The alternative is to have a runtime branch at each stage.
500 memcpy(ptr, &v, sizeof(Scalar) * n);
501 }
502
503 static MOCHI_ANY MOCHI_FORCE_INLINE int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
504 int count = First::StoreSelected(ptr, condition.first, values.first);
505 count += Second::StoreSelected(ptr + count, condition.second, values.second);
506 return count;
507 }
508
509 template <int kTupleCount = kSize, class... InputVectors>
510 static MOCHI_ANY MOCHI_FORCE_INLINE void StoreTransposed(Scalar* ptr, InputVectors... v) {
511 First::template StoreTransposed<superdex::Min(kTupleCount, First::kSize)>(ptr, v.first...);
512 if constexpr (kTupleCount > First::kSize) {
513 Second::template StoreTransposed<kTupleCount - First::kSize>(
514 ptr + sizeof...(InputVectors) * kSizeFirst, v.second...);
515 }
516 }
517
518 static MOCHI_ANY MOCHI_FORCE_INLINE void
519 StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
520 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
521 if (count <= kSizeFirst) {
522 First::StoreTransposed(ptr, a.first, b.first, c.first, count);
523 } else {
524 First::StoreTransposed(ptr, a.first, b.first, c.first);
525 Second::StoreTransposed(
526 ptr + 3 * kSizeFirst, a.second, b.second, c.second, count - kSizeFirst);
527 }
528 }
529
531 return {First::SignBitMask(), Second::SignBitMask()};
532 }
533
534 template <int SZ = kSize>
535 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
536 if constexpr (SZ <= kSizeFirst) {
537 return First::template HMin<SZ>(a.first);
538 } else if constexpr (SZ - kSizeFirst == 1) {
539 // HMin<1> is not normally supported, so use a scalar
540 return superdex::Min(
541 First::template HMin<kSizeFirst>(a.first), Second::template Get<0>(a.second));
542 } else if constexpr (kSizeFirst == kSizeSecond && SZ == kSize) {
543 // Special case for 2x native size. One horizontal operation.
544 return First::template HMin<kSizeFirst>(First::Min(a.first, a.second));
545 } else if constexpr (kSizeFirst * 2 == kSizeSecond && SZ == kSize) {
546 // Special case for 3x native size. One horizontal operation.
547 return First::template HMin<kSizeFirst>(
548 First::Min(a.first, First::Min(a.second.first, a.second.second)));
549 } else if constexpr (kSizeFirst * 3 == kSizeSecond && SZ == kSize) {
550 // Special case for 4x native size. One horizontal operation.
551 return First::template HMin<kSizeFirst>(First::Min(
552 a.first,
553 First::Min(a.second.first, First::Min(a.second.second.first, a.second.second.second))));
554 } else {
555 // May not be as fast due to repeated horizontal operations
556 return superdex::Min(
557 First::template HMin<kSizeFirst>(a.first),
558 Second::template HMin<SZ - kSizeFirst>(a.second));
559 }
560 }
561
562 template <int SZ = kSize>
563 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
564 if constexpr (SZ <= kSizeFirst) {
565 return First::template HMax<SZ>(a.first);
566 } else if constexpr (SZ - kSizeFirst == 1) {
567 // HMax<1> is not normally supported, so use a scalar
568 return superdex::Max(
569 First::template HMax<kSizeFirst>(a.first), Second::template Get<0>(a.second));
570 } else if constexpr (kSizeFirst == kSizeSecond && SZ == kSize) {
571 // Special case for 2x native size. One horizontal operation.
572 return First::template HMax<kSizeFirst>(First::Max(a.first, a.second));
573 } else if constexpr (kSizeFirst * 2 == kSizeSecond && SZ == kSize) {
574 // Special case for 3x native size. One horizontal operation.
575 return First::template HMax<kSizeFirst>(
576 First::Max(a.first, First::Max(a.second.first, a.second.second)));
577 } else if constexpr (kSizeFirst * 3 == kSizeSecond && SZ == kSize) {
578 // Special case for 4x native size. One horizontal operation.
579 return First::template HMax<kSizeFirst>(First::Max(
580 a.first,
581 First::Max(a.second.first, First::Max(a.second.second.first, a.second.second.second))));
582 } else {
583 // May not be as fast due to repeated horizontal operations
584 return superdex::Max(
585 First::template HMax<kSizeFirst>(a.first),
586 Second::template HMax<SZ - kSizeFirst>(a.second));
587 }
588 }
589
590 template <int SZ = kSize>
591 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
592 if constexpr (SZ <= kSizeFirst) {
593 return First::template HSum<SZ>(a.first);
594 } else if constexpr (SZ - kSizeFirst == 1) {
595 // HSum<1> is not normally supported, so add a scalar
596 return First::template HSum<kSizeFirst>(a.first) + Second::template Get<0>(a.second);
597 } else if constexpr (kSizeFirst == kSizeSecond && SZ == kSize) {
598 // Special case for 2x native size. One horizontal operation.
599 return First::template HSum<kSizeFirst>(a.first + a.second);
600 } else if constexpr (kSizeFirst * 2 == kSizeSecond && SZ == kSize) {
601 // Special case for 3x native size. One horizontal operation.
602 return First::template HSum<kSizeFirst>(a.first + a.second.first + a.second.second);
603 } else if constexpr (kSizeFirst * 3 == kSizeSecond && SZ == kSize) {
604 // Special case for 4x native size. One horizontal operation.
605 return First::template HSum<kSizeFirst>(
606 a.first + a.second.first + a.second.second.first + a.second.second.second);
607 } else {
608 // May not be as fast due to repeated horizontal operations
609 return First::template HSum<kSizeFirst>(a.first) +
610 Second::template HSum<SZ - kSizeFirst>(a.second);
611 }
612 }
613
614 // Constrained to 4 lanes in its declaration rather than by a static_assert, so that
615 // details::kHasOwnHSumEach is false at other sizes, and the generic HSumEach adds their halves.
616 template <int SZ = kSize, std::enable_if_t<SZ == 4, int> = 0>
617 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b, Simd c, Simd d) {
618 static_assert(kSizeFirst == kSizeSecond, "Unsupported size");
619 // Like HSum, adds the halves of each vector first.
620 return {
621 First::HSumEach(a.first + a.second, b.first + b.second),
622 First::HSumEach(c.first + c.second, d.first + d.second)};
623 }
624
625 template <int SZ = kSize>
626 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
627 if constexpr (SZ <= kSizeFirst) {
628 return First::template HProd<SZ>(a.first);
629 } else if constexpr (SZ - kSizeFirst == 1) {
630 // HProd<1> is not normally supported, so multiply a scalar
631 return First::template HProd<kSizeFirst>(a.first) * Second::template Get<0>(a.second);
632 } else if constexpr (kSizeFirst == kSizeSecond && SZ == kSize) {
633 return First::template HProd<kSizeFirst>(a.first * a.second);
634 } else {
635 // May not be as fast due to repeated horizontal operations
636 return First::template HProd<kSizeFirst>(a.first) *
637 Second::template HProd<SZ - kSizeFirst>(a.second);
638 }
639 }
640
641 template <int SZ = kSize>
642 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
643 return Broadcast(HSum<SZ>(a * b)); // Use HSum to hopefully avoid repeated horizontal operations
644 }
645
646 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Zero() {
647 return {First::Zero(), Second::Zero()};
648 }
649
651
652 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
654 first = rhs;
655 second = rhs;
656 return *this;
657 }
658
659 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
660 if constexpr (kSizeFirst == kSizeSecond) {
661 // Special case for 2x native size. Just one AllTrue.
662 return First::template AllTrue<kSizeFirst>(
663 First::Equal(this->first, rhs.first) & Second::Equal(this->second, rhs.second));
664 } else {
665 return (this->first == rhs.first) && (this->second == rhs.second);
666 }
667 }
668
669 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
670 return !(*this == rhs);
671 }
672
673 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Scalar operator[](int i) const {
674 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
675 return i < kSizeFirst ? first[i] : second[i - kSizeFirst];
676 }
677
678 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator<<(int i) const {
679 return {this->first << i, this->second << i};
680 }
681
683 this->first <<= i;
684 this->second <<= i;
685 return *this;
686 }
687
688 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator>>(int i) const {
689 return {this->first >> i, this->second >> i};
690 }
691
693 this->first >>= i;
694 this->second >>= i;
695 return *this;
696 }
697
698#define MOCHI_SIMD_COMPOSITE_FN_1(FnName) \
699 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a) { \
700 return {First::FnName(a.first), Second::FnName(a.second)}; \
701 }
702
703#define MOCHI_SIMD_COMPOSITE_FN_2(FnName) \
704 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a, Simd b) { \
705 return {First::FnName(a.first, b.first), Second::FnName(a.second, b.second)}; \
706 }
707
708#define MOCHI_SIMD_COMPOSITE_FN_3(FnName) \
709 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a, Simd b, Simd c) { \
710 return { \
711 First::FnName(a.first, b.first, c.first), Second::FnName(a.second, b.second, c.second)}; \
712 }
713
714#define MOCHI_SIMD_COMPOSITE_OP_1(OP) \
715 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator OP() const { \
716 return {OP this->first, OP this->second}; \
717 }
718
719#define MOCHI_SIMD_COMPOSITE_OP_2(OP) \
720 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator OP(Simd rhs) const { \
721 return {this->first OP rhs.first, this->second OP rhs.second}; \
722 }
723
724#define MOCHI_SIMD_COMPOSITE_OP_EQ(OP_EQ, OP) \
725 MOCHI_ANY MOCHI_FORCE_INLINE Simd& operator OP_EQ(Simd a) { \
726 this->first = this->first OP a.first; \
727 this->second = this->second OP a.second; \
728 return *this; \
729 }
730
731#define MOCHI_SIMD_COMPOSITE_OP_EQ_WITH_SCALAR(OP_EQ, OP) \
732 MOCHI_SIMD_COMPOSITE_OP_EQ(OP_EQ, OP); \
733 MOCHI_ANY MOCHI_FORCE_INLINE Simd& operator OP_EQ(Scalar a) { \
734 this->first = this->first OP First{a}; \
735 this->second = this->second OP Second{a}; \
736 return *this; \
737 }
738
739 // Unary functions:
755
756 // Binary functions
761
762 // Ternary functions
767
768 // Unary operators
771
772 // Binary operators
784
785 // Math assignment operators
786 //
787 // VS2019 WORKAROUND: These operators should not be necessary because there are generic templates
788 // in simd_inl.h which convert operations like `a += b;` into `a = a + b;`. However, there is
789 // a bug in the VS2019 compiler resulting in incorrect runtime behavior in some of these cases.
790 // The operators here take precedence and shortcut a couple layers of template abstraction,
791 // which appears to be a sufficient work-around. The bug was only observed with VS2019
792 // optimized builds, not with VS2022, Clang, nor GCC.
800
801#undef MOCHI_SIMD_COMPOSITE_FN_1
802#undef MOCHI_SIMD_COMPOSITE_FN_2
803#undef MOCHI_SIMD_COMPOSITE_FN_3
804#undef MOCHI_SIMD_COMPOSITE_OP_1
805#undef MOCHI_SIMD_COMPOSITE_OP_2
806#undef MOCHI_SIMD_COMPOSITE_OP_EQ
807#undef MOCHI_SIMD_COMPOSITE_OP_EQ_WITH_SCALAR
808};
809
810// ReinterpretCast for composite Simd types
811template <
812 class To,
813 class FromT,
814 int FromN,
815 MOCHI_CONCEPT((Simd<FromT, FromN>::kIsComposite) && To::kIsComposite)>
816[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To ReinterpretCast(Simd<FromT, FromN> const& a) {
817 using From = Simd<FromT, FromN>;
818 static_assert(sizeof(typename To::Scalar) * To::kSize == sizeof(FromT) * FromN, "Size mismatch");
819 static_assert(
820 sizeof(typename To::Scalar) * To::kSizeFirst == sizeof(FromT) * From::kSizeFirst,
821 "Component size mismatch");
822 return {
823 ReinterpretCast<typename To::First>(a.first), ReinterpretCast<typename To::Second>(a.second)};
824}
825
826namespace details {
827template <typename T>
828[[nodiscard]] constexpr bool IsCompositeWithEqualHalves() {
829 if constexpr (T::kIsComposite) {
830 return T::kSizeFirst == T::kSizeSecond;
831 } else {
832 return false;
833 }
834}
835
836template <class To, class From, std::size_t... Is>
837[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To
838StaticCastElementwise(From const& a, std::index_sequence<Is...>) {
839 return To{static_cast<typename To::Scalar>(a[Is])...};
840}
841} // namespace details
842
843// StaticCast when at least one of the types is a Simd composite.
844// Also handles the pass-through case where To and From are the same.
845template <class To, class From, MOCHI_CONCEPT_DEF(IsSimd<To>&& IsSimd<From>)>
846[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To StaticCast(From const& a) {
847 static_assert(To::kSize == From::kSize, "Size mismatch");
848 static_assert(To::kIsSupported, "Unsupported type");
849 using ToHalf = Simd<typename To::Scalar, To::kSize / 2>;
850 using FromHalf = Simd<typename From::Scalar, From::kSize / 2>;
851 if constexpr (std::is_same_v<typename To::Scalar, typename From::Scalar>) {
852 return a; // No change
853 } else if constexpr (
854 To::kIsComposite && From::kIsComposite &&
855 (sizeof(typename To::Scalar) == sizeof(typename From::Scalar))) {
856 // Casting a composite to another composite, where the native sizes match
857 // Examples: Vec16f <--> Vec16i
859 } else if constexpr (
860 From::kIsComposite && !To::kIsComposite && ToHalf::kIsSupported &&
861 (sizeof(typename To::Scalar) * 2 == sizeof(typename From::Scalar))) {
862 // Casting from a composite to a native vector, where the destination scalar type is smaller
863 // Examples: Vec8i <-- Vec8d, Vec8f <-- Vec8d (using AVX)
864 return {StaticCast<ToHalf>(a.first), StaticCast<ToHalf>(a.second)};
865 } else if constexpr (
866 To::kIsComposite && !From::kIsComposite && FromHalf::kIsSupported &&
867 (sizeof(typename To::Scalar) == 2 * sizeof(typename From::Scalar))) {
868 // Casting from a native size to a composite, where the destination scalar type is larger
869 // Examples: Vec8d <-- Vec8i, Vec8d <-- Vec8f (using AVX)
870 return {
872 StaticCast<typename To::Second>(From::template GetHalf<1>(a))}; // Not optimized
873 } else if constexpr (
874 ::superdex::details::IsCompositeWithEqualHalves<From>() &&
875 ::superdex::details::IsCompositeWithEqualHalves<To>() &&
876 sizeof(typename To::Scalar) != sizeof(typename From::Scalar)) {
877 // Both sides are composite with equal-sized halves (i.e. GetHalf<0/1> can split them
878 // symmetrically), but their tree shapes may differ due to different native SIMD widths
879 // (e.g. Half↔float or double↔float on ARM), so we can't recurse via .first/.second directly.
880 return To{
881 StaticCast<ToHalf>(From::template GetHalf<0>(a)),
882 StaticCast<ToHalf>(From::template GetHalf<1>(a))};
883 } else {
884 return superdex::details::StaticCastElementwise<To>(a, std::make_index_sequence<From::kSize>{});
885 }
886}
887
888} // namespace superdex
Simd(Scalar a, Scalar b, Scalar c, Scalar d, Scalar e, Scalar f, Scalar g, Scalar h, MoreScalars... args)
Simd(Scalar a, Scalar b, Scalar c, Scalar d, Scalar e, Scalar f, Scalar g, Scalar h, Scalar i, Scalar j, Scalar k, Scalar l, Scalar m, Scalar n, Scalar o, Scalar p, MoreScalars... args)
static void LoadTransposed(Scalar const *ptr, Simd &out0, Simd &out1, Simd &out2, int count)
static constexpr bool kIsComposite
Definition simd.h:98
static constexpr bool kIsSupported
Definition simd.h:97
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
#define MOCHI_CONCEPT(a)
#define MOCHI_FORCE_INLINE
#define MOCHI_ANY
#define MOCHI_NO_INIT
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
Definition simd_inl.h:287
constexpr T const & Min(T const &a, T const &b)
T HSum(Simd< T, N > a)
Definition simd_inl.h:391
constexpr To StaticCast(From const &a)
Definition basic_utils.h:79
T HMin(Simd< T, N > a)
Definition simd_inl.h:451
bool AllTrue(T const &a)
Definition basic_utils.h:60
Simd< T, N > Set(Simd< T, N > a, T value)
Definition simd_inl.h:327
T HMax(Simd< T, N > a)
Definition simd_inl.h:461
V Broadcast(typename V::Scalar a)
Definition simd_inl.h:117
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:302
bool AnyTrue(T const &a)
Definition basic_utils.h:66
T Get(Simd< T, N > v)
Definition simd_inl.h:317
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Definition simd_inl.h:322
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
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
V Load(typename V::Scalar const *ptr)
Definition simd_inl.h:186
Simd< T, N > ShiftRight(Simd< T, N > a)
Definition simd_inl.h:274
#define MOCHI_SIMD_COMPOSITE_FN_3(FnName)
#define MOCHI_SIMD_COMPOSITE_OP_1(OP)
#define MOCHI_SIMD_COMPOSITE_OP_EQ(OP_EQ, OP)
#define MOCHI_SIMD_COMPOSITE_FN_2(FnName)
#define MOCHI_SIMD_COMPOSITE_OP_2(OP)
#define MOCHI_SIMD_COMPOSITE_OP_EQ_WITH_SCALAR(OP_EQ, OP)
#define MOCHI_SIMD_COMPOSITE_FN_1(FnName)