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 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b, Simd c, Simd d) {
615 static_assert(kSize == 4 && kSizeFirst == kSizeSecond, "Unsupported size");
616 // Like HSum, adds the halves of each vector first.
617 return {
618 First::HSumEach(a.first + a.second, b.first + b.second),
619 First::HSumEach(c.first + c.second, d.first + d.second)};
620 }
621
622 template <int SZ = kSize>
623 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
624 if constexpr (SZ <= kSizeFirst) {
625 return First::template HProd<SZ>(a.first);
626 } else if constexpr (SZ - kSizeFirst == 1) {
627 // HProd<1> is not normally supported, so multiply a scalar
628 return First::template HProd<kSizeFirst>(a.first) * Second::template Get<0>(a.second);
629 } else if constexpr (kSizeFirst == kSizeSecond && SZ == kSize) {
630 return First::template HProd<kSizeFirst>(a.first * a.second);
631 } else {
632 // May not be as fast due to repeated horizontal operations
633 return First::template HProd<kSizeFirst>(a.first) *
634 Second::template HProd<SZ - kSizeFirst>(a.second);
635 }
636 }
637
638 template <int SZ = kSize>
639 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
640 return Broadcast(HSum<SZ>(a * b)); // Use HSum to hopefully avoid repeated horizontal operations
641 }
642
643 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd Zero() {
644 return {First::Zero(), Second::Zero()};
645 }
646
648
649 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
651 first = rhs;
652 second = rhs;
653 return *this;
654 }
655
656 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
657 if constexpr (kSizeFirst == kSizeSecond) {
658 // Special case for 2x native size. Just one AllTrue.
659 return First::template AllTrue<kSizeFirst>(
660 First::Equal(this->first, rhs.first) & Second::Equal(this->second, rhs.second));
661 } else {
662 return (this->first == rhs.first) && (this->second == rhs.second);
663 }
664 }
665
666 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
667 return !(*this == rhs);
668 }
669
670 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Scalar operator[](int i) const {
671 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
672 return i < kSizeFirst ? first[i] : second[i - kSizeFirst];
673 }
674
675 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator<<(int i) const {
676 return {this->first << i, this->second << i};
677 }
678
680 this->first <<= i;
681 this->second <<= i;
682 return *this;
683 }
684
685 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator>>(int i) const {
686 return {this->first >> i, this->second >> i};
687 }
688
690 this->first >>= i;
691 this->second >>= i;
692 return *this;
693 }
694
695#define MOCHI_SIMD_COMPOSITE_FN_1(FnName) \
696 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a) { \
697 return {First::FnName(a.first), Second::FnName(a.second)}; \
698 }
699
700#define MOCHI_SIMD_COMPOSITE_FN_2(FnName) \
701 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a, Simd b) { \
702 return {First::FnName(a.first, b.first), Second::FnName(a.second, b.second)}; \
703 }
704
705#define MOCHI_SIMD_COMPOSITE_FN_3(FnName) \
706 [[nodiscard]] static MOCHI_ANY MOCHI_FORCE_INLINE Simd FnName(Simd a, Simd b, Simd c) { \
707 return { \
708 First::FnName(a.first, b.first, c.first), Second::FnName(a.second, b.second, c.second)}; \
709 }
710
711#define MOCHI_SIMD_COMPOSITE_OP_1(OP) \
712 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator OP() const { \
713 return {OP this->first, OP this->second}; \
714 }
715
716#define MOCHI_SIMD_COMPOSITE_OP_2(OP) \
717 [[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE Simd operator OP(Simd rhs) const { \
718 return {this->first OP rhs.first, this->second OP rhs.second}; \
719 }
720
721#define MOCHI_SIMD_COMPOSITE_OP_EQ(OP_EQ, OP) \
722 MOCHI_ANY MOCHI_FORCE_INLINE Simd& operator OP_EQ(Simd a) { \
723 this->first = this->first OP a.first; \
724 this->second = this->second OP a.second; \
725 return *this; \
726 }
727
728#define MOCHI_SIMD_COMPOSITE_OP_EQ_WITH_SCALAR(OP_EQ, OP) \
729 MOCHI_SIMD_COMPOSITE_OP_EQ(OP_EQ, OP); \
730 MOCHI_ANY MOCHI_FORCE_INLINE Simd& operator OP_EQ(Scalar a) { \
731 this->first = this->first OP First{a}; \
732 this->second = this->second OP Second{a}; \
733 return *this; \
734 }
735
736 // Unary functions:
752
753 // Binary functions
758
759 // Ternary functions
764
765 // Unary operators
768
769 // Binary operators
781
782 // Math assignment operators
783 //
784 // VS2019 WORKAROUND: These operators should not be necessary because there are generic templates
785 // in simd_inl.h which convert operations like `a += b;` into `a = a + b;`. However, there is
786 // a bug in the VS2019 compiler resulting in incorrect runtime behavior in some of these cases.
787 // The operators here take precedence and shortcut a couple layers of template abstraction,
788 // which appears to be a sufficient work-around. The bug was only observed with VS2019
789 // optimized builds, not with VS2022, Clang, nor GCC.
797
798#undef MOCHI_SIMD_COMPOSITE_FN_1
799#undef MOCHI_SIMD_COMPOSITE_FN_2
800#undef MOCHI_SIMD_COMPOSITE_FN_3
801#undef MOCHI_SIMD_COMPOSITE_OP_1
802#undef MOCHI_SIMD_COMPOSITE_OP_2
803#undef MOCHI_SIMD_COMPOSITE_OP_EQ
804#undef MOCHI_SIMD_COMPOSITE_OP_EQ_WITH_SCALAR
805};
806
807// ReinterpretCast for composite Simd types
808template <
809 class To,
810 class FromT,
811 int FromN,
812 MOCHI_CONCEPT((Simd<FromT, FromN>::kIsComposite) && To::kIsComposite)>
813[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To ReinterpretCast(Simd<FromT, FromN> const& a) {
814 using From = Simd<FromT, FromN>;
815 static_assert(sizeof(typename To::Scalar) * To::kSize == sizeof(FromT) * FromN, "Size mismatch");
816 static_assert(
817 sizeof(typename To::Scalar) * To::kSizeFirst == sizeof(FromT) * From::kSizeFirst,
818 "Component size mismatch");
819 return {
820 ReinterpretCast<typename To::First>(a.first), ReinterpretCast<typename To::Second>(a.second)};
821}
822
823namespace details {
824template <typename T>
825[[nodiscard]] constexpr bool IsCompositeWithEqualHalves() {
826 if constexpr (T::kIsComposite) {
827 return T::kSizeFirst == T::kSizeSecond;
828 } else {
829 return false;
830 }
831}
832
833template <class To, class From, std::size_t... Is>
834[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To
835StaticCastElementwise(From const& a, std::index_sequence<Is...>) {
836 return To{static_cast<typename To::Scalar>(a[Is])...};
837}
838} // namespace details
839
840// StaticCast when at least one of the types is a Simd composite.
841// Also handles the pass-through case where To and From are the same.
842template <class To, class From, MOCHI_CONCEPT_DEF(IsSimd<To>&& IsSimd<From>)>
843[[nodiscard]] MOCHI_ANY MOCHI_FORCE_INLINE To StaticCast(From const& a) {
844 static_assert(To::kSize == From::kSize, "Size mismatch");
845 static_assert(To::kIsSupported, "Unsupported type");
846 using ToHalf = Simd<typename To::Scalar, To::kSize / 2>;
847 using FromHalf = Simd<typename From::Scalar, From::kSize / 2>;
848 if constexpr (std::is_same_v<typename To::Scalar, typename From::Scalar>) {
849 return a; // No change
850 } else if constexpr (
851 To::kIsComposite && From::kIsComposite &&
852 (sizeof(typename To::Scalar) == sizeof(typename From::Scalar))) {
853 // Casting a composite to another composite, where the native sizes match
854 // Examples: Vec16f <--> Vec16i
856 } else if constexpr (
857 From::kIsComposite && !To::kIsComposite && ToHalf::kIsSupported &&
858 (sizeof(typename To::Scalar) * 2 == sizeof(typename From::Scalar))) {
859 // Casting from a composite to a native vector, where the destination scalar type is smaller
860 // Examples: Vec8i <-- Vec8d, Vec8f <-- Vec8d (using AVX)
861 return {StaticCast<ToHalf>(a.first), StaticCast<ToHalf>(a.second)};
862 } else if constexpr (
863 To::kIsComposite && !From::kIsComposite && FromHalf::kIsSupported &&
864 (sizeof(typename To::Scalar) == 2 * sizeof(typename From::Scalar))) {
865 // Casting from a native size to a composite, where the destination scalar type is larger
866 // Examples: Vec8d <-- Vec8i, Vec8d <-- Vec8f (using AVX)
867 return {
869 StaticCast<typename To::Second>(From::template GetHalf<1>(a))}; // Not optimized
870 } else if constexpr (
871 ::superdex::details::IsCompositeWithEqualHalves<From>() &&
872 ::superdex::details::IsCompositeWithEqualHalves<To>() &&
873 sizeof(typename To::Scalar) != sizeof(typename From::Scalar)) {
874 // Both sides are composite with equal-sized halves (i.e. GetHalf<0/1> can split them
875 // symmetrically), but their tree shapes may differ due to different native SIMD widths
876 // (e.g. Half↔float or double↔float on ARM), so we can't recurse via .first/.second directly.
877 return To{
878 StaticCast<ToHalf>(From::template GetHalf<0>(a)),
879 StaticCast<ToHalf>(From::template GetHalf<1>(a))};
880 } else {
881 return superdex::details::StaticCastElementwise<To>(a, std::make_index_sequence<From::kSize>{});
882 }
883}
884
885} // 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:286
constexpr T const & Min(T const &a, T const &b)
T HSum(Simd< T, N > a)
Definition simd_inl.h:390
constexpr To StaticCast(From const &a)
Definition basic_utils.h:79
T HMin(Simd< T, N > a)
Definition simd_inl.h:430
bool AllTrue(T const &a)
Definition basic_utils.h:60
Simd< T, N > Set(Simd< T, N > a, T value)
Definition simd_inl.h:326
T HMax(Simd< T, N > a)
Definition simd_inl.h:440
V Broadcast(typename V::Scalar a)
Definition simd_inl.h:116
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:301
bool AnyTrue(T const &a)
Definition basic_utils.h:66
T Get(Simd< T, N > v)
Definition simd_inl.h:316
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Definition simd_inl.h:321
T HProd(Simd< T, N > a)
Definition simd_inl.h:420
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:208
void StoreTransposed(T *ptr, Simd< T, N > a, Simd< T, N > b, Simd< T, N > c)
Definition simd_inl.h:252
void Store(T *ptr, Simd< T, N > a)
Definition simd_inl.h:220
V Load(typename V::Scalar const *ptr)
Definition simd_inl.h:185
Simd< T, N > ShiftRight(Simd< T, N > a)
Definition simd_inl.h:273
#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)