[PATCH 8/8] math: Use binary search on tgammaf slow path
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Mon Oct 13 20:01:42 GMT 2025
Hi Adhemerval,
> Hum, I am having some trouble to implement this approach.
This has multiple bugs in it...
>> + while (a <= b)
>> + { /* Binary search. */
>> + uint32_t tbi = asuint (tb[m].x);
>> + if (t == tbi)
>> + return tb[m].f + tb[m].df;
>> + else if (t > tbi)
>> + a = m + 1; // this can read beyond the array in next iteration
>> + else
>> + b = m - 1; // this skips m-1 due to b being 1 beyond the end
>> + m = (a + b) / 2;
Something like this should work (assumes array is not empty like above):
int a = 0, b = array_length (tb) - 1;
while (a < b)
{
m = (a + b) >> 1;
tbi = asuint (tb[m].x);
if (t > tbi)
a = m + 1;
else
b = m;
}
if (t == asuint (tb[a].x))
// found
Cheers,
Wilco
More information about the Libc-alpha
mailing list