about summary refs log tree commit diff
path: root/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c')
-rw-r--r--sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c75
1 files changed, 62 insertions, 13 deletions
diff --git a/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c b/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c
index 8a2d45e2d8..21d1e62dab 100644
--- a/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c
+++ b/sysdeps/ieee754/ldbl-128ibm/mpn2ldbl.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2006
+/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2002, 2003, 2004, 2006, 2007
 	Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -31,19 +31,20 @@ long double
 __mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
 {
   union ibm_extended_long_double u;
-  unsigned long hidden2, lzcount;
+  unsigned long lzcount;
   unsigned long long hi, lo;
+  int exponent2;
 
   u.ieee.negative = sign;
   u.ieee.negative2 = sign;
   u.ieee.exponent = expt + IBM_EXTENDED_LONG_DOUBLE_BIAS;
-  u.ieee.exponent2 = expt - 53 + IBM_EXTENDED_LONG_DOUBLE_BIAS;
+  u.ieee.exponent2 = 0;
+  exponent2 = expt - 53 + IBM_EXTENDED_LONG_DOUBLE_BIAS;
 
 #if BITS_PER_MP_LIMB == 32
   /* The low order 53 bits (52 + hidden) go into the lower double */
   lo = frac_ptr[0];
   lo |= (frac_ptr[1] & ((1LL << (53 - 32)) - 1)) << 32;
-  hidden2 = (frac_ptr[1] >> (52 - 32)) & ((mp_limb_t) 1);
   /* The high order 53 bits (52 + hidden) go into the upper double */
   hi = (frac_ptr[1] >> (53 - 32)) & ((1 << 11) - 1);
   hi |= ((unsigned long long) frac_ptr[2]) << 11;
@@ -51,7 +52,6 @@ __mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
 #elif BITS_PER_MP_LIMB == 64
   /* The low order 53 bits (52 + hidden) go into the lower double */
   lo = frac_ptr[0] & (((mp_limb_t) 1 << 53) - 1);
-  hidden2 = (frac_ptr[0] >> 52) & ((mp_limb_t) 1);
   /* The high order 53 bits (52 + hidden) go into the upper double */
   hi = (frac_ptr[0] >> 53) & (((mp_limb_t) 1 << 11) - 1);
   hi |= (frac_ptr[1] << 11);
@@ -59,14 +59,62 @@ __mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
   #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
 #endif
 
+  if ((hi & (1LL << 52)) == 0 && (hi | lo) != 0)
+    {
+      /* denormal number  */
+      unsigned long long val = hi ? hi : lo;
+
+      if (sizeof (val) == sizeof (long))
+	lzcount = __builtin_clzl (val);
+      else if ((val >> 32) != 0)
+	lzcount = __builtin_clzl ((long) (val >> 32));
+      else
+	lzcount = __builtin_clzl ((long) val) + 32;
+      if (hi)
+	lzcount = lzcount - 11;
+      else
+	lzcount = lzcount + 42;
+
+      if (lzcount > u.ieee.exponent)
+	{
+	  lzcount = u.ieee.exponent;
+	  u.ieee.exponent = 0;
+	  exponent2 -= lzcount;
+	}
+      else
+	{
+	  u.ieee.exponent -= (lzcount - 1);
+	  exponent2 -= (lzcount - 1);
+	}
+
+      if (lzcount <= 53)
+	{
+	  hi = (hi << lzcount) | (lo >> (53 - lzcount));
+	  lo = (lo << lzcount) & ((1LL << 53) - 1);
+	}
+      else
+	{
+	  hi = lo << (lzcount - 53);
+	  lo = 0;
+	}
+    }
+
   if (lo != 0L)
     {
       /* hidden2 bit of low double controls rounding of the high double.
-	 If hidden2 is '1' then round up hi and adjust lo (2nd mantissa)
+	 If hidden2 is '1' and either the explicit mantissa is non-zero
+	 or hi is odd, then round up hi and adjust lo (2nd mantissa)
 	 plus change the sign of the low double to compensate.  */
-      if (hidden2)
+      if ((lo & (1LL << 52)) != 0
+	  && ((hi & 1) != 0 || (lo & ((1LL << 52) - 1))))
 	{
 	  hi++;
+	  if ((hi & ((1LL << 52) - 1)) == 0)
+	    {
+	      if ((hi & (1LL << 53)) != 0)
+		hi -= 1LL << 52;
+	      u.ieee.exponent++;
+	    }
 	  u.ieee.negative2 = !sign;
 	  lo = (1LL << 53) - lo;
 	}
@@ -85,17 +133,18 @@ __mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
       if (lzcount > 0)
 	{
 	  lo = lo << lzcount;
-	  u.ieee.exponent2 = u.ieee.exponent2 - lzcount;
+	  exponent2 = exponent2 - lzcount;
 	}
+      if (exponent2 > 0)
+	u.ieee.exponent2 = exponent2;
+      else
+	lo >>= 1 - exponent2;
     }
   else
-    {
-      u.ieee.negative2 = 0;
-      u.ieee.exponent2 = 0;
-    }
+    u.ieee.negative2 = 0;
 
   u.ieee.mantissa3 = lo & 0xffffffffLL;
-  u.ieee.mantissa2 = (lo >> 32) & 0xffffff;
+  u.ieee.mantissa2 = (lo >> 32) & 0xfffff;
   u.ieee.mantissa1 = hi & 0xffffffffLL;
   u.ieee.mantissa0 = (hi >> 32) & ((1LL << (LDBL_MANT_DIG - 86)) - 1);