Skip to content

Commit 880f5a2

Browse files
committed
Add tests and fix bug in move_nans_to_end_of_array
1 parent b3cd262 commit 880f5a2

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/avx512-common-qsort.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,18 @@ int64_t move_nans_to_end_of_array(T* arr, int64_t arrsize)
131131
{
132132
int64_t jj = arrsize - 1;
133133
int64_t ii = 0;
134-
while (ii < jj) {
134+
int64_t count = 0;
135+
while (ii <= jj) {
135136
if (is_a_nan(arr[ii])) {
136137
std::swap(arr[ii], arr[jj]);
137138
jj -= 1;
139+
count++;
138140
}
139141
else {
140142
ii += 1;
141143
}
142144
}
143-
return ii;
145+
return arrsize-count-1;
144146
}
145147

146148
template <typename vtype, typename T = typename vtype::type_t>

tests/test-qsort-fp.hpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*******************************************
2+
* * Copyright (C) 2022 Intel Corporation
3+
* * SPDX-License-Identifier: BSD-3-Clause
4+
* *******************************************/
5+
6+
#include "test-qsort-common.h"
7+
8+
template <typename T>
9+
class avx512_sort_fp : public ::testing::Test {
10+
};
11+
TYPED_TEST_SUITE_P(avx512_sort_fp);
12+
13+
TYPED_TEST_P(avx512_sort_fp, test_random_nan)
14+
{
15+
const int num_nans = 3;
16+
if (!cpu_has_avx512bw()) {
17+
GTEST_SKIP() << "Skipping this test, it requires avx512bw";
18+
}
19+
std::vector<int64_t> arrsizes;
20+
for (int64_t ii = num_nans; ii < 1024; ++ii) {
21+
arrsizes.push_back((TypeParam)ii);
22+
}
23+
std::vector<TypeParam> arr;
24+
std::vector<TypeParam> sortedarr;
25+
for (auto &size : arrsizes) {
26+
/* Random array */
27+
arr = get_uniform_rand_array<TypeParam>(size);
28+
for (auto ii = 1; ii <= num_nans; ++ii) {
29+
arr[size-ii] = std::numeric_limits<TypeParam>::quiet_NaN();
30+
}
31+
sortedarr = arr;
32+
std::sort(sortedarr.begin(), sortedarr.end()-3);
33+
std::random_shuffle(arr.begin(), arr.end());
34+
avx512_qsort<TypeParam>(arr.data(), arr.size());
35+
for (auto ii = 1; ii <= num_nans; ++ii) {
36+
if (!std::isnan(arr[size-ii])) {
37+
ASSERT_TRUE(false) << "NAN's aren't sorted to the end. Arr size = " << size;
38+
}
39+
}
40+
if (!std::is_sorted(arr.begin(), arr.end() - num_nans)) {
41+
ASSERT_TRUE(true) << "Array isn't sorted";
42+
}
43+
arr.clear();
44+
sortedarr.clear();
45+
}
46+
}
47+
REGISTER_TYPED_TEST_SUITE_P(avx512_sort_fp, test_random_nan);

tests/test-qsort.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "test-partial-qsort.hpp"
22
#include "test-qselect.hpp"
3+
#include "test-qsort-fp.hpp"
34
#include "test-qsort.hpp"
45

56
using QSortTestTypes = testing::Types<uint16_t,
@@ -10,6 +11,10 @@ using QSortTestTypes = testing::Types<uint16_t,
1011
int32_t,
1112
uint64_t,
1213
int64_t>;
14+
15+
using QSortTestFPTypes = testing::Types<float, double>;
16+
1317
INSTANTIATE_TYPED_TEST_SUITE_P(T, avx512_sort, QSortTestTypes);
18+
INSTANTIATE_TYPED_TEST_SUITE_P(T, avx512_sort_fp, QSortTestFPTypes);
1419
INSTANTIATE_TYPED_TEST_SUITE_P(T, avx512_select, QSortTestTypes);
1520
INSTANTIATE_TYPED_TEST_SUITE_P(T, avx512_partial_sort, QSortTestTypes);

0 commit comments

Comments
 (0)