Blame sysdeps/i386/ldbl2mpn.c

Packit 6c4009
/* Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "gmp.h"
Packit 6c4009
#include "gmp-impl.h"
Packit 6c4009
#include "longlong.h"
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
/* Convert a `long double' in IEEE854 standard double-precision format to a
Packit 6c4009
   multi-precision integer representing the significand scaled up by its
Packit 6c4009
   number of bits (64 for long double) and an integral power of two
Packit 6c4009
   (MPN frexpl). */
Packit 6c4009
Packit 6c4009
mp_size_t
Packit 6c4009
__mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
Packit 6c4009
			   int *expt, int *is_neg,
Packit 6c4009
			   long double value)
Packit 6c4009
{
Packit 6c4009
  union ieee854_long_double u;
Packit 6c4009
  u.d = value;
Packit 6c4009
Packit 6c4009
  *is_neg = u.ieee.negative;
Packit 6c4009
  *expt = (int) u.ieee.exponent - IEEE854_LONG_DOUBLE_BIAS;
Packit 6c4009
Packit 6c4009
#if BITS_PER_MP_LIMB == 32
Packit 6c4009
  res_ptr[0] = u.ieee.mantissa1; /* Low-order 32 bits of fraction.  */
Packit 6c4009
  res_ptr[1] = u.ieee.mantissa0; /* High-order 32 bits.  */
Packit 6c4009
  #define N 2
Packit 6c4009
#elif BITS_PER_MP_LIMB == 64
Packit 6c4009
  /* Hopefully the compiler will combine the two bitfield extracts
Packit 6c4009
     and this composition into just the original quadword extract.  */
Packit 6c4009
  res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
Packit 6c4009
  #define N 1
Packit 6c4009
#else
Packit 6c4009
  #error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (u.ieee.exponent == 0)
Packit 6c4009
    {
Packit 6c4009
      /* A biased exponent of zero is a special case.
Packit 6c4009
	 Either it is a zero or it is a denormal number.  */
Packit 6c4009
      if (res_ptr[0] == 0 && res_ptr[N - 1] == 0) /* Assumes N<=2.  */
Packit 6c4009
	/* It's zero.  */
Packit 6c4009
	*expt = 0;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* It is a denormal number, meaning it has no implicit leading
Packit 6c4009
	     one bit, and its exponent is in fact the format minimum.  */
Packit 6c4009
	  int cnt;
Packit 6c4009
Packit 6c4009
	  /* One problem with Intel's 80-bit format is that the explicit
Packit 6c4009
	     leading one in the normalized representation has to be zero
Packit 6c4009
	     for denormalized number.  If it is one, the number is according
Packit 6c4009
	     to Intel's specification an invalid number.  We make the
Packit 6c4009
	     representation unique by explicitly clearing this bit.  */
Packit 6c4009
	  res_ptr[N - 1] &= ~((mp_limb_t) 1 << ((LDBL_MANT_DIG - 1) % BITS_PER_MP_LIMB));
Packit 6c4009
Packit 6c4009
	  if (res_ptr[N - 1] != 0)
Packit 6c4009
	    {
Packit 6c4009
	      count_leading_zeros (cnt, res_ptr[N - 1]);
Packit 6c4009
	      if (cnt != 0)
Packit 6c4009
		{
Packit 6c4009
#if N == 2
Packit 6c4009
		  res_ptr[N - 1] = res_ptr[N - 1] << cnt
Packit 6c4009
				   | (res_ptr[0] >> (BITS_PER_MP_LIMB - cnt));
Packit 6c4009
		  res_ptr[0] <<= cnt;
Packit 6c4009
#else
Packit 6c4009
		  res_ptr[N - 1] <<= cnt;
Packit 6c4009
#endif
Packit 6c4009
		}
Packit 6c4009
	      *expt = LDBL_MIN_EXP - 1 - cnt;
Packit 6c4009
	    }
Packit 6c4009
	  else if (res_ptr[0] != 0)
Packit 6c4009
	    {
Packit 6c4009
	      count_leading_zeros (cnt, res_ptr[0]);
Packit 6c4009
	      res_ptr[N - 1] = res_ptr[0] << cnt;
Packit 6c4009
	      res_ptr[0] = 0;
Packit 6c4009
	      *expt = LDBL_MIN_EXP - 1 - BITS_PER_MP_LIMB - cnt;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      /* This is the special case of the pseudo denormal number
Packit 6c4009
		 with only the implicit leading bit set.  The value is
Packit 6c4009
		 in fact a normal number and so we have to treat this
Packit 6c4009
		 case differently.  */
Packit 6c4009
#if N == 2
Packit 6c4009
	      res_ptr[N - 1] = 0x80000000ul;
Packit 6c4009
#else
Packit 6c4009
	      res_ptr[0] = 0x8000000000000000ul;
Packit 6c4009
#endif
Packit 6c4009
	      *expt = LDBL_MIN_EXP - 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else if (u.ieee.exponent < 0x7fff
Packit 6c4009
#if N == 2
Packit 6c4009
	   && res_ptr[0] == 0
Packit 6c4009
#endif
Packit 6c4009
	   && res_ptr[N - 1] == 0)
Packit 6c4009
    /* Pseudo zero.  */
Packit 6c4009
    *expt = 0;
Packit 6c4009
Packit 6c4009
  return N;
Packit 6c4009
}