2025-09-29 18:21:16 +02:00
|
|
|
#ifndef THREE_PASS_XB_H
|
|
|
|
|
#define THREE_PASS_XB_H
|
|
|
|
|
/* Basically like threepass.h but but fewer bits - only for thiersort3! */
|
|
|
|
|
|
|
|
|
|
/* How the 28-30 bits gets separated? This is chosen because I saw that: */
|
|
|
|
|
/* - in float trickery I saw at around 4 billion the buckets change around 200 million */
|
|
|
|
|
/* - Which is smaller than 1/2 or 1/4 or 1/8 or 1/16 of it because 16*200 mil = 3.2 bill 28 bit is needed */
|
|
|
|
|
/* - This means those bits in numbers DO get used when we are running as internal sort of thier3 */
|
|
|
|
|
/* - But the numbers high bits (29..32th bits) stay the same inside each bucket so we spare it! */
|
|
|
|
|
/* - If you know your data does not have all the range of 32 bits you can #define these by #define CUSTOM_TPBX_BITS */
|
|
|
|
|
#ifndef CUSTOM_TPBX_BITS
|
|
|
|
|
#define TPBX1 10 // top
|
|
|
|
|
#define TPBX2 9 // mid
|
|
|
|
|
#define TPBX3 9 // bottom
|
|
|
|
|
#endif /* CUSTOM_TPBX_BITS */
|
|
|
|
|
|
2025-10-01 01:28:49 +02:00
|
|
|
#ifdef TPXB_USE_NON_TEMPORAL_WRITES
|
|
|
|
|
#include <emmintrin.h> /* Required for the intrinsic */
|
|
|
|
|
#endif /* TPXB_USE_NON_TEMPORAL_WRITES */
|
|
|
|
|
|
2025-09-30 22:17:30 +02:00
|
|
|
static inline constexpr uint32_t min3u32_xb(uint32_t a, uint32_t b, uint32_t c) noexcept {
|
2025-09-29 18:21:16 +02:00
|
|
|
return (a <= b) ?
|
|
|
|
|
((a <= c) ? a : c) :
|
|
|
|
|
((b <= c) ? b : c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple three-pass (ok: 3 + 1) bottom-up internal radix sort writter for thiersort3
|
|
|
|
|
*
|
|
|
|
|
* @param a The array to sort - will be changed too!
|
|
|
|
|
* @param buf Result array with the same size - result will be here
|
|
|
|
|
* @param n The number of elements
|
|
|
|
|
*/
|
2025-10-01 02:06:14 +02:00
|
|
|
static inline void threepass_xb(uint32_t *a, uint32_t *buf, uint32_t n) noexcept {
|
2025-09-29 18:21:16 +02:00
|
|
|
assert(buf != NULL);
|
2025-10-01 02:06:14 +02:00
|
|
|
constexpr uint32_t shr1 = TPBX3 + TPBX2;
|
|
|
|
|
constexpr uint32_t shr2 = TPBX3;
|
|
|
|
|
constexpr uint32_t shr3 = 0;
|
|
|
|
|
constexpr uint32_t mask1 = (1 << TPBX1) - 1;
|
|
|
|
|
constexpr uint32_t mask2 = (1 << TPBX2) - 1;
|
|
|
|
|
constexpr uint32_t mask3 = (1 << TPBX3) - 1;
|
2025-09-29 18:21:16 +02:00
|
|
|
|
2025-09-30 22:17:30 +02:00
|
|
|
static thread_local uint32_t bucket1[1 << TPBX1];
|
2025-09-29 18:21:16 +02:00
|
|
|
memset(bucket1, 0, (1 << TPBX1) * sizeof(uint32_t));
|
2025-09-30 22:17:30 +02:00
|
|
|
static thread_local uint32_t bucket2[1 << TPBX2];
|
2025-09-29 18:21:16 +02:00
|
|
|
memset(bucket2, 0, (1 << TPBX2) * sizeof(uint32_t));
|
2025-09-30 22:17:30 +02:00
|
|
|
static thread_local uint32_t bucket3[1 << TPBX3];
|
2025-09-29 18:21:16 +02:00
|
|
|
memset(bucket3, 0, (1 << TPBX3) * sizeof(uint32_t));
|
|
|
|
|
|
2025-09-30 22:18:10 +02:00
|
|
|
/* Count occurences (can count together with good ILP) */
|
|
|
|
|
#pragma GCC unroll 64
|
|
|
|
|
for(uint32_t i = 0; i < n; ++i) {
|
|
|
|
|
++bucket1[(a[i] >> shr1) & mask1];
|
|
|
|
|
++bucket2[(a[i] >> shr2) & mask2];
|
|
|
|
|
++bucket3[(a[i] >> shr3) & mask3];
|
|
|
|
|
}
|
2025-09-29 18:21:16 +02:00
|
|
|
|
|
|
|
|
/* Count prefix sums - try as much ILP as possible because bigger arrays than usual! */
|
|
|
|
|
uint32_t prev1 = 0;
|
|
|
|
|
uint32_t prev2 = 0;
|
|
|
|
|
uint32_t prev3 = 0;
|
|
|
|
|
uint32_t common = min3u32_xb(
|
|
|
|
|
(1 << TPBX1),
|
|
|
|
|
(1 << TPBX2),
|
|
|
|
|
(1 << TPBX3)
|
|
|
|
|
);
|
2025-10-01 02:06:14 +02:00
|
|
|
uint32_t i = 0;
|
2025-09-29 18:21:16 +02:00
|
|
|
#pragma GCC unroll 8
|
|
|
|
|
for (; i < common; ++i) {
|
|
|
|
|
bucket1[i] += prev1;
|
|
|
|
|
prev1 = bucket1[i];
|
|
|
|
|
bucket2[i] += prev2;
|
|
|
|
|
prev2 = bucket2[i];
|
|
|
|
|
bucket3[i] += prev3;
|
|
|
|
|
prev3 = bucket3[i];
|
|
|
|
|
}
|
|
|
|
|
/* Do remaining 1 */
|
2025-10-01 02:06:14 +02:00
|
|
|
for (uint32_t j = i; j < (1 << TPBX1); ++j) {
|
2025-09-29 18:21:16 +02:00
|
|
|
bucket1[j] += prev1;
|
|
|
|
|
prev1 = bucket1[j];
|
|
|
|
|
}
|
|
|
|
|
/* Do remaining 2 */
|
2025-10-01 02:06:14 +02:00
|
|
|
for (uint32_t j = i; j< (1 << TPBX2); ++j) {
|
2025-09-29 18:21:16 +02:00
|
|
|
bucket2[j] += prev2;
|
|
|
|
|
prev2 = bucket2[j];
|
|
|
|
|
}
|
|
|
|
|
/* Do remaining 3 */
|
2025-10-01 02:06:14 +02:00
|
|
|
for (uint32_t j = i; j < (1 << TPBX3); ++j) {
|
2025-09-29 18:21:16 +02:00
|
|
|
bucket3[j] += prev3;
|
|
|
|
|
prev3 = bucket3[j];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bottom digit a->buf
|
2025-09-30 22:18:10 +02:00
|
|
|
// right-to-left to ensure already sorted digits order we keep for iterations
|
|
|
|
|
#pragma GCC unroll 48
|
2025-10-01 01:53:38 +02:00
|
|
|
for(uint32_t i = n; i > 0; --i) {
|
2025-09-30 22:18:10 +02:00
|
|
|
// Prefetch caches
|
|
|
|
|
//__builtin_prefetch(&a[i-8]);
|
|
|
|
|
// Get num and its new offset / location
|
2025-10-01 01:53:38 +02:00
|
|
|
auto num = a[i - 1];
|
2025-09-30 22:18:10 +02:00
|
|
|
auto bkeyni = (num >> shr3) & mask3;
|
|
|
|
|
auto offset = --bucket3[bkeyni];
|
2025-09-29 18:52:02 +02:00
|
|
|
|
2025-09-30 22:18:10 +02:00
|
|
|
// Add to the proper target location
|
2025-10-01 01:28:49 +02:00
|
|
|
#ifndef TPXB_USE_NON_TEMPORAL_WRITES
|
2025-09-30 22:18:10 +02:00
|
|
|
buf[offset] = num;
|
2025-10-01 01:28:49 +02:00
|
|
|
#else
|
|
|
|
|
_mm_stream_si32((int*)(&buf[offset]), num);
|
|
|
|
|
#endif /* TPXB_USE_NON_TEMPORAL_WRITES */
|
2025-09-30 22:18:10 +02:00
|
|
|
}
|
2025-09-29 18:21:16 +02:00
|
|
|
// Mid digit buf->a
|
2025-09-30 22:18:10 +02:00
|
|
|
// right-to-left to ensure already sorted digits order we keep for iterations
|
|
|
|
|
#pragma GCC unroll 48
|
2025-10-01 01:53:38 +02:00
|
|
|
for(uint32_t i = n; i > 0; --i) {
|
2025-09-30 22:18:10 +02:00
|
|
|
// Prefetch caches
|
|
|
|
|
//__builtin_prefetch(&buf[i-8]);
|
|
|
|
|
// Get num and its new offset / location
|
2025-10-01 01:53:38 +02:00
|
|
|
auto num = buf[i - 1];
|
2025-09-30 22:18:10 +02:00
|
|
|
auto bkeyni = (num >> shr2) & mask2;
|
|
|
|
|
auto offset = --bucket2[bkeyni];
|
2025-09-29 18:52:02 +02:00
|
|
|
|
2025-09-30 22:18:10 +02:00
|
|
|
// Add to the proper target location
|
2025-10-01 01:28:49 +02:00
|
|
|
#ifndef TPXB_USE_NON_TEMPORAL_WRITES
|
2025-09-30 22:18:10 +02:00
|
|
|
a[offset] = num;
|
2025-10-01 01:28:49 +02:00
|
|
|
#else
|
|
|
|
|
_mm_stream_si32((int*)(&a[offset]), num);
|
|
|
|
|
#endif /* TPXB_USE_NON_TEMPORAL_WRITES */
|
2025-09-30 22:18:10 +02:00
|
|
|
}
|
2025-09-29 18:21:16 +02:00
|
|
|
// Top digit a->buf
|
2025-09-30 22:18:10 +02:00
|
|
|
// right-to-left to ensure already sorted digits order we keep for iterations
|
|
|
|
|
#pragma GCC unroll 48
|
2025-10-01 01:53:38 +02:00
|
|
|
for(uint32_t i = n; i > 0; --i) {
|
2025-09-30 22:18:10 +02:00
|
|
|
// Prefetch caches
|
|
|
|
|
// __builtin_prefetch(&a[i-16]);
|
|
|
|
|
// Get num and its new offset / location
|
2025-10-01 01:53:38 +02:00
|
|
|
auto num = a[i - 1];
|
2025-09-30 22:18:10 +02:00
|
|
|
auto bkeyni = (num >> shr1) & mask1;
|
|
|
|
|
auto offset = --bucket1[bkeyni];
|
|
|
|
|
|
|
|
|
|
// Add to the proper target location
|
2025-10-01 01:28:49 +02:00
|
|
|
#ifndef TPXB_USE_NON_TEMPORAL_WRITES
|
2025-09-30 22:18:10 +02:00
|
|
|
buf[offset] = num;
|
2025-10-01 01:28:49 +02:00
|
|
|
#else
|
|
|
|
|
_mm_stream_si32((int*)(&buf[offset]), num);
|
|
|
|
|
#endif /* TPXB_USE_NON_TEMPORAL_WRITES */
|
2025-09-30 22:18:10 +02:00
|
|
|
}
|
2025-09-29 18:21:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* THREE_PASS_XB_H */
|