From cebc516125e67dd0928a97a577d18564b0db064c Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Tue, 8 Apr 2025 01:33:11 +0200 Subject: [PATCH] optimization that does not work, but is maybe not totally bad --- qsort.h | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/qsort.h b/qsort.h index dc32535..46edbd5 100644 --- a/qsort.h +++ b/qsort.h @@ -102,15 +102,14 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv /* index until smaller or eq elements lay */ int i = (low - 1); - uint32_t pc = 0; + /* index until smaller or eq elements lay */ + int i2 = (high + 1); /* traverse each element of the array */ /* compare them with the pivot */ + int j2 = high; // j2 > i; --j2) { #pragma GCC unroll 4 for (int j = low; j <= high; ++j) { - /* Branchless pivot-count */ - pc += (array[j] == pivot); - if(array[j] < pivot) { /* if element smaller than pivot is found */ /* swap it with the greater element pointed by i */ @@ -119,28 +118,16 @@ static inline pret3 partition3(uint32_t array[], int low, int high, uint32_t piv /* swap element at i with element at j */ swapit(&array[i], &array[j]); } - } + if(j2 > i) { + if (array[j2] > pivot) { + /* if element smaller than pivot is found */ + /* swap it with the greater element pointed by i */ + --i2; - /* Can spare out the second loop in these cases */ - if(pc < 2) { - pret3 ret; - ret.leftend = i; - ret.rightend = i + 1; - return ret; - } - - /* index until smaller or eq elements lay */ - int i2 = (high + 1); - - #pragma GCC unroll 4 - for (int j = high; j > i; --j) { - if (array[j] > pivot) { - /* if element smaller than pivot is found */ - /* swap it with the greater element pointed by i */ - --i2; - - /* swap element at i with element at j */ - swapit(&array[i2], &array[j]); + /* swap element at i with element at j */ + swapit(&array[i2], &array[j2]); + } + --j2; } }