diff --git a/src/xss-common-comparators.hpp b/src/xss-common-comparators.hpp index 447709a..0b1a7eb 100644 --- a/src/xss-common-comparators.hpp +++ b/src/xss-common-comparators.hpp @@ -31,7 +31,7 @@ type_t next_value(type_t value) } } -template +template X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b); template @@ -57,11 +57,12 @@ struct Comparator { } } + template X86_SIMD_SORT_FINLINE void COEX(reg_t &a, reg_t &b) { - if constexpr (descend) { ::COEX(b, a); } + if constexpr (descend) { ::COEX(b, a); } else { - ::COEX(a, b); + ::COEX(a, b); } } diff --git a/src/xss-common-qsort.h b/src/xss-common-qsort.h index 70aa346..69f34cf 100644 --- a/src/xss-common-qsort.h +++ b/src/xss-common-qsort.h @@ -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 +template 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) { + a = vtype::min(b, a); + } + else { + a = vtype::min(a, b); + } b = vtype::max(temp, b); } diff --git a/src/xss-network-qsort.hpp b/src/xss-network-qsort.hpp index 4bceda1..eaf0a01 100644 --- a/src/xss-network-qsort.hpp +++ b/src/xss-network-qsort.hpp @@ -3,9 +3,6 @@ #include "xss-optimal-networks.hpp" -template -X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b); - template (v); - comparator::COEX(rev, v); + // Mirrored pairs are compared in both lane orders before merging. + comparator::template COEX(rev, v); v = swizzle::template merge_n(v, rev); } } @@ -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(v); - comparator::COEX(swap, v); + comparator::template COEX(swap, v); v = swizzle::template merge_n(v, swap); } } diff --git a/src/xss-pivot-selection.hpp b/src/xss-pivot-selection.hpp index c09dfc6..6b86d58 100644 --- a/src/xss-pivot-selection.hpp +++ b/src/xss-pivot-selection.hpp @@ -20,9 +20,6 @@ struct pivot_results { } }; -template -X86_SIMD_SORT_INLINE void COEX(mm_t &a, mm_t &b); - template X86_SIMD_SORT_INLINE type_t get_pivot(type_t *arr, const arrsize_t left, diff --git a/tests/test-qsort.cpp b/tests/test-qsort.cpp index 89e1435..c021bc6 100644 --- a/tests/test-qsort.cpp +++ b/tests/test-qsort.cpp @@ -5,6 +5,8 @@ #include "test-qsort-common.h" +#include + template class simdsort : public ::testing::Test { public: @@ -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) { + // 33 half-precision values span two AVX-512 registers. + for (size_t size : {5, 17, 33}) { + for (bool descending : {false, true}) { + std::vector values(size); + values.back() = static_cast(-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(x)); + }); + EXPECT_EQ(negative_zeros, 1) + << "size=" << size << ", descending=" << descending; + } + } + } +} + TYPED_TEST_P(simdsort, test_qsort_ascending) { for (auto type : this->arrtype) { @@ -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,