2020-01-01 00:14:33 +00:00
|
|
|
/* Copyright (C) 1995-2020 Free Software Foundation, Inc.
|
2006-01-28 00:15:15 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
#include <ieee754.h>
|
2015-11-13 12:03:46 +00:00
|
|
|
#include <errno.h>
|
2006-01-28 00:15:15 +00:00
|
|
|
#include <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2015-11-17 18:27:39 +00:00
|
|
|
/* Need to set this when including gmp headers after system headers. */
|
|
|
|
#define HAVE_ALLOCA 1
|
|
|
|
|
|
|
|
#include "gmp.h"
|
|
|
|
#include "gmp-impl.h"
|
|
|
|
|
2006-01-28 00:15:15 +00:00
|
|
|
/* Convert a multi-precision integer of the needed number of bits (106
|
|
|
|
for long double) and an integral power of two to a `long double' in
|
|
|
|
IBM extended format. */
|
|
|
|
|
|
|
|
long double
|
|
|
|
__mpn_construct_long_double (mp_srcptr frac_ptr, int expt, int sign)
|
|
|
|
{
|
|
|
|
union ibm_extended_long_double u;
|
2007-06-08 03:08:45 +00:00
|
|
|
unsigned long lzcount;
|
2006-01-28 00:15:15 +00:00
|
|
|
unsigned long long hi, lo;
|
2007-06-08 03:08:45 +00:00
|
|
|
int exponent2;
|
2006-01-28 00:15:15 +00:00
|
|
|
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[0].ieee.negative = sign;
|
|
|
|
u.d[1].ieee.negative = sign;
|
|
|
|
u.d[0].ieee.exponent = expt + IEEE754_DOUBLE_BIAS;
|
|
|
|
u.d[1].ieee.exponent = 0;
|
|
|
|
exponent2 = expt - 53 + IEEE754_DOUBLE_BIAS;
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
/* 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;
|
|
|
|
hi |= ((unsigned long long) frac_ptr[3]) << (32 + 11);
|
|
|
|
#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);
|
|
|
|
/* 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);
|
|
|
|
#else
|
|
|
|
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
|
|
|
|
#endif
|
|
|
|
|
2007-06-08 03:08:45 +00:00
|
|
|
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)
|
2013-08-17 08:49:44 +00:00
|
|
|
lzcount = lzcount - (64 - 53);
|
2007-06-08 03:08:45 +00:00
|
|
|
else
|
2013-08-17 08:49:44 +00:00
|
|
|
lzcount = lzcount + 53 - (64 - 53);
|
2007-06-08 03:08:45 +00:00
|
|
|
|
2013-08-17 08:42:56 +00:00
|
|
|
if (lzcount > u.d[0].ieee.exponent)
|
2007-06-08 03:08:45 +00:00
|
|
|
{
|
2013-08-17 08:42:56 +00:00
|
|
|
lzcount = u.d[0].ieee.exponent;
|
|
|
|
u.d[0].ieee.exponent = 0;
|
2007-06-08 03:08:45 +00:00
|
|
|
exponent2 -= lzcount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[0].ieee.exponent -= (lzcount - 1);
|
2007-06-08 03:08:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:49:44 +00:00
|
|
|
if (lo != 0)
|
2006-01-28 00:15:15 +00:00
|
|
|
{
|
2013-08-17 08:49:44 +00:00
|
|
|
/* hidden bit of low double controls rounding of the high double.
|
|
|
|
If hidden is '1' and either the explicit mantissa is non-zero
|
2007-06-08 03:08:45 +00:00
|
|
|
or hi is odd, then round up hi and adjust lo (2nd mantissa)
|
2006-01-28 00:15:15 +00:00
|
|
|
plus change the sign of the low double to compensate. */
|
2007-06-08 03:08:45 +00:00
|
|
|
if ((lo & (1LL << 52)) != 0
|
2013-08-17 08:49:44 +00:00
|
|
|
&& ((hi & 1) != 0 || (lo & ((1LL << 52) - 1)) != 0))
|
2006-01-28 00:15:15 +00:00
|
|
|
{
|
|
|
|
hi++;
|
2013-08-17 08:49:44 +00:00
|
|
|
if ((hi & (1LL << 53)) != 0)
|
2007-06-08 03:08:45 +00:00
|
|
|
{
|
2013-08-17 08:49:44 +00:00
|
|
|
hi >>= 1;
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[0].ieee.exponent++;
|
2015-11-13 12:03:46 +00:00
|
|
|
if (u.d[0].ieee.exponent == IEEE754_DOUBLE_BIAS + DBL_MAX_EXP)
|
|
|
|
{
|
|
|
|
/* Overflow. The appropriate overflowed result must
|
|
|
|
be produced (if an infinity, that means the low
|
|
|
|
part must be zero). */
|
|
|
|
__set_errno (ERANGE);
|
|
|
|
return (sign ? -LDBL_MAX : LDBL_MAX) * LDBL_MAX;
|
|
|
|
}
|
2007-06-08 03:08:45 +00:00
|
|
|
}
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[1].ieee.negative = !sign;
|
2006-01-28 00:15:15 +00:00
|
|
|
lo = (1LL << 53) - lo;
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:49:44 +00:00
|
|
|
/* Normalize the low double. Shift the mantissa left until
|
|
|
|
the hidden bit is '1' and adjust the exponent accordingly. */
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
if (sizeof (lo) == sizeof (long))
|
|
|
|
lzcount = __builtin_clzl (lo);
|
|
|
|
else if ((lo >> 32) != 0)
|
|
|
|
lzcount = __builtin_clzl ((long) (lo >> 32));
|
|
|
|
else
|
|
|
|
lzcount = __builtin_clzl ((long) lo) + 32;
|
2013-08-17 08:49:44 +00:00
|
|
|
lzcount = lzcount - (64 - 53);
|
|
|
|
lo <<= lzcount;
|
|
|
|
exponent2 -= lzcount;
|
|
|
|
|
2007-06-08 03:08:45 +00:00
|
|
|
if (exponent2 > 0)
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[1].ieee.exponent = exponent2;
|
2013-08-17 08:49:44 +00:00
|
|
|
else if (exponent2 > -53)
|
2007-06-08 03:08:45 +00:00
|
|
|
lo >>= 1 - exponent2;
|
2013-08-17 08:49:44 +00:00
|
|
|
else
|
|
|
|
lo = 0;
|
2006-01-28 00:15:15 +00:00
|
|
|
}
|
|
|
|
else
|
2013-08-17 08:42:56 +00:00
|
|
|
u.d[1].ieee.negative = 0;
|
2006-01-28 00:15:15 +00:00
|
|
|
|
2013-08-17 08:49:44 +00:00
|
|
|
u.d[1].ieee.mantissa1 = lo;
|
|
|
|
u.d[1].ieee.mantissa0 = lo >> 32;
|
|
|
|
u.d[0].ieee.mantissa1 = hi;
|
|
|
|
u.d[0].ieee.mantissa0 = hi >> 32;
|
2006-01-28 00:15:15 +00:00
|
|
|
|
2013-08-17 08:42:56 +00:00
|
|
|
return u.ld;
|
2006-01-28 00:15:15 +00:00
|
|
|
}
|