The newsgroup news:comp.lang.c was {so far} unable to come up with a solution. I want to be able to do a branchless calculation of a last increment for shellsort that does not use if checks.
I am experimenting with the increment for shellsort using a modified version of Pete Pfilander's sorting driver and this bit of code (shell_ratio is a floating point number for the time being during my search):
void ssort2b(e_type array[], size_t count)
{
size_t i,
inc,
j;
e_type tmp;
int inversion = 1.0 / shell_ratio;
for (inc = count; inc > 0;) {
for (i = inc; i < count; i++) {
j = i;
tmp = array[i];
while (j >= inc && (GT(array + j - inc, &tmp))) {
array[j] = array[j - inc];
j -= inc;
}
array[j] = tmp;
}
if (inc <= inversion && inc > 1) {
inc = 1;
} else
inc *= shell_ratio;
}
}
At any rate, the bit at the end with:
if (inc <= inversion && inc > 1) {
inc = 1;
} else
inc *= shell_ratio;
really gives me the heebie-jeebies (imagining missed branch predictions). Eventually, I am going to use a rational increment and I also would like to change the calculation for the last increment so it is totally branchless.
So what I am wondering is:
"Is there a way to modify an integer so that there is no change if it is larger to a certain value and so that it gets truncated to 1 if it is less than or equal to a certain value?"
It also needs to get clobbered on the final pass so that it becomes zero, so the bitwise operations would also have to take that into account.