thier3: realized that I can run with 256 bucketed float prepass (many us faster!) + experimented with a 1bit split trick (too much overhead to win, despite less cache misses)
This commit is contained in:
parent
45820cf81c
commit
1f6ef0f2ea
87
thiersort3.h
87
thiersort3.h
@ -1,9 +1,19 @@
|
|||||||
#ifndef THIER_SORT3_H
|
#ifndef THIER_SORT3_H
|
||||||
#define THIER_SORT3_H
|
#define THIER_SORT3_H
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "threepass_xbit.h"
|
|
||||||
/* A non-implace tricky float-hackz based bucket sort variant. Uses threepass_xbit and removes some compies! */
|
/* A non-implace tricky float-hackz based bucket sort variant. Uses threepass_xbit and removes some compies! */
|
||||||
|
|
||||||
|
/* Disables extra 1-bit split processing before threepass_xb */
|
||||||
|
#define NO_EXTRA_BIT /* I measure extra split to be slightly slower because overhead - despite less cache misses */
|
||||||
|
|
||||||
|
#ifndef NO_EXTRA_BIT
|
||||||
|
#define CUSTOM_TPBX_BITS
|
||||||
|
#define TPBX1 9 /* top - not 10 because our extra bit split / partitioning trickery! */
|
||||||
|
#define TPBX2 9 /* mid */
|
||||||
|
#define TPBX3 9 /* bottom */
|
||||||
|
#endif /* NO_EXTRA_BIT */
|
||||||
|
#include "threepass_xbit.h"
|
||||||
|
|
||||||
/* Float and unsigned32 reinterpreter */
|
/* Float and unsigned32 reinterpreter */
|
||||||
union th3_fu {
|
union th3_fu {
|
||||||
float f;
|
float f;
|
||||||
@ -26,11 +36,12 @@ static inline uint32_t witch_bucket3(uint32_t key) {
|
|||||||
as.f = (float) key;
|
as.f = (float) key;
|
||||||
|
|
||||||
uint32_t witch_base = (key <= 2) ? 0 : (as.u >> 23) - 128; // 0, [127..159] -> [0..31]
|
uint32_t witch_base = (key <= 2) ? 0 : (as.u >> 23) - 128; // 0, [127..159] -> [0..31]
|
||||||
// return witch_base * 8 + ((as.u >> (23 - 3)) & 7); // 0..255
|
return witch_base * 8 + ((as.u >> (23 - 3)) & 7); // 0..255
|
||||||
return witch_base * 128 + ((as.u >> (23 - 7)) & 127); // 0..2047
|
/* All below ones were worse, because three-ways 28 bit sorting would just make unnecessary work at cost of some locality */
|
||||||
/* Alternative (but I measure it being worse):
|
//return witch_base * 128 + ((as.u >> (23 - 7)) & 127); // 0..4095
|
||||||
return (as.u >> 23);
|
//return witch_base * 64 + ((as.u >> (23 - 6)) & 63); // 0..2047
|
||||||
*/
|
//return witch_base * 32 + ((as.u >> (23 - 5)) & 31); // 0..1023
|
||||||
|
//return witch_base; // not enough!
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,6 +56,11 @@ static inline void thiersort3(uint32_t *arr, uint32_t *temparr, int n) {
|
|||||||
int bucket[4096]; /* Inclusive */
|
int bucket[4096]; /* Inclusive */
|
||||||
int bucket_end[4096]; /* Not inclusive */
|
int bucket_end[4096]; /* Not inclusive */
|
||||||
|
|
||||||
|
#ifndef NO_EXTRA_BIT
|
||||||
|
int bucket_leftend[4096]; /* for extra 1bit split processing */
|
||||||
|
int bucket_left[4096]; /* for extra 1bit split processing */
|
||||||
|
#endif /* NO_EXTRA_BIT */
|
||||||
|
|
||||||
/* Check if need to sort at all - needed for invariants later */
|
/* Check if need to sort at all - needed for invariants later */
|
||||||
if(n < 2) {
|
if(n < 2) {
|
||||||
return;
|
return;
|
||||||
@ -74,7 +90,19 @@ static inline void thiersort3(uint32_t *arr, uint32_t *temparr, int n) {
|
|||||||
bucket_end[i] = bucket[i];
|
bucket_end[i] = bucket[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef NO_EXTRA_BIT
|
||||||
|
/* Save left-offsets */
|
||||||
|
bucket_left[0] = 0;
|
||||||
|
bucket_leftend[0] = 0;
|
||||||
|
#pragma GCC unroll 64
|
||||||
|
for(int i = 0; i < 4095; ++i) {
|
||||||
|
bucket_left[1 + i] = bucket[i];
|
||||||
|
bucket_leftend[1 + i] = bucket[i];
|
||||||
|
}
|
||||||
|
#endif /* NO_EXTRA_BIT */
|
||||||
|
|
||||||
/* arr -> temparr */
|
/* arr -> temparr */
|
||||||
|
#ifdef NO_EXTRA_BIT
|
||||||
/* Move to the buckets */
|
/* Move to the buckets */
|
||||||
/* Rem.: This also changes bucket[i] so they will point to bucket beginnings */
|
/* Rem.: This also changes bucket[i] so they will point to bucket beginnings */
|
||||||
#pragma GCC unroll 64
|
#pragma GCC unroll 64
|
||||||
@ -104,6 +132,53 @@ static inline void thiersort3(uint32_t *arr, uint32_t *temparr, int n) {
|
|||||||
uint32_t *buf = &(arr[begin]);
|
uint32_t *buf = &(arr[begin]);
|
||||||
threepass_xb(a, buf, n);
|
threepass_xb(a, buf, n);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
/* Move to the buckets by split-partitioning on the 28th bit */
|
||||||
|
/* Rem.: This also changes bucket[i] so they will point to bucket.right beginnings */
|
||||||
|
/* Rem.: This also changes bucket_leftend[i] so they will point to bucket.left endings (needed in-process only) */
|
||||||
|
#pragma GCC unroll 64
|
||||||
|
for(int i = 0; i < n; ++i) {
|
||||||
|
uint32_t num = arr[i];
|
||||||
|
uint32_t witch = witch_bucket3(num);
|
||||||
|
int offset = (num & (1 << 27)) ?
|
||||||
|
(--bucket[witch]) :
|
||||||
|
(bucket_leftend[witch]++);
|
||||||
|
|
||||||
|
temparr[offset] = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* temparr -> arr each bucket and sort them in-place */
|
||||||
|
#pragma GCC unroll 64
|
||||||
|
for(int b = 0; b < 4096; ++b) {
|
||||||
|
assert(bucket_leftend[b] == bucket[b]);
|
||||||
|
int lbegin = bucket_left[b];
|
||||||
|
int lend = bucket[b]; /* non-inclusive */
|
||||||
|
int rbegin = bucket[b];
|
||||||
|
int rend = bucket_end[b]; /* non-inclusive */
|
||||||
|
|
||||||
|
/* Ensure exists and process left part */
|
||||||
|
if(lbegin < lend) {
|
||||||
|
/* Use our specialized threepass */
|
||||||
|
/* - which does not use up all the bits */
|
||||||
|
/* - which does not allocate and copy back to arr here */
|
||||||
|
uint32_t n = lend - lbegin;
|
||||||
|
uint32_t *a = &(temparr[lbegin]);
|
||||||
|
uint32_t *buf = &(arr[lbegin]);
|
||||||
|
threepass_xb(a, buf, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Ensure exists and process right part */
|
||||||
|
if(rbegin < rend) {
|
||||||
|
/* Use our specialized threepass */
|
||||||
|
/* - which does not use up all the bits */
|
||||||
|
/* - which does not allocate and copy back to arr here */
|
||||||
|
uint32_t n = rend - rbegin;
|
||||||
|
uint32_t *a = &(temparr[rbegin]);
|
||||||
|
uint32_t *buf = &(arr[rbegin]);
|
||||||
|
threepass_xb(a, buf, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif /* NO_EXTRA_BIT */
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* THIER_SORT3_H */
|
#endif /* THIER_SORT3_H */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user