From 0b864ad2f0eb302b8df8885086fbd6cb58af262f Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Tue, 6 May 2025 02:27:38 +0200 Subject: [PATCH] bugfixed branchless code having extra nonSameIndex - but was working when only half of it is branchless --- qsort.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/qsort.h b/qsort.h index 7236112..c69c9fc 100644 --- a/qsort.h +++ b/qsort.h @@ -262,16 +262,17 @@ static inline pret3 partition3_sp2(uint32_t array[], int low, int high, uint32_t int nonSameIndex = (il + 1 != jl); if(isNonPivot & nonSameIndex) swapit(&array[il + 1], &array[jl]); - il += isNonPivot & nonSameIndex; + il += isNonPivot; ++jl; } /* Handle right and find wrongly placed element */ while((array[jr] >= pivotval) && (jl <= jr)) { - if(array[jr] != pivotval) { - if(--ir != jr) - swapit(&array[ir], &array[jr]); - } + int isNonPivot = (array[jr] != pivotval); + int nonSameIndex = (ir - 1 != jr); + if(isNonPivot & nonSameIndex) + swapit(&array[ir - 1], &array[jr]); + ir -= isNonPivot; --jr; }