2006-01-28 00:15:15 +00:00
|
|
|
/* e_fmodl.c -- long double version of e_fmod.c.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* ====================================================
|
|
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
|
|
* Permission to use, copy, modify, and distribute this
|
|
|
|
* software is freely granted, provided that this notice
|
|
|
|
* is preserved.
|
|
|
|
* ====================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __ieee754_fmodl(x,y)
|
|
|
|
* Return x mod y in exact arithmetic
|
|
|
|
* Method: shift and subtract
|
|
|
|
*/
|
|
|
|
|
2012-03-09 19:29:16 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <math_private.h>
|
2006-01-28 00:15:15 +00:00
|
|
|
#include <ieee754.h>
|
2019-07-16 15:17:22 +00:00
|
|
|
#include <libm-alias-finite.h>
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
static const long double one = 1.0, Zero[] = {0.0, -0.0,};
|
|
|
|
|
2011-10-12 15:27:51 +00:00
|
|
|
long double
|
|
|
|
__ieee754_fmodl (long double x, long double y)
|
2006-01-28 00:15:15 +00:00
|
|
|
{
|
2013-08-17 08:55:51 +00:00
|
|
|
int64_t hx, hy, hz, sx, sy;
|
|
|
|
uint64_t lx, ly, lz;
|
|
|
|
int n, ix, iy;
|
|
|
|
double xhi, xlo, yhi, ylo;
|
2006-01-28 00:15:15 +00:00
|
|
|
|
2013-08-17 08:55:51 +00:00
|
|
|
ldbl_unpack (x, &xhi, &xlo);
|
|
|
|
EXTRACT_WORDS64 (hx, xhi);
|
|
|
|
EXTRACT_WORDS64 (lx, xlo);
|
|
|
|
ldbl_unpack (y, &yhi, &ylo);
|
|
|
|
EXTRACT_WORDS64 (hy, yhi);
|
|
|
|
EXTRACT_WORDS64 (ly, ylo);
|
2006-01-28 00:15:15 +00:00
|
|
|
sx = hx&0x8000000000000000ULL; /* sign of x */
|
2013-08-17 08:55:51 +00:00
|
|
|
hx ^= sx; /* |x| */
|
|
|
|
sy = hy&0x8000000000000000ULL; /* sign of y */
|
|
|
|
hy ^= sy; /* |y| */
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
/* purge off exception values */
|
2013-08-17 08:55:51 +00:00
|
|
|
if(__builtin_expect(hy==0 ||
|
2012-06-05 13:13:41 +00:00
|
|
|
(hx>=0x7ff0000000000000LL)|| /* y=0,or x not finite */
|
|
|
|
(hy>0x7ff0000000000000LL),0)) /* or y is NaN */
|
2006-01-28 00:15:15 +00:00
|
|
|
return (x*y)/(x*y);
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (hx <= hy))
|
2013-08-17 08:55:51 +00:00
|
|
|
{
|
|
|
|
/* If |x| < |y| return x. */
|
|
|
|
if (hx < hy)
|
|
|
|
return x;
|
|
|
|
/* At this point the absolute value of the high doubles of
|
|
|
|
x and y must be equal. */
|
2016-02-18 22:54:07 +00:00
|
|
|
if ((lx & 0x7fffffffffffffffLL) == 0
|
|
|
|
&& (ly & 0x7fffffffffffffffLL) == 0)
|
|
|
|
/* Both low parts are zero. The result should be an
|
|
|
|
appropriately signed zero, but the subsequent logic
|
|
|
|
could treat them as unequal, depending on the signs
|
|
|
|
of the low parts. */
|
|
|
|
return Zero[(uint64_t) sx >> 63];
|
2013-08-17 08:55:51 +00:00
|
|
|
/* If the low double of y is the same sign as the high
|
|
|
|
double of y (ie. the low double increases |y|)... */
|
|
|
|
if (((ly ^ sy) & 0x8000000000000000LL) == 0
|
|
|
|
/* ... then a different sign low double to high double
|
|
|
|
for x or same sign but lower magnitude... */
|
|
|
|
&& (int64_t) (lx ^ sx) < (int64_t) (ly ^ sy))
|
|
|
|
/* ... means |x| < |y|. */
|
|
|
|
return x;
|
|
|
|
/* If the low double of x differs in sign to the high
|
|
|
|
double of x (ie. the low double decreases |x|)... */
|
|
|
|
if (((lx ^ sx) & 0x8000000000000000LL) != 0
|
|
|
|
/* ... then a different sign low double to high double
|
|
|
|
for y with lower magnitude (we've already caught
|
|
|
|
the same sign for y case above)... */
|
|
|
|
&& (int64_t) (lx ^ sx) > (int64_t) (ly ^ sy))
|
|
|
|
/* ... means |x| < |y|. */
|
|
|
|
return x;
|
|
|
|
/* If |x| == |y| return x*0. */
|
|
|
|
if ((lx ^ sx) == (ly ^ sy))
|
|
|
|
return Zero[(uint64_t) sx >> 63];
|
2006-01-28 00:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the IBM extended format 105 bit mantissa look like the ieee854 112
|
2012-06-05 13:13:41 +00:00
|
|
|
bit mantissa so the following operations will give the correct
|
2006-01-28 00:15:15 +00:00
|
|
|
result. */
|
2013-08-17 08:55:51 +00:00
|
|
|
ldbl_extract_mantissa(&hx, &lx, &ix, x);
|
|
|
|
ldbl_extract_mantissa(&hy, &ly, &iy, y);
|
2006-01-28 00:15:15 +00:00
|
|
|
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (ix == -IEEE754_DOUBLE_BIAS))
|
2013-08-17 08:55:51 +00:00
|
|
|
{
|
|
|
|
/* subnormal x, shift x to normal. */
|
|
|
|
while ((hx & (1LL << 48)) == 0)
|
|
|
|
{
|
|
|
|
hx = (hx << 1) | (lx >> 63);
|
|
|
|
lx = lx << 1;
|
|
|
|
ix -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (iy == -IEEE754_DOUBLE_BIAS))
|
2013-08-17 08:55:51 +00:00
|
|
|
{
|
|
|
|
/* subnormal y, shift y to normal. */
|
|
|
|
while ((hy & (1LL << 48)) == 0)
|
|
|
|
{
|
|
|
|
hy = (hy << 1) | (ly >> 63);
|
|
|
|
ly = ly << 1;
|
|
|
|
iy -= 1;
|
|
|
|
}
|
|
|
|
}
|
2006-01-28 00:15:15 +00:00
|
|
|
|
|
|
|
/* fix point fmod */
|
|
|
|
n = ix - iy;
|
|
|
|
while(n--) {
|
|
|
|
hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
|
|
|
|
if(hz<0){hx = hx+hx+(lx>>63); lx = lx+lx;}
|
|
|
|
else {
|
2013-08-17 08:55:51 +00:00
|
|
|
if((hz|lz)==0) /* return sign(x)*0 */
|
2017-08-03 19:55:04 +00:00
|
|
|
return Zero[(uint64_t)sx>>63];
|
2011-10-12 15:27:51 +00:00
|
|
|
hx = hz+hz+(lz>>63); lx = lz+lz;
|
2006-01-28 00:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
|
|
|
|
if(hz>=0) {hx=hz;lx=lz;}
|
|
|
|
|
|
|
|
/* convert back to floating value and restore the sign */
|
2013-08-17 08:55:51 +00:00
|
|
|
if((hx|lx)==0) /* return sign(x)*0 */
|
2017-08-03 19:55:04 +00:00
|
|
|
return Zero[(uint64_t)sx>>63];
|
2006-01-28 00:15:15 +00:00
|
|
|
while(hx<0x0001000000000000LL) { /* normalize x */
|
|
|
|
hx = hx+hx+(lx>>63); lx = lx+lx;
|
|
|
|
iy -= 1;
|
|
|
|
}
|
2012-06-05 13:13:41 +00:00
|
|
|
if(__builtin_expect(iy>= -1022,0)) { /* normalize output */
|
[BZ #2423]
2006-03-07 Jakub Jelinek <jakub@redhat.com>
[BZ #2423]
* math/libm-test.inc [TEST_LDOUBLE] (ceil_test, floor_test, rint_test,
round_test, trunc_test): Only run some of the new tests if
LDBL_MANT_DIG > 100.
2006-03-03 Steven Munroe <sjmunroe@us.ibm.com>
Alan Modra <amodra@bigpond.net.au>
* sysdeps/powerpc/fpu/fenv_libc.h (__fegetround, __fesetround):
Define inline implementations.
* sysdeps/powerpc/fpu/fegetround.c: Use __fegetround.
* sysdeps/powerpc/fpu/fesetround.c: Use __fesetround.
* sysdeps/powerpc/fpu/math_ldbl.h: New file.
[BZ #2423]
* math/libm-test.inc [TEST_LDOUBLE] (ceil_test, floor_test, rint_test,
round_test, trunc_test): Add new tests.
* sysdeps/ieee754/ldbl-128ibm/math_ldbl.h
(EXTRACT_IBM_EXTENDED_MANTISSA, INSERT_IBM_EXTENDED_MANTISSA):
Removed, replaced with ...
(ldbl_extract_mantissa, ldbl_insert_mantissa, ldbl_pack, ldbl_unpack,
ldbl_canonicalise, ldbl_nearbyint): New functions.
* sysdeps/ieee754/ldbl-128ibm/e_fmodl.c (__ieee754_fmodl): Replace
EXTRACT_IBM_EXTENDED_MANTISSA and INSERT_IBM_EXTENDED_MANTISSA
with ldbl_extract_mantissa and ldbl_insert_mantissa.
* sysdeps/ieee754/ldbl-128ibm/e_rem_pio2l.c (__ieee754_rem_pio2l):
Replace EXTRACT_IBM_EXTENDED_MANTISSA with ldbl_extract_mantissa.
(ldbl_extract_mantissa, ldbl_insert_mantissa): New inline functions.
* sysdeps/ieee754/ldbl-128ibm/s_ceill.c (__ceill): Handle rounding
that spans doubles in IBM long double format.
* sysdeps/ieee754/ldbl-128ibm/s_floorl.c: Likewise.
* sysdeps/ieee754/ldbl-128ibm/s_rintl.c: Likewise.
* sysdeps/ieee754/ldbl-128ibm/s_roundl.c: Likewise.
* sysdeps/ieee754/ldbl-128ibm/s_truncl.c: Likewise.
* sysdeps/powerpc/powerpc64/fpu/s_rintl.S: File removed.
2006-03-16 11:47:24 +00:00
|
|
|
x = ldbl_insert_mantissa((sx>>63), iy, hx, lx);
|
2006-01-28 00:15:15 +00:00
|
|
|
} else { /* subnormal output */
|
|
|
|
n = -1022 - iy;
|
2016-02-18 22:42:06 +00:00
|
|
|
/* We know 1 <= N <= 52, and that there are no nonzero
|
|
|
|
bits in places below 2^-1074. */
|
2017-08-03 19:55:04 +00:00
|
|
|
lx = (lx >> n) | ((uint64_t) hx << (64 - n));
|
2016-02-18 22:42:06 +00:00
|
|
|
hx >>= n;
|
|
|
|
x = ldbl_insert_mantissa((sx>>63), -1023, hx, lx);
|
2006-01-28 00:15:15 +00:00
|
|
|
x *= one; /* create necessary signal */
|
|
|
|
}
|
|
|
|
return x; /* exact output */
|
|
|
|
}
|
2019-07-16 15:17:22 +00:00
|
|
|
libm_alias_finite (__ieee754_fmodl, __fmodl)
|