12745e
commit aa5f0ff11ad2cc85277c64cf65c723a9664e1149
12745e
Author: Alan Modra <amodra@gmail.com>
12745e
Date:   Wed Apr 16 19:33:32 2014 +0930
12745e
12745e
    Correct IBM long double frexpl.
12745e
    
12745e
    Besides fixing the bugzilla, this also fixes corner-cases where the high
12745e
    and low double differ greatly in magnitude, and handles a denormal
12745e
    input without resorting to a fp rescale.
12745e
    
12745e
    	[BZ #16740]
12745e
    	[BZ #16619]
12745e
    	* sysdeps/ieee754/ldbl-128ibm/s_frexpl.c (__frexpl): Rewrite.
12745e
    	* math/libm-test.inc (frexp_test_data): Add tests.
12745e
12745e
diff --git glibc-2.17-c758a686/math/libm-test.inc glibc-2.17-c758a686/math/libm-test.inc
12745e
index 5e6789f..a4bf0b8 100644
12745e
--- glibc-2.17-c758a686/math/libm-test.inc
12745e
+++ glibc-2.17-c758a686/math/libm-test.inc
12745e
@@ -5691,6 +5691,15 @@ frexp_test (void)
12745e
   TEST_fI_f1 (frexp, 12.8L, 0.8L, 4);
12745e
   TEST_fI_f1 (frexp, -27.34L, -0.854375L, 5);
12745e
 
12745e
+#if defined TEST_LDOUBLE && LDBL_MANT_DIG >= 106
12745e
+  TEST_fI_f1 (frexp, 1.0L-0x1p-106L, 1.0L-0x1p-106L, 0),
12745e
+  TEST_fI_f1 (frexp, 1.0L, 0.5L, 1),
12745e
+  TEST_fI_f1 (frexp, 1.0L+0x1p-105L, 0.5L+0x1p-106L, 1),
12745e
+  TEST_fI_f1 (frexp, -1.0L+0x1p-106L, -1.0L+0x1p-106L, 0),
12745e
+  TEST_fI_f1 (frexp, -1.0L, -0.5L, 1),
12745e
+  TEST_fI_f1 (frexp, -1.0L-0x1p-105L, -0.5L-0x1p-106L, 1),
12745e
+#endif
12745e
+
12745e
   END (frexp);
12745e
 }
12745e
 
12745e
--- glibc-2.17-c758a686/sysdeps/ieee754/ldbl-128ibm/s_frexpl.c	2015-06-16 15:37:44.293960271 -0400
12745e
+++ glibc-2.17-c758a686/sysdeps/ieee754/ldbl-128ibm/s_frexpl.c	2015-06-16 13:53:25.578700428 -0400
12745e
@@ -31,57 +31,115 @@
12745e
 #include <math_private.h>
12745e
 #include <math_ldbl_opt.h>
12745e
 
12745e
-static const long double
12745e
-two107 = 162259276829213363391578010288128.0; /* 0x4670000000000000, 0 */
12745e
-
12745e
 long double __frexpl(long double x, int *eptr)
12745e
 {
12745e
-	uint64_t hx, lx, ix, ixl;
12745e
-	int64_t explo;
12745e
-	double xhi, xlo;
12745e
-
12745e
-	ldbl_unpack (x, &xhi, &xlo;;
12745e
-	EXTRACT_WORDS64 (hx, xhi);
12745e
-	EXTRACT_WORDS64 (lx, xlo);
12745e
-	ixl = 0x7fffffffffffffffULL&lx;
12745e
-	ix =  0x7fffffffffffffffULL&hx;
12745e
-	*eptr = 0;
12745e
-	if(ix>=0x7ff0000000000000ULL||ix==0) return x;	/* 0,inf,nan */
12745e
-	if (ix<0x0010000000000000ULL) {		/* subnormal */
12745e
-	    x *= two107;
12745e
-	    xhi = ldbl_high (x);
12745e
-	    EXTRACT_WORDS64 (hx, xhi);
12745e
-	    ix = hx&0x7fffffffffffffffULL;
12745e
-	    *eptr = -107;
12745e
+  uint64_t hx, lx, ix, ixl;
12745e
+  int64_t explo, expon;
12745e
+  double xhi, xlo;
12745e
+
12745e
+  ldbl_unpack (x, &xhi, &xlo;;
12745e
+  EXTRACT_WORDS64 (hx, xhi);
12745e
+  EXTRACT_WORDS64 (lx, xlo);
12745e
+  ixl = 0x7fffffffffffffffULL & lx;
12745e
+  ix  = 0x7fffffffffffffffULL & hx;
12745e
+  expon = 0;
12745e
+  if (ix >= 0x7ff0000000000000ULL || ix == 0)
12745e
+    {
12745e
+      /* 0,inf,nan.  */
12745e
+      *eptr = expon;
12745e
+      return x;
12745e
+    }
12745e
+  expon = ix >> 52;
12745e
+  if (expon == 0)
12745e
+    {
12745e
+      /* Denormal high double, the low double must be 0.0.  */
12745e
+      int cnt;
12745e
+
12745e
+      /* Normalize.  */
12745e
+      if (sizeof (ix) == sizeof (long))
12745e
+	cnt = __builtin_clzl (ix);
12745e
+      else if ((ix >> 32) != 0)
12745e
+	cnt = __builtin_clzl ((long) (ix >> 32));
12745e
+      else
12745e
+	cnt = __builtin_clzl ((long) ix) + 32;
12745e
+      cnt = cnt - 12;
12745e
+      expon -= cnt;
12745e
+      ix <<= cnt + 1;
12745e
+    }
12745e
+  expon -= 1022;
12745e
+  ix &= 0x000fffffffffffffULL;
12745e
+  hx &= 0x8000000000000000ULL;
12745e
+  hx |= (1022LL << 52) | ix;
12745e
+
12745e
+  if (ixl != 0)
12745e
+    {
12745e
+      /* If the high double is an exact power of two and the low
12745e
+	 double has the opposite sign, then the exponent calculated
12745e
+	 from the high double is one too big.  */
12745e
+      if (ix == 0
12745e
+	  && (int64_t) (hx ^ lx) < 0)
12745e
+	{
12745e
+	  hx += 1LL << 52;
12745e
+	  expon -= 1;
12745e
+	}
12745e
+
12745e
+      explo = ixl >> 52;
12745e
+      if (explo == 0)
12745e
+	{
12745e
+	  /* The low double started out as a denormal.  Normalize its
12745e
+	     mantissa and adjust the exponent.  */
12745e
+	  int cnt;
12745e
+
12745e
+	  if (sizeof (ixl) == sizeof (long))
12745e
+	    cnt = __builtin_clzl (ixl);
12745e
+	  else if ((ixl >> 32) != 0)
12745e
+	    cnt = __builtin_clzl ((long) (ixl >> 32));
12745e
+	  else
12745e
+	    cnt = __builtin_clzl ((long) ixl) + 32;
12745e
+	  cnt = cnt - 12;
12745e
+	  explo -= cnt;
12745e
+	  ixl <<= cnt + 1;
12745e
+	}
12745e
+
12745e
+      /* With variable precision we can't assume much about the
12745e
+	 magnitude of the returned low double.  It may even be a
12745e
+	 denormal.  */
12745e
+      explo -= expon;
12745e
+      ixl &= 0x000fffffffffffffULL;
12745e
+      lx  &= 0x8000000000000000ULL;
12745e
+      if (explo <= 0)
12745e
+	{
12745e
+	  /* Handle denormal low double.  */
12745e
+	  if (explo > -52)
12745e
+	    {
12745e
+	      ixl |= 1LL << 52;
12745e
+	      ixl >>= 1 - explo;
12745e
+	    }
12745e
+	  else
12745e
+	    {
12745e
+	      ixl = 0;
12745e
+	      lx = 0;
12745e
+	      if ((hx & 0x7ff0000000000000ULL) == (1023LL << 52))
12745e
+		{
12745e
+		  /* Oops, the adjustment we made above for values a
12745e
+		     little smaller than powers of two turned out to
12745e
+		     be wrong since the returned low double will be
12745e
+		     zero.  This can happen if the input was
12745e
+		     something weird like 0x1p1000 - 0x1p-1000.  */
12745e
+		  hx -= 1LL << 52;
12745e
+		  expon += 1;
12745e
+		}
12745e
+	    }
12745e
+	  explo = 0;
12745e
 	}
12745e
-	*eptr += (ix>>52)-1022;
12745e
+      lx |= (explo << 52) | ixl;
12745e
+    }
12745e
 
12745e
-	if (ixl != 0ULL) {
12745e
-	  explo = (ixl>>52) - (ix>>52) + 0x3fe;
12745e
-	  if ((ixl&0x7ff0000000000000ULL) == 0LL) {
12745e
-	    /* the lower double is a denormal so we need to correct its
12745e
-	       mantissa and perhaps its exponent.  */
12745e
-	    int cnt;
12745e
-
12745e
-	    if (sizeof (ixl) == sizeof (long))
12745e
-	      cnt = __builtin_clzl (ixl);
12745e
-	    else if ((ixl >> 32) != 0)
12745e
-	      cnt = __builtin_clzl ((long) (ixl >> 32));
12745e
-	    else
12745e
-	      cnt = __builtin_clzl ((long) ixl) + 32;
12745e
-	    cnt = cnt - 12;
12745e
-	    lx = (lx&0x8000000000000000ULL) | ((explo-cnt)<<52)
12745e
-	       | ((ixl<<(cnt+1))&0x000fffffffffffffULL);
12745e
-	  } else
12745e
-	    lx = (lx&0x800fffffffffffffULL) | (explo<<52);
12745e
-	} else
12745e
-	  lx = 0ULL;
12745e
-
12745e
-	hx = (hx&0x800fffffffffffffULL) | 0x3fe0000000000000ULL;
12745e
-	INSERT_WORDS64 (xhi, hx);
12745e
-	INSERT_WORDS64 (xlo, lx);
12745e
-	x = ldbl_pack (xhi, xlo);
12745e
-	return x;
12745e
+  INSERT_WORDS64 (xhi, hx);
12745e
+  INSERT_WORDS64 (xlo, lx);
12745e
+  x = ldbl_pack (xhi, xlo);
12745e
+  *eptr = expon;
12745e
+  return x;
12745e
 }
12745e
 #ifdef IS_IN_libm
12745e
 long_double_symbol (libm, __frexpl, frexpl);