From 266f489002a62bbf21eb5b86b05504ba81304241 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Fri, 9 May 2025 05:35:42 +0200 Subject: [PATCH] schwab: minor fine-tune so we more easily win over std::sort --- schwab_sort.h | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/schwab_sort.h b/schwab_sort.h index e4dcbe9..9905e09 100644 --- a/schwab_sort.h +++ b/schwab_sort.h @@ -16,7 +16,7 @@ /** Below this many elements we do insertion sort */ #ifndef SCHWAB_INSERTION_THRESHOLD -#define SCHWAB_INSERTION_THRESHOLD 64 +#define SCHWAB_INSERTION_THRESHOLD 128 #endif /* SCHWAB_DELTA_THRESHOLD */ 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 */ inline void sch_insertion_sort(uint32_t *arr, int low, int high) { - for (int i = low + 1; i <= high; ++i) { - uint32_t key = arr[i]; - int j = i; + for(int i = low + 1; i <= high; ++i) { + uint32_t key = arr[i]; - /* Move elements of arr[0..i-1] that are greater than key */ - /* to one position ahead of their current position */ - while (j > 0 && arr[j - 1] > key) { - arr[j] = arr[j - 1]; - --j; - } - arr[j] = key; - } + /* Move elements of arr[0..i-1] that are greater than key */ + /* to one position ahead of their current position */ + int j = i; + #pragma GCC unroll 2 + while(j > 0 && arr[j - 1] > key) { + arr[j] = arr[j - 1]; + --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; + } } /**