From f24b3987c0a4cf1d77455df50374e1b499f62f06 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Sat, 18 Dec 2021 02:34:22 +0100 Subject: [PATCH] improved indirections --- magyarsort.h | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/magyarsort.h b/magyarsort.h index 1ff572a..66d6aee 100644 --- a/magyarsort.h +++ b/magyarsort.h @@ -244,11 +244,9 @@ namespace MagyarSort { struct RadixMagic : public RadixMagic { inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept : RadixMagic(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(num); auto offset = (--rGet(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 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); } }