improved indirections

This commit is contained in:
Richard Thier 2021-12-18 02:34:22 +01:00
parent 3b413fcba0
commit f24b3987c0

View File

@ -244,11 +244,9 @@ namespace MagyarSort {
struct RadixMagic : public RadixMagic<DIGIT - 1, COUNTER_TYP> {
inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept
: RadixMagic<DIGIT - 1, COUNTER_TYP>(radics, from, to, size) {
//
// Compile time - only happens on bottom level
if(DIGIT == 0) {
this->ourFrom = from;
this->ourTo = to;
// Tricky: see (**)
if(this->swapped) { // never true for DIGIT 0
std::swap(from, to);
}
// DEBUG
@ -263,27 +261,26 @@ namespace MagyarSort {
if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll?
*/
// Get num and its new offset / location
auto num = this->ourFrom[i - 1];
auto num = from[i - 1];
auto digVal = getDigit<DIGIT>(num);
auto offset = (--rGet<DIGIT>(radics, digVal));
// Add to the proper target location
this->ourTo[offset] = num;
to[offset] = num;
}
// DEBUG
//printf("%d after: ", DIGIT);
//debugArr(to, size);
// Only swaps pointers :-)
std::swap(this->ourFrom, this->ourTo);
// (**) Only swaps pointers above in the child class constructor IF NEEDED :-)
this->swapped = !this->swapped;
}
};
/** Ends template recursion */
template<typename COUNTER_TYP>
struct RadixMagic<-1, COUNTER_TYP> {
uint32_t *ourFrom = nullptr;
uint32_t *ourTo = nullptr;
bool swapped = false;
inline RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept { }
};
@ -370,8 +367,8 @@ namespace MagyarSort {
// With an other API we could spare this copy if we can delete original arr and return ptr or something...
// I am fine with this... this is not my main idea anyways, just little ILP tweak to regular radix sort
//if(to != arr) // <- logically, but bad they are already swapped here!!! BEWARE
if(r.ourFrom != arr) { // <- in reality this is what we want because of last swap happened anyways!
memcpy(arr, r.ourFrom, size);
if(r.swapped) { // <- in reality this is what we want because of last swap happened anyways!
memcpy(arr, to, size);
}
}