removed reference to pointer parameter - a bit better indirections
This commit is contained in:
parent
298edba5d2
commit
3b413fcba0
23
magyarsort.h
23
magyarsort.h
@ -242,8 +242,15 @@ namespace MagyarSort {
|
|||||||
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
||||||
template<int DIGIT, typename COUNTER_TYP>
|
template<int DIGIT, typename COUNTER_TYP>
|
||||||
struct RadixMagic : public RadixMagic<DIGIT - 1, COUNTER_TYP> {
|
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 // BEWARE: "*&" needed to swap pointers..
|
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) {
|
: 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;
|
||||||
|
}
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
//printf("%d before: ", DIGIT);
|
//printf("%d before: ", DIGIT);
|
||||||
//debugArr(from, size);
|
//debugArr(from, size);
|
||||||
@ -256,12 +263,12 @@ namespace MagyarSort {
|
|||||||
if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll?
|
if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll?
|
||||||
*/
|
*/
|
||||||
// Get num and its new offset / location
|
// Get num and its new offset / location
|
||||||
auto num = from[i - 1];
|
auto num = this->ourFrom[i - 1];
|
||||||
auto digVal = getDigit<DIGIT>(num);
|
auto digVal = getDigit<DIGIT>(num);
|
||||||
auto offset = (--rGet<DIGIT>(radics, digVal));
|
auto offset = (--rGet<DIGIT>(radics, digVal));
|
||||||
|
|
||||||
// Add to the proper target location
|
// Add to the proper target location
|
||||||
to[offset] = num;
|
this->ourTo[offset] = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG
|
// DEBUG
|
||||||
@ -269,12 +276,14 @@ namespace MagyarSort {
|
|||||||
//debugArr(to, size);
|
//debugArr(to, size);
|
||||||
|
|
||||||
// Only swaps pointers :-)
|
// Only swaps pointers :-)
|
||||||
std::swap(from, to);
|
std::swap(this->ourFrom, this->ourTo);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/** Ends template recursion */
|
/** Ends template recursion */
|
||||||
template<typename COUNTER_TYP>
|
template<typename COUNTER_TYP>
|
||||||
struct RadixMagic<-1, COUNTER_TYP> {
|
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 { }
|
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 *from = arr;
|
||||||
uint32_t *to = &arc[0];
|
uint32_t *to = &arc[0];
|
||||||
|
|
||||||
RadixMagic<DIGITS - 1, COUNTER_TYP>(radics, from, to, size);
|
RadixMagic<DIGITS - 1, COUNTER_TYP> 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...
|
// 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
|
// 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(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!
|
if(r.ourFrom != arr) { // <- in reality this is what we want because of last swap happened anyways!
|
||||||
memcpy(arr, from, size);
|
memcpy(arr, r.ourFrom, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user