[PATCH] math: Add internal roundeven_finite

Florian Weimer fweimer@redhat.com
Tue Nov 19 13:32:20 GMT 2024


* Adhemerval Zanella:

> +static inline double
> +roundeven_finite (double_t x)
> +{
> +  double_t y = round (x);
> +  if (fabs (x - y) == 0.5)
> +    {
> +      union { double f; uint64_t i; } u = {y};
> +      union { double f; uint64_t i; } v = {(x > 0) ? y - 1.0 : y + 1.0};
> +      if (__builtin_ctzll (v.i) > __builtin_ctzll (u.i))
> +        y = v.f;
> +    }
> +  return y;
> +}

Inconsistent use of double and double_t?

Would this work instead?

  union { double f; unsigned long i; } v = {y - __builtin_copysign (1.0, x)};

And maybe add

   if (!isfinite (x))
    __builtin_unreachable ();

at the start?

Thanks,
Florian



More information about the Libc-alpha mailing list