diff --git a/magyarsort.h b/magyarsort.h index 0d39686..1ff572a 100644 --- a/magyarsort.h +++ b/magyarsort.h @@ -242,8 +242,15 @@ namespace MagyarSort { /** Recursive Functor: no class should be generated I think (compiler should be smart) */ template struct RadixMagic : public RadixMagic { - inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept // BEWARE: "*&" needed to swap pointers.. + 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; + } + // DEBUG //printf("%d before: ", DIGIT); //debugArr(from, size); @@ -256,12 +263,12 @@ namespace MagyarSort { if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll? */ // Get num and its new offset / location - auto num = from[i - 1]; + auto num = this->ourFrom[i - 1]; auto digVal = getDigit(num); auto offset = (--rGet(radics, digVal)); // Add to the proper target location - to[offset] = num; + this->ourTo[offset] = num; } // DEBUG @@ -269,12 +276,14 @@ namespace MagyarSort { //debugArr(to, size); // Only swaps pointers :-) - std::swap(from, to); + std::swap(this->ourFrom, this->ourTo); } }; /** Ends template recursion */ template struct RadixMagic<-1, COUNTER_TYP> { + uint32_t *ourFrom = nullptr; + uint32_t *ourTo = nullptr; inline RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept { } }; @@ -356,13 +365,13 @@ namespace MagyarSort { uint32_t *from = arr; uint32_t *to = &arc[0]; - RadixMagic(radics, from, to, size); + RadixMagic r(radics, from, to, size); // 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(from != arr) { // <- in reality this is what we want because of last swap happened anyways! - memcpy(arr, from, size); + if(r.ourFrom != arr) { // <- in reality this is what we want because of last swap happened anyways! + memcpy(arr, r.ourFrom, size); } }