Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/xss-common-comparators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type_t next_value(type_t value)
}
}

template <typename vtype, typename mm_t>
template <typename vtype, bool keep_lane_on_tie = true, typename mm_t>
X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b);

template <typename vtype, bool descend>
Expand All @@ -57,11 +57,12 @@ struct Comparator {
}
}

template <bool keep_lane_on_tie = true>
X86_SIMD_SORT_FINLINE void COEX(reg_t &a, reg_t &b)
{
if constexpr (descend) { ::COEX<vtype, reg_t>(b, a); }
if constexpr (descend) { ::COEX<vtype, keep_lane_on_tie>(b, a); }
else {
::COEX<vtype, reg_t>(a, b);
::COEX<vtype, keep_lane_on_tie>(a, b);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/xss-common-qsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,18 @@ X86_SIMD_SORT_INLINE bool comparison_func(const T &a, const T &b)
/*
* COEX == Compare and Exchange two registers by swapping min and max values
*/
template <typename vtype, typename mm_t>
template <typename vtype, bool keep_lane_on_tie, typename mm_t>
X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b)
{
mm_t temp = a;
a = vtype::min(a, b);
// Floating-point min and max select their second operand on a tie.
if constexpr (keep_lane_on_tie
&& xss::fp::is_floating_point_v<typename vtype::type_t>) {
a = vtype::min(b, a);
}
else {
a = vtype::min(a, b);
}
b = vtype::max(temp, b);
}

Expand Down
8 changes: 3 additions & 5 deletions src/xss-network-qsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#include "xss-optimal-networks.hpp"

template <typename vtype, typename mm_t>
X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b);

template <typename vtype,
typename comparator,
int numVecs,
Expand Down Expand Up @@ -75,7 +72,8 @@ X86_SIMD_SORT_FINLINE void internal_merge_n_vec(typename vtype::reg_t *reg)
for (int i = 0; i < numVecs; i++) {
reg_t &v = reg[i];
reg_t rev = swizzle::template reverse_n<vtype, scale>(v);
comparator::COEX(rev, v);
// Mirrored pairs are compared in both lane orders before merging.
comparator::template COEX<false>(rev, v);
v = swizzle::template merge_n<vtype, scale>(v, rev);
}
}
Expand All @@ -85,7 +83,7 @@ X86_SIMD_SORT_FINLINE void internal_merge_n_vec(typename vtype::reg_t *reg)
for (int i = 0; i < numVecs; i++) {
reg_t &v = reg[i];
reg_t swap = swizzle::template swap_n<vtype, scale>(v);
comparator::COEX(swap, v);
comparator::template COEX<false>(swap, v);
v = swizzle::template merge_n<vtype, scale>(v, swap);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/xss-pivot-selection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ struct pivot_results {
}
};

template <typename vtype, typename mm_t>
X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b);

template <typename vtype, typename type_t>
X86_SIMD_SORT_INLINE type_t get_pivot(type_t *arr,
const arrsize_t left,
Expand Down
26 changes: 26 additions & 0 deletions tests/test-qsort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "test-qsort-common.h"

#include <cmath>

template <typename T>
class simdsort : public ::testing::Test {
public:
Expand Down Expand Up @@ -37,6 +39,29 @@ class simdsort : public ::testing::Test {

TYPED_TEST_SUITE_P(simdsort);

TYPED_TEST_P(simdsort, preserves_signed_zeros)
{
if constexpr (xss::fp::is_floating_point_v<TypeParam>) {
// 33 half-precision values span two AVX-512 registers.
for (size_t size : {5, 17, 33}) {
for (bool descending : {false, true}) {
std::vector<TypeParam> values(size);
values.back() = static_cast<TypeParam>(-0.0);
x86simdsort::qsort(values.data(), size, false, descending);

// Equal zeros may move, but each sign must keep its multiplicity.
const auto negative_zeros = std::count_if(
values.begin(), values.end(), [](TypeParam x) {
return x == 0
&& std::signbit(static_cast<double>(x));
});
EXPECT_EQ(negative_zeros, 1)
<< "size=" << size << ", descending=" << descending;
}
}
}
}

TYPED_TEST_P(simdsort, test_qsort_ascending)
{
for (auto type : this->arrtype) {
Expand Down Expand Up @@ -687,6 +712,7 @@ TYPED_TEST_P(simdsort, test_qsort_leading_nans)
}

REGISTER_TYPED_TEST_SUITE_P(simdsort,
preserves_signed_zeros,
test_qsort_ascending,
test_qsort_descending,
test_qsort_nans_last,
Expand Down
Loading