[PATCH 8/8] math: Use binary search on tgammaf slow path

Paul Zimmermann Paul.Zimmermann@inria.fr
Tue Oct 14 07:51:04 GMT 2025


> 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

personally I prefer the following form, with invariant
a <= wanted_index < b:

int a = 0, b = array_length (tb);
while (a + 1 < b)
  {
     m = (a + b) / 2;
     tbi = asuint (tb[m].x);
     if (t < tbi)
       b = m;
     else
       a = m;
  }
  if (t == asuint (tb[a].x))
    // found

That's a matter of taste.

Paul



More information about the Libc-alpha mailing list