[PATCH 4/5] newlib: libm: sqrtl: round correctly for 80-bit long double

Bernie Innocenti bernie@codewiz.org
Sat Oct 17 09:40:37 GMT 2026


The Newton-step version "rounds to nearest for simplicity" and is one
ulp off for about half of all arguments on x86 (1000174 of 2000000
random ones, compared with glibc's sqrtl).  IEEE 754 requires a
correctly rounded square root.

For the formats with a 64-bit mantissa, take the integer square root of
the mantissa, 2 bits per step, and round from the remainder; there are
no ties.  Raise FE_INEXACT when the root is not exact.  The 113-bit
formats keep the existing code.

Checked on x86_64: identical to glibc's sqrtl for 2000000 random
arguments and for all powers of two down to the smallest subnormal.

Fixes: b9e7cd9a846c ("* libc/include/complex.h (cabsl): Add prototype.")
Assisted-by: Claude:claude-opus-5-5 claude-code
Signed-off-by: Bernie Innocenti <bernie@codewiz.org>
---
 newlib/libm/common/sqrtl.c | 96 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)

diff --git a/newlib/libm/common/sqrtl.c b/newlib/libm/common/sqrtl.c
index 9976f35e7..10dba3e28 100644
--- a/newlib/libm/common/sqrtl.c
+++ b/newlib/libm/common/sqrtl.c
@@ -79,6 +79,101 @@ sqrtl (long double x)
 #define LDBL_MAX_EXP	DBL_MAX_EXP
 #endif
 
+#if LDBL_MANT_DIG == 64
+
+/* The 80-bit formats: a correctly rounded integer square root of the
+   64-bit mantissa.  */
+
+long double
+sqrtl (long double x)
+{
+  union ieee_ext_u ux = { .extu_ld = x, };
+  unsigned long long m, r, rh, rl, th, tl, mh, ml;
+  int e, k = 0, i;
+
+  /* If x = NaN, then sqrt(x) = NaN.  */
+  /* If x = Inf, then sqrt(x) = Inf.  */
+  /* If x = -Inf, then sqrt(x) = NaN.  */
+  if (ux.extu_ext.ext_exp == LDBL_MAX_EXP * 2 - 1)
+    return (x * x + x);
+
+  /* If x = +-0, then sqrt(x) = +-0.  */
+  if (x == 0.0L || x == -0.0L)
+    return x;
+
+  /* If x < 0, then raise invalid and return NaN.  */
+  if (ux.extu_ext.ext_sign)
+    return ((x - x) / (x - x));
+
+  /* Scale up an exponent of 0: subnormals, and on m68k also the normal
+     numbers in [2^-16383, 2^-16382).  */
+  if (ux.extu_ext.ext_exp == 0)
+    {
+      ux.extu_ld *= 0x1.0p64L;
+      k = -64;
+    }
+
+  /* x = m * 2^e, with m in [2^63, 2^64).  */
+  m = ((unsigned long long) ux.extu_ext.ext_frach << 32)
+      | ux.extu_ext.ext_fracl;
+  e = (int) ux.extu_ext.ext_exp - EXT_EXP_BIAS - 63 + k;
+
+  /* sqrt(x) = sqrt(M) * 2^((e - s) / 2), where M = m * 2^s is in
+     [2^126, 2^128), so that its root has 64 bits, and e - s is even.  */
+  if (e & 1)
+    {
+      mh = m >> 1;
+      ml = m << 63;
+      e -= 63;
+    }
+  else
+    {
+      mh = m;
+      ml = 0;
+      e -= 64;
+    }
+
+  /* Two bits of M at a time: r is the root so far, rh:rl is M - r^2.  */
+  r = rh = rl = 0;
+  for (i = 0; i < 64; i++)
+    {
+      rh = (rh << 2) | (rl >> 62);
+      rl = (rl << 2) | (mh >> 62);
+      mh = (mh << 2) | (ml >> 62);
+      ml <<= 2;
+      th = r >> 62;		/* th:tl = 4r + 1 */
+      tl = (r << 2) | 1;
+      if (rh > th || (rh == th && rl >= tl))
+	{
+	  rh -= th + (rl < tl);
+	  rl -= tl;
+	  r = (r << 1) | 1;
+	}
+      else
+	r <<= 1;
+    }
+
+  /* Not a square: raise FE_INEXACT, as a floating-point square root
+     would.  LDBL_MAX + 1 rounds back to LDBL_MAX.  */
+  if (rh != 0 || rl != 0)
+    {
+      volatile long double huge = LDBL_MAX;
+      huge += 1.0L;
+    }
+
+  /* Round to nearest: up when M - r^2 > r.  There are no ties, and no
+     carry out of r, since M <= (2^64 - 1) * 2^64 < (2^64 - 1/2)^2.  */
+  if (rh != 0 || rl > r)
+    r++;
+
+  ux.extu_ext.ext_exp = e / 2 + 63 + EXT_EXP_BIAS;
+  ux.extu_ext.ext_frach = r >> 32;
+  ux.extu_ext.ext_fracl = r;
+  return ux.extu_ld;
+}
+
+#else /* LDBL_MANT_DIG != 64 */
+
 /* Return (x + ulp) for normal positive x.  Assumes no overflow.  */
 
 static inline long double
@@ -190,6 +285,7 @@ sqrtl (long double x)
 
   return ux.extu_ld;
 }
+#endif /* LDBL_MANT_DIG != 64 */
 #endif /* ! _LDBL_EQ_DBL */
 
 
-- 
2.56.0



More information about the Newlib mailing list