[PATCH 02/13] loongarch: Add an inline roundeven_finite
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Nov 17 20:46:40 GMT 2026
Every range reduction that uses roundeven_finite requires a call to
__roundeven. The FTINTRNE.L.D and FFINT.D.L pair (FTINTRNE.L.S and
FFINT.S.L for roundevenf_finite) gives the round-to-nearest-even value
in two instructions, independently of the rounding mode, for
-2^63 <= x < 2^63.
Checked on loongarch64-linux-gnuf64.
---
sysdeps/loongarch/fpu/math-roundeven-finite.h | 46 +++++++++++++++++++
1 file changed, 46 insertions(+)
create mode 100644 sysdeps/loongarch/fpu/math-roundeven-finite.h
diff --git a/sysdeps/loongarch/fpu/math-roundeven-finite.h b/sysdeps/loongarch/fpu/math-roundeven-finite.h
new file mode 100644
index 00000000000..e775c4ab02f
--- /dev/null
+++ b/sysdeps/loongarch/fpu/math-roundeven-finite.h
@@ -0,0 +1,46 @@
+/* Round to nearest integer, ties to even, for range reductions. LoongArch.
+ 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
+
+/* FTINTRNE.L.D converts X to a 64-bit integer, rounding to nearest with
+ ties to even regardless of the rounding mode, and FFINT.D.L converts it
+ back. The result is exact for -2^63 <= X < 2^63, and FTINTRNE saturates
+ and raises invalid outside that range. */
+static inline double
+roundeven_finite (double x)
+{
+ double r;
+ __asm__ ("ftintrne.l.d %0, %1\n\t"
+ "ffint.d.l %0, %0"
+ : "=f" (r) : "f" (x));
+ return r;
+}
+
+static inline float
+roundevenf_finite (float x)
+{
+ float r;
+ __asm__ ("ftintrne.l.s %0, %1\n\t"
+ "ffint.s.l %0, %0"
+ : "=f" (r) : "f" (x));
+ return r;
+}
+
+#endif
--
2.53.0
More information about the Libc-alpha
mailing list