magyarsort/thiersort3.h
Richard Thier 52fc14b0f6 Revert "thier3: write caching queues fixed - bug just makes it slower despite less cache misses"
This reverts commit 967c7c19b54fd0db820bbfa1cbe199a8ac9f5419.
2025-09-30 22:17:30 +02:00

110 lines
3.1 KiB
C

#ifndef THIER_SORT3_H
#define THIER_SORT3_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! */
/* Float and unsigned32 reinterpreter */
union th3_fu {
float f;
uint32_t u;
};
typedef union th3_fu th3_fu;
/** Tells from the key which bucket it is in. */
static inline uint32_t witch_bucket3(uint32_t key) {
/* This is hackz to misuse int->float converter HEAVILY and IEE for bucketing */
/* https://en.wikipedia.org/wiki/Single-precision_floating-point_format */
/* Old Hungarian ASM trick I know from Tomcat/Abaddon mailing list and prog.hu */
/* "A következő nagyon fontos gondolat, hogy két lebegőpontos számot, */
/* amennyiben pozitív, "simán" is összehasonlíthatunk" */
/* See: https://prog.hu/cikkek/100239/fpu-gems */
/* This approach uses 12 bits from a 32 bit float to map onto a byte bucket index */
th3_fu as;
as.f = (float) key;
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 * 128 + ((as.u >> (23 - 7)) & 127); // 0..2047
/* Alternative (but I measure it being worse):
return (as.u >> 23);
*/
}
/**
* Sort the array using the temporary array of the same size with fast bucket sort thiersort.
*
* @param arr The array to sort, will contain result afterwards
* @param temparr The temporary array with same size
* @param n Number of elements in arr and temparr
* @param rstate Create with sch_rand_state rstate = schwab_rand_state(junk_uint32_t);
*/
static inline void thiersort3(uint32_t *arr, uint32_t *temparr, uint32_t n) {
uint32_t bucket[4096]; /* Inclusive */
uint32_t bucket_end[4096]; /* Not inclusive */
/* Check if need to sort at all - needed for invariants later */
if(n < 2) {
return;
}
/* Count */
#pragma GCC unroll 64
for(uint32_t i = 0; i < 4096; ++i) {
bucket[i] = 0;
}
#pragma GCC unroll 64
for(uint32_t i = 0; i < n; ++i) {
++bucket[witch_bucket3(arr[i])];
}
/* Prefix sum (like in Magyarsort) */
uint32_t prev = 0;
#pragma GCC unroll 4
for (uint32_t i = 0; i < 4096; i++) {
bucket[i] += prev;
prev = bucket[i];
}
/* Save end-offsets */
#pragma GCC unroll 64
for(uint32_t i = 0; i < 4096; ++i) {
bucket_end[i] = bucket[i];
}
/* arr -> temparr */
/* Move to the buckets */
/* Rem.: This also changes bucket[i] so they will point to bucket beginnings */
#pragma GCC unroll 64
for(uint32_t i = 0; i < n; ++i) {
uint32_t num = arr[i];
uint32_t witch = witch_bucket3(num);
uint32_t offset = (--bucket[witch]);
temparr[offset] = num;
}
/* temparr -> arr each bucket and sort them in-place */
#pragma GCC unroll 64
for(uint32_t b = 0; b < 4096; ++b) {
uint32_t begin = bucket[b];
uint32_t end = bucket_end[b];
/* Ensure exists */
if(begin >= end) {
continue;
}
/* 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 = end - begin;
uint32_t *a = &(temparr[begin]);
uint32_t *buf = &(arr[begin]);
threepass_xb(a, buf, n);
}
}
#endif /* THIER_SORT3_H */