Blame sysdeps/ieee754/ldbl-128ibm/math_ldbl.h

Packit 6c4009
/* Manipulation of the bit representation of 'long double' quantities.
Packit 6c4009
   Copyright (C) 2006-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
#ifndef _MATH_LDBL_H_
Packit 6c4009
#define _MATH_LDBL_H_ 1
Packit 6c4009
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
/* To suit our callers we return *hi64 and *lo64 as if they came from
Packit 6c4009
   an ieee854 112 bit mantissa, that is, 48 bits in *hi64 (plus one
Packit 6c4009
   implicit bit) and 64 bits in *lo64.  */
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
ldbl_extract_mantissa (int64_t *hi64, uint64_t *lo64, int *exp, long double x)
Packit 6c4009
{
Packit 6c4009
  /* We have 105 bits of mantissa plus one implicit digit.  Since
Packit 6c4009
     106 bits are representable we use the first implicit digit for
Packit 6c4009
     the number before the decimal point and the second implicit bit
Packit 6c4009
     as bit 53 of the mantissa.  */
Packit 6c4009
  uint64_t hi, lo;
Packit 6c4009
  union ibm_extended_long_double u;
Packit 6c4009
Packit 6c4009
  u.ld = x;
Packit 6c4009
  *exp = u.d[0].ieee.exponent - IEEE754_DOUBLE_BIAS;
Packit 6c4009
Packit 6c4009
  lo = ((uint64_t) u.d[1].ieee.mantissa0 << 32) | u.d[1].ieee.mantissa1;
Packit 6c4009
  hi = ((uint64_t) u.d[0].ieee.mantissa0 << 32) | u.d[0].ieee.mantissa1;
Packit 6c4009
Packit 6c4009
  if (u.d[0].ieee.exponent != 0)
Packit 6c4009
    {
Packit 6c4009
      int ediff;
Packit 6c4009
Packit 6c4009
      /* If not a denormal or zero then we have an implicit 53rd bit.  */
Packit 6c4009
      hi |= (uint64_t) 1 << 52;
Packit 6c4009
Packit 6c4009
      if (u.d[1].ieee.exponent != 0)
Packit 6c4009
	lo |= (uint64_t) 1 << 52;
Packit 6c4009
      else
Packit 6c4009
	/* A denormal is to be interpreted as having a biased exponent
Packit 6c4009
	   of 1.  */
Packit 6c4009
	lo = lo << 1;
Packit 6c4009
Packit 6c4009
      /* We are going to shift 4 bits out of hi later, because we only
Packit 6c4009
	 want 48 bits in *hi64.  That means we want 60 bits in lo, but
Packit 6c4009
	 we currently only have 53.  Shift the value up.  */
Packit 6c4009
      lo = lo << 7;
Packit 6c4009
Packit 6c4009
      /* The lower double is normalized separately from the upper.
Packit 6c4009
	 We may need to adjust the lower mantissa to reflect this.
Packit 6c4009
	 The difference between the exponents can be larger than 53
Packit 6c4009
	 when the low double is much less than 1ULP of the upper
Packit 6c4009
	 (in which case there are significant bits, all 0's or all
Packit 6c4009
	 1's, between the two significands).  The difference between
Packit 6c4009
	 the exponents can be less than 53 when the upper double
Packit 6c4009
	 exponent is nearing its minimum value (in which case the low
Packit 6c4009
	 double is denormal ie. has an exponent of zero).  */
Packit 6c4009
      ediff = u.d[0].ieee.exponent - u.d[1].ieee.exponent - 53;
Packit 6c4009
      if (ediff > 0)
Packit 6c4009
	{
Packit 6c4009
	  if (ediff < 64)
Packit 6c4009
	    lo = lo >> ediff;
Packit 6c4009
	  else
Packit 6c4009
	    lo = 0;
Packit 6c4009
	}
Packit 6c4009
      else if (ediff < 0)
Packit 6c4009
	lo = lo << -ediff;
Packit 6c4009
Packit 6c4009
      if (u.d[0].ieee.negative != u.d[1].ieee.negative
Packit 6c4009
	  && lo != 0)
Packit 6c4009
	{
Packit 6c4009
	  hi--;
Packit 6c4009
	  lo = ((uint64_t) 1 << 60) - lo;
Packit 6c4009
	  if (hi < (uint64_t) 1 << 52)
Packit 6c4009
	    {
Packit 6c4009
	      /* We have a borrow from the hidden bit, so shift left 1.  */
Packit 6c4009
	      hi = (hi << 1) | (lo >> 59);
Packit 6c4009
	      lo = (((uint64_t) 1 << 60) - 1) & (lo << 1);
Packit 6c4009
	      *exp = *exp - 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    /* If the larger magnitude double is denormal then the smaller
Packit 6c4009
       one must be zero.  */
Packit 6c4009
    hi = hi << 1;
Packit 6c4009
Packit 6c4009
  *lo64 = (hi << 60) | lo;
Packit 6c4009
  *hi64 = hi >> 4;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline long double
Packit 6c4009
ldbl_insert_mantissa (int sign, int exp, int64_t hi64, uint64_t lo64)
Packit 6c4009
{
Packit 6c4009
  union ibm_extended_long_double u;
Packit 6c4009
  int expnt2;
Packit 6c4009
  uint64_t hi, lo;
Packit 6c4009
Packit 6c4009
  u.d[0].ieee.negative = sign;
Packit 6c4009
  u.d[1].ieee.negative = sign;
Packit 6c4009
  u.d[0].ieee.exponent = exp + IEEE754_DOUBLE_BIAS;
Packit 6c4009
  u.d[1].ieee.exponent = 0;
Packit 6c4009
  expnt2 = exp - 53 + IEEE754_DOUBLE_BIAS;
Packit 6c4009
Packit 6c4009
  /* Expect 113 bits (112 bits + hidden) right justified in two longs.
Packit 6c4009
     The low order 53 bits (52 + hidden) go into the lower double */
Packit 6c4009
  lo = (lo64 >> 7) & (((uint64_t) 1 << 53) - 1);
Packit 6c4009
  /* The high order 53 bits (52 + hidden) go into the upper double */
Packit 6c4009
  hi = lo64 >> 60;
Packit 6c4009
  hi |= hi64 << 4;
Packit 6c4009
Packit 6c4009
  if (lo != 0)
Packit 6c4009
    {
Packit 6c4009
      int lzcount;
Packit 6c4009
Packit 6c4009
      /* hidden bit of low double controls rounding of the high double.
Packit 6c4009
	 If hidden is '1' and either the explicit mantissa is non-zero
Packit 6c4009
	 or hi is odd, then round up hi and adjust lo (2nd mantissa)
Packit 6c4009
	 plus change the sign of the low double to compensate.  */
Packit 6c4009
      if ((lo & ((uint64_t) 1 << 52)) != 0
Packit 6c4009
	  && ((hi & 1) != 0 || (lo & (((uint64_t) 1 << 52) - 1)) != 0))
Packit 6c4009
	{
Packit 6c4009
	  hi++;
Packit 6c4009
	  if ((hi & ((uint64_t) 1 << 53)) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      hi = hi >> 1;
Packit 6c4009
	      u.d[0].ieee.exponent++;
Packit 6c4009
	    }
Packit 6c4009
	  u.d[1].ieee.negative = !sign;
Packit 6c4009
	  lo = ((uint64_t) 1 << 53) - lo;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Normalize the low double.  Shift the mantissa left until
Packit 6c4009
	 the hidden bit is '1' and adjust the exponent accordingly.  */
Packit 6c4009
Packit 6c4009
      if (sizeof (lo) == sizeof (long))
Packit 6c4009
	lzcount = __builtin_clzl (lo);
Packit 6c4009
      else if ((lo >> 32) != 0)
Packit 6c4009
	lzcount = __builtin_clzl ((long) (lo >> 32));
Packit 6c4009
      else
Packit 6c4009
	lzcount = __builtin_clzl ((long) lo) + 32;
Packit 6c4009
      lzcount = lzcount - (64 - 53);
Packit 6c4009
      lo <<= lzcount;
Packit 6c4009
      expnt2 -= lzcount;
Packit 6c4009
Packit 6c4009
      if (expnt2 >= 1)
Packit 6c4009
	/* Not denormal.  */
Packit 6c4009
	u.d[1].ieee.exponent = expnt2;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Is denormal.  Note that biased exponent of 0 is treated
Packit 6c4009
	     as if it was 1, hence the extra shift.  */
Packit 6c4009
	  if (expnt2 > -53)
Packit 6c4009
	    lo >>= 1 - expnt2;
Packit 6c4009
	  else
Packit 6c4009
	    lo = 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    u.d[1].ieee.negative = 0;
Packit 6c4009
Packit 6c4009
  u.d[1].ieee.mantissa1 = lo;
Packit 6c4009
  u.d[1].ieee.mantissa0 = lo >> 32;
Packit 6c4009
  u.d[0].ieee.mantissa1 = hi;
Packit 6c4009
  u.d[0].ieee.mantissa0 = hi >> 32;
Packit 6c4009
  return u.ld;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Handy utility functions to pack/unpack/cononicalize and find the nearbyint
Packit 6c4009
   of long double implemented as double double.  */
Packit 6c4009
static inline long double
Packit 6c4009
default_ldbl_pack (double a, double aa)
Packit 6c4009
{
Packit 6c4009
  union ibm_extended_long_double u;
Packit 6c4009
  u.d[0].d = a;
Packit 6c4009
  u.d[1].d = aa;
Packit 6c4009
  return u.ld;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
default_ldbl_unpack (long double l, double *a, double *aa)
Packit 6c4009
{
Packit 6c4009
  union ibm_extended_long_double u;
Packit 6c4009
  u.ld = l;
Packit 6c4009
  *a = u.d[0].d;
Packit 6c4009
  *aa = u.d[1].d;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifndef ldbl_pack
Packit 6c4009
# define ldbl_pack   default_ldbl_pack
Packit 6c4009
#endif
Packit 6c4009
#ifndef ldbl_unpack
Packit 6c4009
# define ldbl_unpack default_ldbl_unpack
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Extract high double.  */
Packit 6c4009
#define ldbl_high(x) ((double) x)
Packit 6c4009
Packit 6c4009
/* Convert a finite long double to canonical form.
Packit 6c4009
   Does not handle +/-Inf properly.  */
Packit 6c4009
static inline void
Packit 6c4009
ldbl_canonicalize (double *a, double *aa)
Packit 6c4009
{
Packit 6c4009
  double xh, xl;
Packit 6c4009
Packit 6c4009
  xh = *a + *aa;
Packit 6c4009
  xl = (*a - xh) + *aa;
Packit 6c4009
  *a = xh;
Packit 6c4009
  *aa = xl;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Simple inline nearbyint (double) function.
Packit 6c4009
   Only works in the default rounding mode
Packit 6c4009
   but is useful in long double rounding functions.  */
Packit 6c4009
static inline double
Packit 6c4009
ldbl_nearbyint (double a)
Packit 6c4009
{
Packit 6c4009
  double two52 = 0x1p52;
Packit 6c4009
Packit 6c4009
  if (__glibc_likely ((__builtin_fabs (a) < two52)))
Packit 6c4009
    {
Packit 6c4009
      if (__glibc_likely ((a > 0.0)))
Packit 6c4009
	{
Packit 6c4009
	  a += two52;
Packit 6c4009
	  a -= two52;
Packit 6c4009
	}
Packit 6c4009
      else if (__glibc_likely ((a < 0.0)))
Packit 6c4009
	{
Packit 6c4009
	  a = two52 - a;
Packit 6c4009
	  a = -(a - two52);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return a;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Canonicalize a result from an integer rounding function, in any
Packit 6c4009
   rounding mode.  *A and *AA are finite and integers, with *A being
Packit 6c4009
   nonzero; if the result is not already canonical, *AA is plus or
Packit 6c4009
   minus a power of 2 that does not exceed the least set bit in
Packit 6c4009
   *A.  */
Packit 6c4009
static inline void
Packit 6c4009
ldbl_canonicalize_int (double *a, double *aa)
Packit 6c4009
{
Packit 6c4009
  /* Previously we used EXTRACT_WORDS64 from math_private.h, but in order
Packit 6c4009
     to avoid including internal headers we duplicate that code here.  */
Packit 6c4009
  uint64_t ax, aax;
Packit 6c4009
  union { double value; uint64_t word; } extractor;
Packit 6c4009
  extractor.value = *a;
Packit 6c4009
  ax = extractor.word;
Packit 6c4009
  extractor.value = *aa;
Packit 6c4009
  aax = extractor.word;
Packit 6c4009
Packit 6c4009
  int expdiff = ((ax >> 52) & 0x7ff) - ((aax >> 52) & 0x7ff);
Packit 6c4009
  if (expdiff <= 53)
Packit 6c4009
    {
Packit 6c4009
      if (expdiff == 53)
Packit 6c4009
	{
Packit 6c4009
	  /* Half way between two double values; noncanonical iff the
Packit 6c4009
	     low bit of A's mantissa is 1.  */
Packit 6c4009
	  if ((ax & 1) != 0)
Packit 6c4009
	    {
Packit 6c4009
	      *a += 2 * *aa;
Packit 6c4009
	      *aa = -*aa;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* The sum can be represented in a single double.  */
Packit 6c4009
	  *a += *aa;
Packit 6c4009
	  *aa = 0;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* math_ldbl.h */