Skip to content

Commit 7aa2787

Browse files
committed
Name the pair layouts used by VQSort compare-exchanges
1 parent 9681c9a commit 7aa2787

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎hwy/contrib/sort/shared-inl.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ static_assert(SortConstants::MaxBufBytes<2>(64) <= 1664, "Unexpectedly high");
165165
namespace hwy {
166166
namespace HWY_NAMESPACE {
167167

168+
namespace detail {
169+
enum class PairLayout { kIndependent, kMirrored };
170+
} // namespace detail
171+
168172
// Default tag / vector width selector.
169173
#if HWY_TARGET == HWY_RVV
170174
// Use LMUL = 1/2; for SEW=64 this ends up emulated via VSETVLI.

‎hwy/contrib/sort/sorting_networks-inl.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct SharedTraits : public Base {
5151
HWY_INLINE Vec<D> SortPairsDistance2(D d, Vec<D> v) const {
5252
const Base* base = static_cast<const Base*>(this);
5353
Vec<D> swapped = base->SwapAdjacentPairs(d, v);
54-
base->template Sort2<true>(d, v, swapped);
54+
base->template Sort2<PairLayout::kMirrored>(d, v, swapped);
5555
return base->OddEvenPairs(d, swapped, v);
5656
}
5757

@@ -60,7 +60,7 @@ struct SharedTraits : public Base {
6060
HWY_INLINE Vec<D> SortPairsReverse8(D d, Vec<D> v) const {
6161
const Base* base = static_cast<const Base*>(this);
6262
Vec<D> swapped = base->ReverseKeys8(d, v);
63-
base->template Sort2<true>(d, v, swapped);
63+
base->template Sort2<PairLayout::kMirrored>(d, v, swapped);
6464
return base->OddEvenQuads(d, swapped, v);
6565
}
6666

@@ -70,7 +70,7 @@ struct SharedTraits : public Base {
7070
const Base* base = static_cast<const Base*>(this);
7171
static_assert(Constants::kMaxCols <= 16, "Need actual Reverse16");
7272
Vec<D> swapped = base->ReverseKeys(d, v);
73-
base->template Sort2<true>(d, v, swapped);
73+
base->template Sort2<PairLayout::kMirrored>(d, v, swapped);
7474
return ConcatUpperLower(d, swapped, v); // 8 = half of the vector
7575
}
7676
};

‎hwy/contrib/sort/traits-inl.h‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ struct TraitsLane : public Base {
550550
// according to Base.
551551
// Corresponds to a conditional swap, which is one "node" of a sorting
552552
// network. Min/Max are cheaper than compare + blend at least for integers.
553-
template <bool kMirror = false, class D>
553+
template <PairLayout kLayout = PairLayout::kIndependent, class D>
554554
HWY_INLINE void Sort2(D d, Vec<D>& a, Vec<D>& b) const {
555555
const Base* base = static_cast<const Base*>(this);
556556

@@ -559,7 +559,8 @@ struct TraitsLane : public Base {
559559
// and two blends, except for mirrored floats: selecting opposite inputs
560560
// on a tie there would duplicate one zero after the lanes are merged.
561561
#if HWY_AVX3 < HWY_TARGET && HWY_TARGET <= HWY_SSSE3
562-
if (sizeof(TFromD<D>) == 8 && !(kMirror && IsFloat<TFromD<D>>())) {
562+
if (sizeof(TFromD<D>) == 8 &&
563+
!(kLayout == PairLayout::kMirrored && IsFloat<TFromD<D>>())) {
563564
const Mask<D> cmp = base->Compare(d, a, b);
564565
a = IfThenElse(cmp, a, b);
565566
b = IfThenElse(cmp, b, a_copy);
@@ -570,7 +571,7 @@ struct TraitsLane : public Base {
570571
// Some floating-point Min/Max instructions both choose the second operand
571572
// for equal zeros. Keep one of each input. Mirrored lanes already compare
572573
// the same pair in opposite orders before selecting one result per lane.
573-
if constexpr (!kMirror && IsFloat<TFromD<D>>()) {
574+
if constexpr (kLayout == PairLayout::kIndependent && IsFloat<TFromD<D>>()) {
574575
b = base->Last(d, b, a_copy);
575576
} else {
576577
b = base->Last(d, a_copy, b);
@@ -588,7 +589,7 @@ struct TraitsLane : public Base {
588589
const Vec<D> cmp = VecFromMask(d, base->Compare(d, v, swapped));
589590
return IfVecThenElse(DupOdd(cmp), swapped, v);
590591
#else
591-
Sort2<true>(d, v, swapped);
592+
Sort2<PairLayout::kMirrored>(d, v, swapped);
592593
return base->OddEvenKeys(swapped, v);
593594
#endif
594595
}
@@ -598,7 +599,7 @@ struct TraitsLane : public Base {
598599
HWY_INLINE Vec<D> SortPairsDistance1(D d, Vec<D> v) const {
599600
const Base* base = static_cast<const Base*>(this);
600601
Vec<D> swapped = base->ReverseKeys2(d, v);
601-
Sort2<true>(d, v, swapped);
602+
Sort2<PairLayout::kMirrored>(d, v, swapped);
602603
return base->OddEvenKeys(swapped, v);
603604
}
604605

@@ -607,7 +608,7 @@ struct TraitsLane : public Base {
607608
HWY_INLINE Vec<D> SortPairsReverse4(D d, Vec<D> v) const {
608609
const Base* base = static_cast<const Base*>(this);
609610
Vec<D> swapped = base->ReverseKeys4(d, v);
610-
Sort2<true>(d, v, swapped);
611+
Sort2<PairLayout::kMirrored>(d, v, swapped);
611612
return base->OddEvenPairs(d, swapped, v);
612613
}
613614

@@ -618,7 +619,7 @@ struct TraitsLane : public Base {
618619
Vec<D> swapped = base->SwapAdjacentQuads(d, v);
619620
// Only used in Merge16, so this will not be used on AVX2 (which only has 4
620621
// u64 lanes), so skip the above optimization for 64-bit AVX2.
621-
Sort2<true>(d, v, swapped);
622+
Sort2<PairLayout::kMirrored>(d, v, swapped);
622623
return base->OddEvenQuads(d, swapped, v);
623624
}
624625
};

‎hwy/contrib/sort/traits128-inl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ struct Traits128 : public Base {
494494
return v;
495495
}
496496

497-
template <bool /*kMirror*/ = false, class D, HWY_IF_U64_D(D)>
497+
template <PairLayout = PairLayout::kIndependent, class D, HWY_IF_U64_D(D)>
498498
HWY_INLINE void Sort2(D d, Vec<D>& a, Vec<D>& b) const {
499499
const Base* base = static_cast<const Base*>(this);
500500

0 commit comments

Comments
 (0)