olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1162895-2.patch

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