[PATCH 01/13] math: Make roundeven_finite an architecture hook
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Nov 17 20:46:39 GMT 2026
Move the alternative to a new sysdeps header (math-roundeven-finite),
and remove the ROUNDEVEN_INTRINSICS and ROUNDEVENF_INTRINSICS macros.
The generic header also documents what the callers guarantee, since
they all use it in range reductions: the argument must be in [-2^63, 2^63),
the sign of a zero result does not matter, and the inexact exception may
be raised for a non-integer argument. An architecture can then
implement it with a conversion to a 64-bit integer and back.
Checked with a build on x86_64-linux-gnu, aarch64-linux-gnu, and
powerpc64le-linux-gnu, and powerpc-linux-gnu.
---
sysdeps/generic/math-roundeven-finite.h | 45 ++++++++++++++++
sysdeps/ieee754/dbl-64/math_config.h | 27 +---------
sysdeps/ieee754/flt-32/math_config.h | 52 +-----------------
sysdeps/powerpc/fpu/math-roundeven-finite.h | 60 +++++++++++++++++++++
sysdeps/powerpc/fpu/math_private.h | 6 ---
5 files changed, 107 insertions(+), 83 deletions(-)
create mode 100644 sysdeps/generic/math-roundeven-finite.h
create mode 100644 sysdeps/powerpc/fpu/math-roundeven-finite.h
diff --git a/sysdeps/generic/math-roundeven-finite.h b/sysdeps/generic/math-roundeven-finite.h
new file mode 100644
index 00000000000..62276d59a2c
--- /dev/null
+++ b/sysdeps/generic/math-roundeven-finite.h
@@ -0,0 +1,45 @@
+/* Round to nearest integer, ties to even, for range reductions. Generic.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _MATH_ROUNDEVEN_FINITE_H
+#define _MATH_ROUNDEVEN_FINITE_H 1
+
+/* Return X rounded to the nearest integer value in floating-point format,
+ with halfway cases rounded to even, independently of the rounding mode.
+ They are meant for range reductionsi, and X must be in [-2^63, 2^63), the
+ sign of a zero result is unspecified, and the inexact exception may be
+ raised when X is not an integer.
+ An architecture can then implement them with a conversion to a 64-bit
+ integer and back. */
+static inline double
+roundeven_finite (double x)
+{
+ if (!isfinite (x))
+ __builtin_unreachable ();
+ return roundeven (x);
+}
+
+static inline float
+roundevenf_finite (float x)
+{
+ if (!isfinite (x))
+ __builtin_unreachable ();
+ return roundevenf (x);
+}
+
+#endif
diff --git a/sysdeps/ieee754/dbl-64/math_config.h b/sysdeps/ieee754/dbl-64/math_config.h
index d8eafd169af..ac2cf8000d2 100644
--- a/sysdeps/ieee754/dbl-64/math_config.h
+++ b/sysdeps/ieee754/dbl-64/math_config.h
@@ -38,32 +38,7 @@
# define WANT_ERRNO_UFLOW (WANT_ROUNDING && WANT_ERRNO)
#endif
-#ifndef ROUNDEVEN_INTRINSICS
-/* When set, roundeven_finite will route to the internal roundeven function. */
-# define ROUNDEVEN_INTRINSICS 1
-#endif
-
-/* Round x to nearest integer value in floating-point format, rounding halfway
- cases to even. If the input is non finite the result is unspecified. */
-static inline double
-roundeven_finite (double x)
-{
- if (!isfinite (x))
- __builtin_unreachable ();
-#if ROUNDEVEN_INTRINSICS
- return roundeven (x);
-#else
- double y = round (x);
- if (fabs (x - y) == 0.5)
- {
- union { double f; uint64_t i; } u = {y};
- union { double f; uint64_t i; } v = {y - copysign (1.0, x)};
- if (stdc_trailing_zeros (v.i) > stdc_trailing_zeros (u.i))
- y = v.f;
- }
- return y;
-#endif
-}
+#include <math-roundeven-finite.h>
#ifndef TOINT_INTRINSICS
/* When set, the roundtoint and converttoint functions are provided with
diff --git a/sysdeps/ieee754/flt-32/math_config.h b/sysdeps/ieee754/flt-32/math_config.h
index d76bd1bbae5..9349e90cf1e 100644
--- a/sysdeps/ieee754/flt-32/math_config.h
+++ b/sysdeps/ieee754/flt-32/math_config.h
@@ -58,57 +58,7 @@ static inline int32_t
converttoint (double x);
#endif
-#ifndef ROUNDEVEN_INTRINSICS
-/* When set, roundeven_finite will route to the internal roundeven function. */
-# define ROUNDEVEN_INTRINSICS 1
-#endif
-
-/* Round x to nearest integer value in floating-point format, rounding halfway
- cases to even. If the input is non finite the result is unspecified. */
-static inline double
-roundeven_finite (double x)
-{
- if (!isfinite (x))
- __builtin_unreachable ();
-#if ROUNDEVEN_INTRINSICS
- return roundeven (x);
-#else
- double y = round (x);
- if (fabs (x - y) == 0.5)
- {
- union { double f; uint64_t i; } u = {y};
- union { double f; uint64_t i; } v = {y - copysign (1.0, x)};
- if (stdc_trailing_zeros (v.i) > stdc_trailing_zeros (u.i))
- y = v.f;
- }
- return y;
-#endif
-}
-
-#ifndef ROUNDEVENF_INTRINSICS
-/* When set, roundevenf_finite will route to the internal roundevenf function. */
-# define ROUNDEVENF_INTRINSICS 1
-#endif
-
-static inline float
-roundevenf_finite (float x)
-{
- if (!isfinite (x))
- __builtin_unreachable ();
-#if ROUNDEVENF_INTRINSICS
- return roundevenf (x);
-#else
- float y = roundf (x);
- if (fabs (x - y) == 0.5)
- {
- union { float f; uint32_t i; } u = {y};
- union { float f; uint32_t i; } v = {y - copysignf (1.0f, x)};
- if (stdc_trailing_zeros (v.i) > stdc_trailing_zeros (u.i))
- y = v.f;
- }
- return y;
-#endif
-}
+#include <math-roundeven-finite.h>
static inline uint32_t
asuint (float f)
diff --git a/sysdeps/powerpc/fpu/math-roundeven-finite.h b/sysdeps/powerpc/fpu/math-roundeven-finite.h
new file mode 100644
index 00000000000..1de34c6e548
--- /dev/null
+++ b/sysdeps/powerpc/fpu/math-roundeven-finite.h
@@ -0,0 +1,60 @@
+/* Round to nearest integer, ties to even, for range reductions. PowerPC.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#ifndef _MATH_ROUNDEVEN_FINITE_POWERPC_H
+#define _MATH_ROUNDEVEN_FINITE_POWERPC_H 1
+
+#ifdef _ARCH_PWR6
+# include <math.h>
+# include <stdbit.h>
+# include <stdint.h>
+
+/* ISA 2.03 provides frin/round() and cntlzw/ctznll(). */
+
+static inline double
+roundeven_finite (double x)
+{
+ double y = round (x);
+ if (fabs (x - y) == 0.5)
+ {
+ union { double f; uint64_t i; } u = {y};
+ union { double f; uint64_t i; } v = {y - copysign (1.0, x)};
+ if (stdc_trailing_zeros (v.i) > stdc_trailing_zeros (u.i))
+ y = v.f;
+ }
+ return y;
+}
+
+static inline float
+roundevenf_finite (float x)
+{
+ float y = roundf (x);
+ if (fabsf (x - y) == 0.5f)
+ {
+ union { float f; uint32_t i; } u = {y};
+ union { float f; uint32_t i; } v = {y - copysignf (1.0f, x)};
+ if (stdc_trailing_zeros (v.i) > stdc_trailing_zeros (u.i))
+ y = v.f;
+ }
+ return y;
+}
+#else
+# include_next <math-roundeven-finite.h>
+#endif
+
+#endif /* math-roundeven-finite.h */
diff --git a/sysdeps/powerpc/fpu/math_private.h b/sysdeps/powerpc/fpu/math_private.h
index 7dd0675cb44..5e631ad73d2 100644
--- a/sysdeps/powerpc/fpu/math_private.h
+++ b/sysdeps/powerpc/fpu/math_private.h
@@ -58,10 +58,4 @@ __ieee754_sqrtf128 (_Float128 __x)
#define _GL_HAS_BUILTIN_ILOGB 0
#endif
-#ifdef _ARCH_PWR6
-/* ISA 2.03 provides frin/round() and cntlzw/ctznll(). */
-# define ROUNDEVEN_INTRINSICS 0
-# define ROUNDEVENF_INTRINSICS 0
-#endif
-
#endif /* _PPC_MATH_PRIVATE_H_ */
--
2.53.0
More information about the Libc-alpha
mailing list