Skip to content

Commit c750811

Browse files
committed
Update common.hlsl
1 parent 0f8c062 commit c750811

File tree

1 file changed

+23
-102
lines changed

1 file changed

+23
-102
lines changed

include/nbl/builtin/hlsl/bitonic_sort/common.hlsl

Lines changed: 23 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
55
#include <nbl/builtin/hlsl/concepts.hlsl>
66
#include <nbl/builtin/hlsl/math/intutil.hlsl>
7-
#include <nbl/builtin/hlsl/pair.hlsl>
7+
#include <nbl/builtin/hlsl/utility.hlsl>
88

99
namespace nbl
1010
{
@@ -15,115 +15,36 @@ namespace bitonic_sort
1515

1616
template<typename KeyType, typename ValueType, typename Comparator>
1717
void compareExchangeWithPartner(
18-
bool takeLarger,
19-
NBL_REF_ARG(KeyType) loKey,
20-
NBL_CONST_REF_ARG(KeyType) partnerLoKey,
21-
NBL_REF_ARG(KeyType) hiKey,
22-
NBL_CONST_REF_ARG(KeyType) partnerHiKey,
23-
NBL_REF_ARG(ValueType) loVal,
24-
NBL_CONST_REF_ARG(ValueType) partnerLoVal,
25-
NBL_REF_ARG(ValueType) hiVal,
26-
NBL_CONST_REF_ARG(ValueType) partnerHiVal,
27-
NBL_CONST_REF_ARG(Comparator) comp)
18+
bool takeLarger,
19+
NBL_REF_ARG(pair<KeyType, ValueType>) loPair,
20+
NBL_CONST_REF_ARG(pair<KeyType, ValueType>) partnerLoPair,
21+
NBL_REF_ARG(pair<KeyType, ValueType>) hiPair,
22+
NBL_CONST_REF_ARG(pair<KeyType, ValueType>) partnerHiPair,
23+
NBL_CONST_REF_ARG(Comparator) comp)
2824
{
29-
const bool loSelfSmaller = comp(loKey, partnerLoKey);
30-
const bool takePartnerLo = takeLarger ? loSelfSmaller : !loSelfSmaller;
31-
loKey = takePartnerLo ? partnerLoKey : loKey;
32-
loVal = takePartnerLo ? partnerLoVal : loVal;
25+
const bool loSelfSmaller = comp(loPair.first, partnerLoPair.first);
26+
const bool takePartnerLo = takeLarger ? loSelfSmaller : !loSelfSmaller;
27+
if (takePartnerLo)
28+
loPair = partnerLoPair;
3329

34-
const bool hiSelfSmaller = comp(hiKey, partnerHiKey);
35-
const bool takePartnerHi = takeLarger ? hiSelfSmaller : !hiSelfSmaller;
36-
hiKey = takePartnerHi ? partnerHiKey : hiKey;
37-
hiVal = takePartnerHi ? partnerHiVal : hiVal;
38-
}
39-
40-
41-
template<typename KeyType, typename ValueType, typename Comparator>
42-
void compareSwap(
43-
bool ascending,
44-
NBL_REF_ARG(KeyType) loKey,
45-
NBL_REF_ARG(KeyType) hiKey,
46-
NBL_REF_ARG(ValueType) loVal,
47-
NBL_REF_ARG(ValueType) hiVal,
48-
NBL_CONST_REF_ARG(Comparator) comp)
49-
{
50-
const bool shouldSwap = comp(hiKey, loKey);
51-
52-
const bool doSwap = (shouldSwap == ascending);
53-
54-
KeyType tempKey = loKey;
55-
loKey = doSwap ? hiKey : loKey;
56-
hiKey = doSwap ? tempKey : hiKey;
57-
58-
ValueType tempVal = loVal;
59-
loVal = doSwap ? hiVal : loVal;
60-
hiVal = doSwap ? tempVal : hiVal;
61-
}
62-
63-
template<typename KeyType, typename ValueType>
64-
void swap(
65-
NBL_REF_ARG(KeyType) loKey,
66-
NBL_REF_ARG(KeyType) hiKey,
67-
NBL_REF_ARG(ValueType) loVal,
68-
NBL_REF_ARG(ValueType) hiVal)
69-
{
70-
KeyType tempKey = loKey;
71-
loKey = hiKey;
72-
hiKey = tempKey;
73-
74-
ValueType tempVal = loVal;
75-
loVal = hiVal;
76-
hiVal = tempVal;
77-
}
78-
79-
80-
81-
template<typename KeyType, typename ValueType, typename Comparator>
82-
void compareExchangeWithPartner(
83-
bool takeLarger,
84-
NBL_REF_ARG(pair<KeyType, ValueType>) loPair,
85-
NBL_CONST_REF_ARG(pair<KeyType, ValueType>) partnerLoPair,
86-
NBL_REF_ARG(pair<KeyType, ValueType>) hiPair,
87-
NBL_CONST_REF_ARG(pair<KeyType, ValueType>) partnerHiPair,
88-
NBL_CONST_REF_ARG(Comparator) comp)
89-
{
90-
const bool loSelfSmaller = comp(loPair.first, partnerLoPair.first);
91-
const bool takePartnerLo = takeLarger ? loSelfSmaller : !loSelfSmaller;
92-
loPair.first = takePartnerLo ? partnerLoPair.first : loPair.first;
93-
loPair.second = takePartnerLo ? partnerLoPair.second : loPair.second;
94-
95-
const bool hiSelfSmaller = comp(hiPair.first, partnerHiPair.first);
96-
const bool takePartnerHi = takeLarger ? hiSelfSmaller : !hiSelfSmaller;
97-
hiPair.first = takePartnerHi ? partnerHiPair.first : hiPair.first;
98-
hiPair.second = takePartnerHi ? partnerHiPair.second : hiPair.second;
30+
const bool hiSelfSmaller = comp(hiPair.first, partnerHiPair.first);
31+
const bool takePartnerHi = takeLarger ? hiSelfSmaller : !hiSelfSmaller;
32+
if (takePartnerHi)
33+
hiPair = partnerHiPair;
9934
}
10035

10136
template<typename KeyType, typename ValueType, typename Comparator>
10237
void compareSwap(
103-
bool ascending,
104-
NBL_REF_ARG(pair<KeyType, ValueType>) loPair,
105-
NBL_REF_ARG(pair<KeyType, ValueType>) hiPair,
106-
NBL_CONST_REF_ARG(Comparator) comp)
38+
bool ascending,
39+
NBL_REF_ARG(pair<KeyType, ValueType>) loPair,
40+
NBL_REF_ARG(pair<KeyType, ValueType>) hiPair,
41+
NBL_CONST_REF_ARG(Comparator) comp)
10742
{
108-
const bool shouldSwap = comp(hiPair.first, loPair.first);
109-
const bool doSwap = (shouldSwap == ascending);
43+
const bool shouldSwap = comp(hiPair.first, loPair.first);
44+
const bool doSwap = (shouldSwap == ascending);
11045

111-
KeyType tempKey = loPair.first;
112-
ValueType tempVal = loPair.second;
113-
loPair.first = doSwap ? hiPair.first : loPair.first;
114-
loPair.second = doSwap ? hiPair.second : loPair.second;
115-
hiPair.first = doSwap ? tempKey : hiPair.first;
116-
hiPair.second = doSwap ? tempVal : hiPair.second;
117-
}
118-
119-
template<typename KeyType, typename ValueType>
120-
void swap(
121-
NBL_REF_ARG(pair<KeyType, ValueType>) loPair,
122-
NBL_REF_ARG(pair<KeyType, ValueType>) hiPair)
123-
{
124-
pair<KeyType, ValueType> temp = loPair;
125-
loPair = hiPair;
126-
hiPair = temp;
46+
if (doSwap)
47+
swap(loPair, hiPair);
12748
}
12849
}
12950
}

0 commit comments

Comments
 (0)