various bugs

This commit is contained in:
Richard Thier 2023-06-30 22:06:24 +02:00
parent 58176a89b6
commit 79b95bf905
2 changed files with 30 additions and 13 deletions

View File

@ -490,7 +490,7 @@ static inline TSU8 ts_radixi(
k.f = (float)(key.i); k.f = (float)(key.i);
/* 8 bit float */ /* 8 bit float */
k.u >> 24; k.u = k.u >> 24;
/* Need float sign trickery */ /* Need float sign trickery */
return ts_floatsigntrick((TSU8)k.u); return ts_floatsigntrick((TSU8)k.u);
@ -502,7 +502,7 @@ static inline TSU8 ts_radixi(
/* 8 bit float */ /* 8 bit float */
/* top bit always zero so ignore it and use 8 useful bits */ /* top bit always zero so ignore it and use 8 useful bits */
/* - this is why we shift 23 and not 24 here */ /* - this is why we shift 23 and not 24 here */
k.u >> 23; k.u = k.u >> 23;
/* We are fine! */ /* We are fine! */
return (TSU8)k.u; return (TSU8)k.u;
@ -512,7 +512,7 @@ static inline TSU8 ts_radixi(
k.f = key.f; k.f = key.f;
/* 8 bit float */ /* 8 bit float */
k.u >> 24; k.u = k.u >> 24;
/* Need float sign trickery */ /* Need float sign trickery */
return ts_floatsigntrick((TSU8)k.u); return ts_floatsigntrick((TSU8)k.u);
@ -534,7 +534,7 @@ static inline void ts_quicksort_inplace(
void *reent_data), void *reent_data),
void *reent_data) { void *reent_data) {
// Must do this early exit! // Must do this early exit!
if(from == to) return; if(from >= to) return;
TSU32 len = (to - from); TSU32 len = (to - from);
TSU32 mid = from + len / 2; TSU32 mid = from + len / 2;
@ -576,13 +576,13 @@ static inline void ts_quicksort_inplace(
if(left == right) { if(left == right) {
const union tskey leftkey = arr[left].key; const union tskey leftkey = arr[left].key;
TSU32 lefti = arr[left].i; TSU32 lefti = arr[left].i;
left += (lt(pivotkey, leftkey, pivoti, lefti, reent_data)); left += (lt(leftkey, pivotkey, lefti, pivoti, reent_data));
} }
/* Just simple recursion for now */ /* Just simple recursion for now */
ts_quicksort_inplace(arr, from, left, lt, reent_data); ts_quicksort_inplace(arr, from, left, lt, reent_data);
ts_quicksort_inplace(arr, left + 1, to, lt, reent_data); ts_quicksort_inplace(arr, left, to, lt, reent_data);
} }
/** /**
* Simple quicksort implementation that also moves data. * Simple quicksort implementation that also moves data.
@ -602,7 +602,7 @@ static inline void ts_quicksort_fromto(
void *reent_data), void *reent_data),
void *reent_data) { void *reent_data) {
// Must do this early exit! // Must do this early exit!
if(from == to) return; if(from >= to) return;
TSU32 len = (to - from); TSU32 len = (to - from);
TSU32 mid = from + len / 2; TSU32 mid = from + len / 2;
@ -656,6 +656,8 @@ static inline void thiersort8_internal(
/* Occurence counting O(n) */ /* Occurence counting O(n) */
for(TSU32 i = 0; i < length; ++i) { for(TSU32 i = 0; i < length; ++i) {
++radics[ts_radixi(arr[i].key, isint, isunsigned, isfloat)]; ++radics[ts_radixi(arr[i].key, isint, isunsigned, isfloat)];
// FIXME: remove this debug code!
// arr[i].i = ts_radixi(arr[i].key, isint, isunsigned, isfloat);
} }
/* Prefix sum + real radics calc O(256) */ /* Prefix sum + real radics calc O(256) */

View File

@ -33,8 +33,9 @@ void measure(const std::string &inputtype, const std::string &name,
worst[name] = std::max(worst[name], seconds); worst[name] = std::max(worst[name], seconds);
} }
std::vector<std::string> inputtypes = { std::vector<std::string> inputtypes = {
"constant", "asc", "desc", "ascasc", "ascdesc", /* "constant", "asc", "desc", "ascasc", "ascdesc",
"descasc", "descdesc", "smallrange", "rand", "descasc", "descdesc", "smallrange",*/
"rand",
}; };
std::vector<uint32_t> geninput(const std::string &type, int n) { std::vector<uint32_t> geninput(const std::string &type, int n) {
std::vector<uint32_t> v(n); std::vector<uint32_t> v(n);
@ -409,6 +410,10 @@ void thier_quicksort(uint32_t *arr, int n) {
n, // length, n, // length,
malloc); malloc);
for(uint32_t i = 0; i < n; ++i) {
printf("In: %d @%d\n", tarr[i].key.u, tarr[i].i);
}
// Quicksort by me // Quicksort by me
ts_quicksort_inplace( ts_quicksort_inplace(
tarr, tarr,
@ -417,6 +422,10 @@ void thier_quicksort(uint32_t *arr, int n) {
ts_lt_uint, ts_lt_uint,
nullptr); nullptr);
for(uint32_t i = 0; i < n; ++i) {
printf("Out: %d @%d\n", tarr[i].key.u, tarr[i].i);
}
// Apply: O(n) // Apply: O(n)
uint32_t tmp[1]; // needed for elem swaps uint32_t tmp[1]; // needed for elem swaps
thiersort_apply( thiersort_apply(
@ -499,7 +508,7 @@ int main(void) {
//int n = 10000000; //int n = 10000000;
//int n = 10000; //int n = 10000;
//int n = 100; //int n = 100;
int n = 65; int n = 10;
printf("Sorting %d elements:\n\n", n); printf("Sorting %d elements:\n\n", n);
@ -525,9 +534,11 @@ int main(void) {
w.swap(buf); w.swap(buf);
} }
}); });
/*
w = v; w = v;
measure(inputtype, "magyar", [&] { MagyarSort::sort<uint32_t>(&w[0], w.size()); }); measure(inputtype, "magyar", [&] { MagyarSort::sort<uint32_t>(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
*/
/* /*
w = v; w = v;
@ -547,18 +558,22 @@ int main(void) {
assert(w == expected); assert(w == expected);
w = v; w = v;
measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); }); measure(inputtype, "sp", [&] { spsort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);*/
w = v;*/ w = v;
measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); }); measure(inputtype, "gptbuck", [&] { gpt_bucket_sort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
/* /*
w = v;
measure(inputtype, "magbuck", [&] { magyar_bucket_sort(&w[0], w.size()); }); measure(inputtype, "magbuck", [&] { magyar_bucket_sort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
w = v;
measure(inputtype, "magbuck2", [&] { magyar_bucket_sort2(&w[0], w.size()); }); measure(inputtype, "magbuck2", [&] { magyar_bucket_sort2(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
*/ */
w = {10, 20, 5, 200, 42, 41, 43, 5};
measure(inputtype, "qsmine", [&] { thier_quicksort(&w[0], w.size()); }); measure(inputtype, "qsmine", [&] { thier_quicksort(&w[0], w.size()); });
assert(w == expected); assert(w == expected);
w = v;
measure(inputtype, "thier", [&] { thiersort_uintkey8(&w[0], w.size()); }); measure(inputtype, "thier", [&] { thiersort_uintkey8(&w[0], w.size()); });
if(w != expected) { if(w != expected) {
for(uint32_t i = 0; i < n; ++i) { for(uint32_t i = 0; i < n; ++i) {