schwab: minor fine-tune so we more easily win over std::sort

This commit is contained in:
Richard Thier 2025-05-09 05:35:42 +02:00
parent 95c759b9e3
commit 266f489002

View File

@ -16,7 +16,7 @@
/** Below this many elements we do insertion sort */ /** Below this many elements we do insertion sort */
#ifndef SCHWAB_INSERTION_THRESHOLD #ifndef SCHWAB_INSERTION_THRESHOLD
#define SCHWAB_INSERTION_THRESHOLD 64 #define SCHWAB_INSERTION_THRESHOLD 128
#endif /* SCHWAB_DELTA_THRESHOLD */ #endif /* SCHWAB_DELTA_THRESHOLD */
typedef uint32_t sch_rand_state; typedef uint32_t sch_rand_state;
@ -48,18 +48,37 @@ static inline void schwab_swap(uint32_t *a, uint32_t *b) {
/** Simple insertion sort for small cases */ /** Simple insertion sort for small cases */
inline void sch_insertion_sort(uint32_t *arr, int low, int high) { inline void sch_insertion_sort(uint32_t *arr, int low, int high) {
for (int i = low + 1; i <= high; ++i) { for(int i = low + 1; i <= high; ++i) {
uint32_t key = arr[i]; uint32_t key = arr[i];
int j = i;
/* Move elements of arr[0..i-1] that are greater than key */ /* Move elements of arr[0..i-1] that are greater than key */
/* to one position ahead of their current position */ /* to one position ahead of their current position */
while (j > 0 && arr[j - 1] > key) { int j = i;
arr[j] = arr[j - 1]; #pragma GCC unroll 2
--j; while(j > 0 && arr[j - 1] > key) {
} arr[j] = arr[j - 1];
arr[j] = key; --j;
} }
arr[j] = key;
}
}
/** Simple insertion sort for small cases v2 - not necessarily better */
inline void sch_insertion_sort2(uint32_t* arr, int low, int high) {
for(size_t i = low + 1; i <= high; ++i) {
uint32_t key = arr[i];
/* Separate load and compare to expose ILP */
int j = i;
#pragma GCC unroll 2
while(j > 0) {
uint32_t prev = arr[j - 1];
if (prev <= key) break;
arr[j] = prev;
--j;
}
arr[j] = key;
}
} }
/** /**