diff --git a/Make.inc b/Make.inc index baf65b1e..90ea9eac 100644 --- a/Make.inc +++ b/Make.inc @@ -151,7 +151,7 @@ endif ifneq ($(filter $(ARCH),i387 amd64),) # Determines whether `long double` is the same as `double` on this arch. # linux x86_64, for instance, `long double` is 80 bits wide, whereas on macOS aarch64, -# `long double` is the same as `double`. +# `long double` is the same as `double`. LONG_DOUBLE_NOT_DOUBLE := 1 else ifeq ($(ARCH), aarch64) ifeq ($(filter $(OS),Darwin WINNT),) diff --git a/bsdsrc/Make.files b/bsdsrc/Make.files index 6777fe23..9a33ecb5 100644 --- a/bsdsrc/Make.files +++ b/bsdsrc/Make.files @@ -1 +1 @@ -$(CUR_SRCS) += b_exp.c b_log.c b_tgamma.c +$(CUR_SRCS) += b_tgamma.c diff --git a/bsdsrc/b_exp.c b/bsdsrc/b_exp.c index 6af8dd71..c2ae19d2 100644 --- a/bsdsrc/b_exp.c +++ b/bsdsrc/b_exp.c @@ -1,4 +1,6 @@ -/* +/*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1985, 1993 * The Regents of the University of California. All rights reserved. * @@ -28,10 +30,8 @@ */ /* @(#)exp.c 8.1 (Berkeley) 6/4/93 */ -#include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/bsdsrc/b_exp.c,v 1.9 2011/10/16 05:37:20 das Exp $"); - -#include +//#include "cdefs-compat.h" +//__FBSDID("$FreeBSD$"); /* EXP(X) * RETURN THE EXPONENTIAL OF X @@ -40,14 +40,14 @@ * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86. * * Required system supported functions: - * scalb(x,n) + * ldexp(x,n) * copysign(x,y) - * finite(x) + * isfinite(x) * * Method: * 1. Argument Reduction: given the input x, find r and integer k such * that - * x = k*ln2 + r, |r| <= 0.5*ln2 . + * x = k*ln2 + r, |r| <= 0.5*ln2. * r will be represented as r := z+c for better accuracy. * * 2. Compute exp(r) by @@ -68,105 +68,59 @@ * with 1,156,000 random arguments on a VAX, the maximum observed * error was 0.869 ulps (units in the last place). */ - -#include "mathimpl.h" - -static const double p1 = 0x1.555555555553ep-3; -static const double p2 = -0x1.6c16c16bebd93p-9; -static const double p3 = 0x1.1566aaf25de2cp-14; -static const double p4 = -0x1.bbd41c5d26bf1p-20; -static const double p5 = 0x1.6376972bea4d0p-25; -static const double ln2hi = 0x1.62e42fee00000p-1; -static const double ln2lo = 0x1.a39ef35793c76p-33; -static const double lnhuge = 0x1.6602b15b7ecf2p9; -static const double lntiny = -0x1.77af8ebeae354p9; -static const double invln2 = 0x1.71547652b82fep0; - -#if 0 -OLM_DLLEXPORT double exp(x) -double x; -{ - double z,hi,lo,c; - int k; - -#if !defined(vax)&&!defined(tahoe) - if(x!=x) return(x); /* x is NaN */ -#endif /* !defined(vax)&&!defined(tahoe) */ - if( x <= lnhuge ) { - if( x >= lntiny ) { - - /* argument reduction : x --> x - k*ln2 */ - - k=invln2*x+copysign(0.5,x); /* k=NINT(x/ln2) */ - - /* express x-k*ln2 as hi-lo and let x=hi-lo rounded */ - - hi=x-k*ln2hi; - x=hi-(lo=k*ln2lo); - - /* return 2^k*[1+x+x*c/(2+c)] */ - z=x*x; - c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5)))); - return scalb(1.0+(hi-(lo-(x*c)/(2.0-c))),k); - - } - /* end of x > lntiny */ - - else - /* exp(-big#) underflows to zero */ - if(finite(x)) return(scalb(1.0,-5000)); - - /* exp(-INF) is zero */ - else return(0.0); - } - /* end of x < lnhuge */ - - else - /* exp(INF) is INF, exp(+big#) overflows to INF */ - return( finite(x) ? scalb(1.0,5000) : x); -} -#endif +static const double + p1 = 1.6666666666666660e-01, /* 0x3fc55555, 0x55555553 */ + p2 = -2.7777777777564776e-03, /* 0xbf66c16c, 0x16c0ac3c */ + p3 = 6.6137564717940088e-05, /* 0x3f11566a, 0xb5c2ba0d */ + p4 = -1.6534060280704225e-06, /* 0xbebbbd53, 0x273e8fb7 */ + p5 = 4.1437773411069054e-08; /* 0x3e663f2a, 0x09c94b6c */ + +static const double + ln2hi = 0x1.62e42fee00000p-1, /* High 32 bits round-down. */ + ln2lo = 0x1.a39ef35793c76p-33; /* Next 53 bits round-to-nearst. */ + +static const double + lnhuge = 0x1.6602b15b7ecf2p9, /* (DBL_MAX_EXP + 9) * log(2.) */ + lntiny = -0x1.77af8ebeae354p9, /* (DBL_MIN_EXP - 53 - 10) * log(2.) */ + invln2 = 0x1.71547652b82fep0; /* 1 / log(2.) */ /* returns exp(r = x + c) for |c| < |x| with no overlap. */ -double __exp__D(x, c) -double x, c; +static double +__exp__D(double x, double c) { - double z,hi,lo; + double hi, lo, z; int k; - if (x != x) /* x is NaN */ + if (x != x) /* x is NaN. */ return(x); - if ( x <= lnhuge ) { - if ( x >= lntiny ) { - /* argument reduction : x --> x - k*ln2 */ - z = invln2*x; - k = z + copysign(.5, x); - - /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */ - - hi=(x-k*ln2hi); /* Exact. */ - x= hi - (lo = k*ln2lo-c); - /* return 2^k*[1+x+x*c/(2+c)] */ - z=x*x; - c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5)))); - c = (x*c)/(2.0-c); - - return scalbn(1.+(hi-(lo - c)), k); + if (x <= lnhuge) { + if (x >= lntiny) { + /* argument reduction: x --> x - k*ln2 */ + z = invln2 * x; + k = z + copysign(0.5, x); + + /* + * Express (x + c) - k * ln2 as hi - lo. + * Let x = hi - lo rounded. + */ + hi = x - k * ln2hi; /* Exact. */ + lo = k * ln2lo - c; + x = hi - lo; + + /* Return 2^k*[1+x+x*c/(2+c)] */ + z = x * x; + c = x - z * (p1 + z * (p2 + z * (p3 + z * (p4 + + z * p5)))); + c = (x * c) / (2 - c); + + return (ldexp(1 + (hi - (lo - c)), k)); + } else { + /* exp(-INF) is 0. exp(-big) underflows to 0. */ + return (isfinite(x) ? ldexp(1., -5000) : 0); } - /* end of x > lntiny */ - - else - /* exp(-big#) underflows to zero */ - if(isfinite(x)) return(scalbn(1.0,-5000)); - - /* exp(-INF) is zero */ - else return(0.0); - } - /* end of x < lnhuge */ - - else + } else /* exp(INF) is INF, exp(+big#) overflows to INF */ - return( isfinite(x) ? scalbn(1.0,5000) : x); + return (isfinite(x) ? ldexp(1., 5000) : x); } diff --git a/bsdsrc/b_log.c b/bsdsrc/b_log.c index c20d2903..6e26f466 100644 --- a/bsdsrc/b_log.c +++ b/bsdsrc/b_log.c @@ -1,4 +1,6 @@ -/* +/*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * @@ -28,12 +30,8 @@ */ /* @(#)log.c 8.2 (Berkeley) 11/30/93 */ -#include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/bsdsrc/b_log.c,v 1.9 2008/02/22 02:26:51 das Exp $"); - -#include - -#include "mathimpl.h" +//#include "cdefs-compat.h" +//__FBSDID("$FreeBSD$"); /* Table-driven natural logarithm. * @@ -42,25 +40,27 @@ * Logarithm in IEEE Floating-Point arithmetic." ACM Trans. * Math Software, vol 16. no 4, pp 378-400, Dec 1990). * - * Calculates log(2^m*F*(1+f/F)), |f/j| <= 1/256, + * Calculates log(2^m*F*(1+f/F)), |f/F| <= 1/256, * where F = j/128 for j an integer in [0, 128]. * * log(2^m) = log2_hi*m + log2_tail*m - * since m is an integer, the dominant term is exact. + * The leading term is exact, because m is an integer, * m has at most 10 digits (for subnormal numbers), * and log2_hi has 11 trailing zero bits. * - * log(F) = logF_hi[j] + logF_lo[j] is in tabular form in log_table.h + * log(F) = logF_hi[j] + logF_lo[j] is in table below. * logF_hi[] + 512 is exact. * * log(1+f/F) = 2*f/(2*F + f) + 1/12 * (2*f/(2*F + f))**3 + ... - * the leading term is calculated to extra precision in two + * + * The leading term is calculated to extra precision in two * parts, the larger of which adds exactly to the dominant * m and F terms. + * * There are two cases: - * 1. when m, j are non-zero (m | j), use absolute + * 1. When m and j are non-zero (m | j), use absolute * precision for the leading term. - * 2. when m = j = 0, |1-x| < 1/256, and log(x) ~= (x-1). + * 2. When m = j = 0, |1-x| < 1/256, and log(x) ~= (x-1). * In this case, use a relative precision of 24 bits. * (This is done differently in the original paper) * @@ -68,11 +68,21 @@ * 0 return signalling -Inf * neg return signalling NaN * +Inf return +Inf -*/ + */ #define N 128 -/* Table of log(Fj) = logF_head[j] + logF_tail[j], for Fj = 1+j/128. +/* + * Coefficients in the polynomial approximation of log(1+f/F). + * Domain of x is [0,1./256] with 2**(-64.187) precision. + */ +static const double + A1 = 8.3333333333333329e-02, /* 0x3fb55555, 0x55555555 */ + A2 = 1.2499999999943598e-02, /* 0x3f899999, 0x99991a98 */ + A3 = 2.2321527525957776e-03; /* 0x3f624929, 0xe24e70be */ + +/* + * Table of log(Fj) = logF_head[j] + logF_tail[j], for Fj = 1+j/128. * Used for generation of extend precision logarithms. * The constant 35184372088832 is 2^45, so the divide is exact. * It ensures correct reading of logF_head, even for inaccurate @@ -80,12 +90,7 @@ * right answer for integers less than 2^53.) * Values for log(F) were generated using error < 10^-57 absolute * with the bc -l package. -*/ -static double A1 = .08333333333333178827; -static double A2 = .01250000000377174923; -static double A3 = .002232139987919447809; -static double A4 = .0004348877777076145742; - + */ static double logF_head[N+1] = { 0., .007782140442060381246, @@ -349,118 +354,51 @@ static double logF_tail[N+1] = { .00000000000025144230728376072, -.00000000000017239444525614834 }; - -#if 0 -OLM_DLLEXPORT double -#ifdef _ANSI_SOURCE -log(double x) -#else -log(x) double x; -#endif -{ - int m, j; - double F, f, g, q, u, u2, v, zero = 0.0, one = 1.0; - volatile double u1; - - /* Catch special cases */ - if (x <= 0) - if (x == zero) /* log(0) = -Inf */ - return (-one/zero); - else /* log(neg) = NaN */ - return (zero/zero); - else if (!finite(x)) - return (x+x); /* x = NaN, Inf */ - - /* Argument reduction: 1 <= g < 2; x/2^m = g; */ - /* y = F*(1 + f/F) for |f| <= 2^-8 */ - - m = logb(x); - g = ldexp(x, -m); - if (m == -1022) { - j = logb(g), m += j; - g = ldexp(g, -j); - } - j = N*(g-1) + .5; - F = (1.0/N) * j + 1; /* F*128 is an integer in [128, 512] */ - f = g - F; - - /* Approximate expansion for log(1+f/F) ~= u + q */ - g = 1/(2*F+f); - u = 2*f*g; - v = u*u; - q = u*v*(A1 + v*(A2 + v*(A3 + v*A4))); - - /* case 1: u1 = u rounded to 2^-43 absolute. Since u < 2^-8, - * u1 has at most 35 bits, and F*u1 is exact, as F has < 8 bits. - * It also adds exactly to |m*log2_hi + log_F_head[j] | < 750 - */ - if (m | j) - u1 = u + 513, u1 -= 513; - - /* case 2: |1-x| < 1/256. The m- and j- dependent terms are zero; - * u1 = u to 24 bits. - */ - else - u1 = u, TRUNC(u1); - u2 = (2.0*(f - F*u1) - u1*f) * g; - /* u1 + u2 = 2f/(2F+f) to extra precision. */ - - /* log(x) = log(2^m*F*(1+f/F)) = */ - /* (m*log2_hi+logF_head[j]+u1) + (m*log2_lo+logF_tail[j]+q); */ - /* (exact) + (tiny) */ - - u1 += m*logF_head[N] + logF_head[j]; /* exact */ - u2 = (u2 + logF_tail[j]) + q; /* tiny */ - u2 += logF_tail[N]*m; - return (u1 + u2); -} -#endif - /* * Extra precision variant, returning struct {double a, b;}; - * log(x) = a+b to 63 bits, with a rounded to 26 bits. + * log(x) = a+b to 63 bits, with 'a' rounded to 24 bits. */ -struct Double -#ifdef _ANSI_SOURCE +static struct Double __log__D(double x) -#else -__log__D(x) double x; -#endif { int m, j; - double F, f, g, q, u, v, u2; - volatile double u1; + double F, f, g, q, u, v, u1, u2; struct Double r; - /* Argument reduction: 1 <= g < 2; x/2^m = g; */ - /* y = F*(1 + f/F) for |f| <= 2^-8 */ - - m = logb(x); - g = ldexp(x, -m); + /* + * Argument reduction: 1 <= g < 2; x/2^m = g; + * y = F*(1 + f/F) for |f| <= 2^-8 + */ + g = frexp(x, &m); + g *= 2; + m--; if (m == -1022) { - j = logb(g), m += j; + j = ilogb(g); + m += j; g = ldexp(g, -j); } - j = N*(g-1) + .5; - F = (1.0/N) * j + 1; + j = N * (g - 1) + 0.5; + F = (1. / N) * j + 1; f = g - F; - g = 1/(2*F+f); - u = 2*f*g; - v = u*u; - q = u*v*(A1 + v*(A2 + v*(A3 + v*A4))); - if (m | j) - u1 = u + 513, u1 -= 513; - else - u1 = u, TRUNC(u1); - u2 = (2.0*(f - F*u1) - u1*f) * g; + g = 1 / (2 * F + f); + u = 2 * f * g; + v = u * u; + q = u * v * (A1 + v * (A2 + v * A3)); + if (m | j) { + u1 = u + 513; + u1 -= 513; + } else { + u1 = (float)u; + } + u2 = (2 * (f - F * u1) - u1 * f) * g; - u1 += m*logF_head[N] + logF_head[j]; + u1 += m * logF_head[N] + logF_head[j]; - u2 += logF_tail[j]; u2 += q; - u2 += logF_tail[N]*m; - r.a = u1 + u2; /* Only difference is here */ - TRUNC(r.a); + u2 += logF_tail[j]; + u2 += q; + u2 += logF_tail[N] * m; + r.a = (float)(u1 + u2); /* Only difference is here. */ r.b = (u1 - r.a) + u2; return (r); } diff --git a/bsdsrc/b_tgamma.c b/bsdsrc/b_tgamma.c index f3871ded..6f7134b4 100644 --- a/bsdsrc/b_tgamma.c +++ b/bsdsrc/b_tgamma.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-3-Clause + * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * @@ -27,39 +29,44 @@ * SUCH DAMAGE. */ -/* @(#)gamma.c 8.1 (Berkeley) 6/4/93 */ -#include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/bsdsrc/b_tgamma.c,v 1.10 2008/02/22 02:26:51 das Exp $"); - /* - * This code by P. McIlroy, Oct 1992; + * The original code, FreeBSD's old svn r93211, contained the following + * attribution: + * + * This code by P. McIlroy, Oct 1992; + * + * The financial support of UUNET Communications Services is greatfully + * acknowledged. * - * The financial support of UUNET Communications Services is greatfully - * acknowledged. + * The algorithm remains, but the code has been re-arranged to facilitate + * porting to other precisions. */ +/* @(#)gamma.c 8.1 (Berkeley) 6/4/93 */ +#include "cdefs-compat.h" +//__FBSDID("$FreeBSD$"); + #include #include -#include "mathimpl.h" +/* Used in b_log.c and below. */ +struct Double { + double a; + double b; +}; -/* METHOD: - * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)) - * At negative integers, return NaN and raise invalid. - * - * x < 6.5: - * Use argument reduction G(x+1) = xG(x) to reach the - * range [1.066124,2.066124]. Use a rational - * approximation centered at the minimum (x0+1) to - * ensure monotonicity. - * - * x >= 6.5: Use the asymptotic approximation (Stirling's formula) - * adjusted for equal-ripples: - * - * log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x)) +#include "b_log.c" +#include "b_exp.c" + +/* + * The range is broken into several subranges. Each is handled by its + * helper functions. * - * Keep extra precision in multiplying (x-.5)(log(x)-1), to - * avoid premature round-off. + * x >= 6.0: large_gam(x) + * 6.0 > x >= xleft: small_gam(x) where xleft = 1 + left + x0. + * xleft > x > iota: smaller_gam(x) where iota = 1e-17. + * iota > x > -itoa: Handle x near 0. + * -iota > x : neg_gam * * Special values: * -Inf: return NaN and raise invalid; @@ -77,201 +84,224 @@ * Maximum observed error < 4ulp in 1,000,000 trials. */ -static double neg_gam(double); -static double small_gam(double); -static double smaller_gam(double); -static struct Double large_gam(double); -static struct Double ratfun_gam(double, double); - -/* - * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval - * [1.066.., 2.066..] accurate to 4.25e-19. - */ -#define LEFT -.3955078125 /* left boundary for rat. approx */ -#define x0 .461632144968362356785 /* xmin - 1 */ - -#define a0_hi 0.88560319441088874992 -#define a0_lo -.00000000000000004996427036469019695 -#define P0 6.21389571821820863029017800727e-01 -#define P1 2.65757198651533466104979197553e-01 -#define P2 5.53859446429917461063308081748e-03 -#define P3 1.38456698304096573887145282811e-03 -#define P4 2.40659950032711365819348969808e-03 -#define Q0 1.45019531250000000000000000000e+00 -#define Q1 1.06258521948016171343454061571e+00 -#define Q2 -2.07474561943859936441469926649e-01 -#define Q3 -1.46734131782005422506287573015e-01 -#define Q4 3.07878176156175520361557573779e-02 -#define Q5 5.12449347980666221336054633184e-03 -#define Q6 -1.76012741431666995019222898833e-03 -#define Q7 9.35021023573788935372153030556e-05 -#define Q8 6.13275507472443958924745652239e-06 /* * Constants for large x approximation (x in [6, Inf]) * (Accurate to 2.8*10^-19 absolute) */ -#define lns2pi_hi 0.418945312500000 -#define lns2pi_lo -.000006779295327258219670263595 -#define Pa0 8.33333333333333148296162562474e-02 -#define Pa1 -2.77777777774548123579378966497e-03 -#define Pa2 7.93650778754435631476282786423e-04 -#define Pa3 -5.95235082566672847950717262222e-04 -#define Pa4 8.41428560346653702135821806252e-04 -#define Pa5 -1.89773526463879200348872089421e-03 -#define Pa6 5.69394463439411649408050664078e-03 -#define Pa7 -1.44705562421428915453880392761e-02 - -static const double zero = 0., one = 1.0, tiny = 1e-300; - -OLM_DLLEXPORT double -tgamma(x) - double x; -{ - struct Double u; - if (isgreaterequal(x, 6)) { - if(x > 171.63) - return (x / zero); - u = large_gam(x); - return(__exp__D(u.a, u.b)); - } else if (isgreaterequal(x, 1.0 + LEFT + x0)) - return (small_gam(x)); - else if (isgreater(x, 1.e-17)) - return (smaller_gam(x)); - else if (isgreater(x, -1.e-17)) { - if (x != 0.0) - u.a = one - tiny; /* raise inexact */ - return (one/x); - } else if (!isfinite(x)) - return (x - x); /* x is NaN or -Inf */ - else - return (neg_gam(x)); -} +static const double zero = 0.; +static const volatile double tiny = 1e-300; /* + * x >= 6 + * + * Use the asymptotic approximation (Stirling's formula) adjusted fof + * equal-ripples: + * + * log(G(x)) ~= (x-0.5)*(log(x)-1) + 0.5(log(2*pi)-1) + 1/x*P(1/(x*x)) + * + * Keep extra precision in multiplying (x-.5)(log(x)-1), to avoid + * premature round-off. + * * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error. */ +static const double + ln2pi_hi = 0.41894531250000000, + ln2pi_lo = -6.7792953272582197e-6, + Pa0 = 8.3333333333333329e-02, /* 0x3fb55555, 0x55555555 */ + Pa1 = -2.7777777777735404e-03, /* 0xbf66c16c, 0x16c145ec */ + Pa2 = 7.9365079044114095e-04, /* 0x3f4a01a0, 0x183de82d */ + Pa3 = -5.9523715464225254e-04, /* 0xbf438136, 0x0e681f62 */ + Pa4 = 8.4161391899445698e-04, /* 0x3f4b93f8, 0x21042a13 */ + Pa5 = -1.9065246069191080e-03, /* 0xbf5f3c8b, 0x357cb64e */ + Pa6 = 5.9047708485785158e-03, /* 0x3f782f99, 0xdaf5d65f */ + Pa7 = -1.6484018705183290e-02; /* 0xbf90e12f, 0xc4fb4df0 */ + static struct Double -large_gam(x) - double x; +large_gam(double x) { - double z, p; - struct Double t, u, v; + double p, z, thi, tlo, xhi, xlo; + struct Double u; - z = one/(x*x); - p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7)))))); - p = p/x; + z = 1 / (x * x); + p = Pa0 + z * (Pa1 + z * (Pa2 + z * (Pa3 + z * (Pa4 + z * (Pa5 + + z * (Pa6 + z * Pa7)))))); + p = p / x; u = __log__D(x); - u.a -= one; - v.a = (x -= .5); - TRUNC(v.a); - v.b = x - v.a; - t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */ - t.b = v.b*u.a + x*u.b; - /* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */ - t.b += lns2pi_lo; t.b += p; - u.a = lns2pi_hi + t.b; u.a += t.a; - u.b = t.a - u.a; - u.b += lns2pi_hi; u.b += t.b; + u.a -= 1; + + /* Split (x - 0.5) in high and low parts. */ + x -= 0.5; + xhi = (float)x; + xlo = x - xhi; + + /* Compute t = (x-.5)*(log(x)-1) in extra precision. */ + thi = xhi * u.a; + tlo = xlo * u.a + x * u.b; + + /* Compute thi + tlo + ln2pi_hi + ln2pi_lo + p. */ + tlo += ln2pi_lo; + tlo += p; + u.a = ln2pi_hi + tlo; + u.a += thi; + u.b = thi - u.a; + u.b += ln2pi_hi; + u.b += tlo; return (u); } /* + * Rational approximation, A0 + x * x * P(x) / Q(x), on the interval + * [1.066.., 2.066..] accurate to 4.25e-19. + * + * Returns r.a + r.b = a0 + (z + c)^2 * p / q, with r.a truncated. + */ +static const double +#if 0 + a0_hi = 8.8560319441088875e-1, + a0_lo = -4.9964270364690197e-17, +#else + a0_hi = 8.8560319441088875e-01, /* 0x3fec56dc, 0x82a74aef */ + a0_lo = -4.9642368725563397e-17, /* 0xbc8c9deb, 0xaa64afc3 */ +#endif + P0 = 6.2138957182182086e-1, + P1 = 2.6575719865153347e-1, + P2 = 5.5385944642991746e-3, + P3 = 1.3845669830409657e-3, + P4 = 2.4065995003271137e-3, + Q0 = 1.4501953125000000e+0, + Q1 = 1.0625852194801617e+0, + Q2 = -2.0747456194385994e-1, + Q3 = -1.4673413178200542e-1, + Q4 = 3.0787817615617552e-2, + Q5 = 5.1244934798066622e-3, + Q6 = -1.7601274143166700e-3, + Q7 = 9.3502102357378894e-5, + Q8 = 6.1327550747244396e-6; + +static struct Double +ratfun_gam(double z, double c) +{ + double p, q, thi, tlo; + struct Double r; + + q = Q0 + z * (Q1 + z * (Q2 + z * (Q3 + z * (Q4 + z * (Q5 + + z * (Q6 + z * (Q7 + z * Q8))))))); + p = P0 + z * (P1 + z * (P2 + z * (P3 + z * P4))); + p = p / q; + + /* Split z into high and low parts. */ + thi = (float)z; + tlo = (z - thi) + c; + tlo *= (thi + z); + + /* Split (z+c)^2 into high and low parts. */ + thi *= thi; + q = thi; + thi = (float)thi; + tlo += (q - thi); + + /* Split p/q into high and low parts. */ + r.a = (float)p; + r.b = p - r.a; + + tlo = tlo * p + thi * r.b + a0_lo; + thi *= r.a; /* t = (z+c)^2*(P/Q) */ + r.a = (float)(thi + a0_hi); + r.b = ((a0_hi - r.a) + thi) + tlo; + return (r); /* r = a0 + t */ +} +/* + * x < 6 + * + * Use argument reduction G(x+1) = xG(x) to reach the range [1.066124, + * 2.066124]. Use a rational approximation centered at the minimum + * (x0+1) to ensure monotonicity. + * * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.) * It also has correct monotonicity. */ +static const double + left = -0.3955078125, /* left boundary for rat. approx */ + x0 = 4.6163214496836236e-1; /* xmin - 1 */ + static double -small_gam(x) - double x; +small_gam(double x) { - double y, ym1, t; + double t, y, ym1; struct Double yy, r; - y = x - one; - ym1 = y - one; - if (y <= 1.0 + (LEFT + x0)) { + + y = x - 1; + if (y <= 1 + (left + x0)) { yy = ratfun_gam(y - x0, 0); return (yy.a + yy.b); } - r.a = y; - TRUNC(r.a); - yy.a = r.a - one; - y = ym1; - yy.b = r.b = y - yy.a; + + r.a = (float)y; + yy.a = r.a - 1; + y = y - 1 ; + r.b = yy.b = y - yy.a; + /* Argument reduction: G(x+1) = x*G(x) */ - for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) { - t = r.a*yy.a; - r.b = r.a*yy.b + y*r.b; - r.a = t; - TRUNC(r.a); + for (ym1 = y - 1; ym1 > left + x0; y = ym1--, yy.a--) { + t = r.a * yy.a; + r.b = r.a * yy.b + y * r.b; + r.a = (float)t; r.b += (t - r.a); } + /* Return r*tgamma(y). */ yy = ratfun_gam(y - x0, 0); - y = r.b*(yy.a + yy.b) + r.a*yy.b; - y += yy.a*r.a; + y = r.b * (yy.a + yy.b) + r.a * yy.b; + y += yy.a * r.a; return (y); } /* - * Good on (0, 1+x0+LEFT]. Accurate to 1ulp. + * Good on (0, 1+x0+left]. Accurate to 1 ulp. */ static double -smaller_gam(x) - double x; +smaller_gam(double x) { - double t, d; - struct Double r, xx; - if (x < x0 + LEFT) { - t = x, TRUNC(t); - d = (t+x)*(x-t); + double d, rhi, rlo, t, xhi, xlo; + struct Double r; + + if (x < x0 + left) { + t = (float)x; + d = (t + x) * (x - t); t *= t; - xx.a = (t + x), TRUNC(xx.a); - xx.b = x - xx.a; xx.b += t; xx.b += d; - t = (one-x0); t += x; - d = (one-x0); d -= t; d += x; - x = xx.a + xx.b; + xhi = (float)(t + x); + xlo = x - xhi; + xlo += t; + xlo += d; + t = 1 - x0; + t += x; + d = 1 - x0; + d -= t; + d += x; + x = xhi + xlo; } else { - xx.a = x, TRUNC(xx.a); - xx.b = x - xx.a; + xhi = (float)x; + xlo = x - xhi; t = x - x0; - d = (-x0 -t); d += x; + d = - x0 - t; + d += x; } + r = ratfun_gam(t, d); - d = r.a/x, TRUNC(d); - r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b; - return (d + r.a/x); + d = (float)(r.a / x); + r.a -= d * xhi; + r.a -= d * xlo; + r.a += r.b; + + return (d + r.a / x); } /* - * returns (z+c)^2 * P(z)/Q(z) + a0 + * x < 0 + * + * Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)). + * At negative integers, return NaN and raise invalid. */ -static struct Double -ratfun_gam(z, c) - double z, c; -{ - double p, q; - struct Double r, t; - - q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8))))))); - p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4))); - - /* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */ - p = p/q; - t.a = z, TRUNC(t.a); /* t ~= z + c */ - t.b = (z - t.a) + c; - t.b *= (t.a + z); - q = (t.a *= t.a); /* t = (z+c)^2 */ - TRUNC(t.a); - t.b += (q - t.a); - r.a = p, TRUNC(r.a); /* r = P/Q */ - r.b = p - r.a; - t.b = t.b*p + t.a*r.b + a0_lo; - t.a *= r.a; /* t = (z+c)^2*(P/Q) */ - r.a = t.a + a0_hi, TRUNC(r.a); - r.b = ((a0_hi-r.a) + t.a) + t.b; - return (r); /* r = a0 + t */ -} - static double -neg_gam(x) - double x; +neg_gam(double x) { int sgn = 1; struct Double lg, lsine; @@ -280,23 +310,29 @@ neg_gam(x) y = ceil(x); if (y == x) /* Negative integer. */ return ((x - x) / zero); + z = y - x; if (z > 0.5) - z = one - z; - y = 0.5 * y; + z = 1 - z; + + y = y / 2; if (y == ceil(y)) sgn = -1; - if (z < .25) - z = sin(M_PI*z); + + if (z < 0.25) + z = sinpi(z); else - z = cos(M_PI*(0.5-z)); + z = cospi(0.5 - z); + /* Special case: G(1-x) = Inf; G(x) may be nonzero. */ if (x < -170) { + if (x < -190) - return ((double)sgn*tiny*tiny); - y = one - x; /* exact: 128 < |x| < 255 */ + return (sgn * tiny * tiny); + + y = 1 - x; /* exact: 128 < |x| < 255 */ lg = large_gam(y); - lsine = __log__D(M_PI/z); /* = TRUNC(log(u)) + small */ + lsine = __log__D(M_PI / z); /* = TRUNC(log(u)) + small */ lg.a -= lsine.a; /* exact (opposite signs) */ lg.b -= lsine.b; y = -(lg.a + lg.b); @@ -305,13 +341,56 @@ neg_gam(x) if (sgn < 0) y = -y; return (y); } - y = one-x; - if (one-y == x) + + y = 1 - x; + if (1 - y == x) y = tgamma(y); else /* 1-x is inexact */ - y = -x*tgamma(-x); + y = - x * tgamma(-x); + if (sgn < 0) y = -y; - return (M_PI / (y*z)); + return (M_PI / (y * z)); +} +/* + * xmax comes from lgamma(xmax) - emax * log(2) = 0. + * static const float xmax = 35.040095f + * static const double xmax = 171.624376956302725; + * ld80: LD80C(0xdb718c066b352e20, 10, 1.75554834290446291689e+03L), + * ld128: 1.75554834290446291700388921607020320e+03L, + * + * iota is a sloppy threshold to isolate x = 0. + */ +static const double xmax = 171.624376956302725; +static const double iota = 0x1p-56; + +double +tgamma(double x) +{ + struct Double u; + + if (x >= 6) { + if (x > xmax) + return (x / zero); + u = large_gam(x); + return (__exp__D(u.a, u.b)); + } + + if (x >= 1 + left + x0) + return (small_gam(x)); + + if (x > iota) + return (smaller_gam(x)); + + if (x > -iota) { + if (x != 0.) + u.a = 1 - tiny; /* raise inexact */ + return (1 / x); + } + + if (!isfinite(x)) + return (x - x); /* x is NaN or -Inf */ + + return (neg_gam(x)); } #if (LDBL_MANT_DIG == 53) diff --git a/bsdsrc/mathimpl.h b/bsdsrc/mathimpl.h deleted file mode 100644 index 983c4eb3..00000000 --- a/bsdsrc/mathimpl.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * - * @(#)mathimpl.h 8.1 (Berkeley) 6/4/93 - * $FreeBSD: src/lib/msun/bsdsrc/mathimpl.h,v 1.7 2005/11/18 05:03:12 bde Exp $ - */ - -#ifndef _MATHIMPL_H_ -#define _MATHIMPL_H_ - -#include "cdefs-compat.h" -#include "math_private.h" - -/* - * TRUNC() is a macro that sets the trailing 27 bits in the mantissa of an - * IEEE double variable to zero. It must be expression-like for syntactic - * reasons, and we implement this expression using an inline function - * instead of a pure macro to avoid depending on the gcc feature of - * statement-expressions. - */ -#define TRUNC(d) (_b_trunc(&(d))) - -static __inline void -_b_trunc(volatile double *_dp) -{ - //VBS - //u_int32_t _lw; - u_int32_t _lw; - - GET_LOW_WORD(_lw, *_dp); - SET_LOW_WORD(*_dp, _lw & 0xf8000000); -} - -struct Double { - double a; - double b; -}; - -/* - * Functions internal to the math package, yet not static. - */ -double __exp__D(double, double); -struct Double __log__D(double); - -#endif /* !_MATHIMPL_H_ */ diff --git a/src/Make.files b/src/Make.files index 5170403c..b2c89b2b 100644 --- a/src/Make.files +++ b/src/Make.files @@ -31,6 +31,11 @@ $(CUR_SRCS) = common.c \ s_trunc.c s_truncf.c s_cpow.c s_cpowf.c \ w_cabs.c w_cabsf.c +# IEEE-754 2008 and ISO/IEC TS 18661-4 half-cycle trignometric functions +$(CUR_SRCS) += s_cospi.c s_cospif.c \ + s_sinpi.c s_sinpif.c \ + s_tanpi.c s_tanpif.c + ifneq ($(ARCH), wasm32) $(CUR_SRCS) += \ diff --git a/src/e_pow.c b/src/e_pow.c index a8915523..b67bb08d 100644 --- a/src/e_pow.c +++ b/src/e_pow.c @@ -4,13 +4,13 @@ * Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. * * Permission to use, copy, modify, and distribute this - * software is freely granted, provided that this notice + * software is freely granted, provided that this notice * is preserved. * ==================================================== */ #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/e_pow.c,v 1.14 2011/10/21 06:26:07 das Exp $"); +//__FBSDID("$FreeBSD$"); /* __ieee754_pow(x,y) return x**y * @@ -19,20 +19,20 @@ * 1. Compute and return log2(x) in two pieces: * log2(x) = w1 + w2, * where w1 has 53-24 = 29 bit trailing zeros. - * 2. Perform y*log2(x) = n+y' by simulating muti-precision + * 2. Perform y*log2(x) = n+y' by simulating multi-precision * arithmetic, where |y'|<=0.5. * 3. Return x**y = 2**n*exp(y'*log2) * * Special cases: * 1. (anything) ** 0 is 1 * 2. (anything) ** 1 is itself - * 3. (anything) ** NAN is NAN + * 3. (anything) ** NAN is NAN except 1 ** NAN = 1 * 4. NAN ** (anything except 0) is NAN * 5. +-(|x| > 1) ** +INF is +INF * 6. +-(|x| > 1) ** -INF is +0 * 7. +-(|x| < 1) ** +INF is +0 * 8. +-(|x| < 1) ** -INF is +INF - * 9. +-1 ** +-INF is NAN + * 9. +-1 ** +-INF is 1 * 10. +0 ** (+anything except 0, NAN) is +0 * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 * 12. +0 ** (-anything except 0, NAN) is +INF @@ -47,19 +47,18 @@ * Accuracy: * pow(x,y) returns x**y nearly rounded. In particular * pow(integer,integer) - * always returns the correct integer provided it is + * always returns the correct integer provided it is * representable. * * Constants : - * The hexadecimal values are the intended ones for the following - * constants. The decimal values may be used, provided that the - * compiler will convert from decimal to binary accurately enough + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough * to produce the hexadecimal values shown. */ #include -#include - +#include "math.h" #include "math_private.h" static const double @@ -67,6 +66,9 @@ bp[] = {1.0, 1.5,}, dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */ dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */ zero = 0.0, +half = 0.5, +qrtr = 0.25, +thrd = 3.3333333333333331e-01, /* 0x3fd55555, 0x55555555 */ one = 1.0, two = 2.0, two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */ @@ -95,7 +97,7 @@ ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */ ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ -OLM_DLLEXPORT double +double __ieee754_pow(double x, double y) { double z,ax,z_h,z_l,p_h,p_l; @@ -109,15 +111,15 @@ __ieee754_pow(double x, double y) ix = hx&0x7fffffff; iy = hy&0x7fffffff; /* y==zero: x**0 = 1 */ - if((iy|ly)==0) return one; + if((iy|ly)==0) return one; /* x==1: 1**y = 1, even if y is NaN */ if (hx==0x3ff00000 && lx == 0) return one; /* y!=zero: result is NaN if either arg is NaN */ if(ix > 0x7ff00000 || ((ix==0x7ff00000)&&(lx!=0)) || - iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) - return (x+0.0)+(y+0.0); + iy > 0x7ff00000 || ((iy==0x7ff00000)&&(ly!=0))) + return nan_mix(x, y); /* determine if y is an odd int when x < 0 * yisint = 0 ... y is not an integer @@ -125,42 +127,37 @@ __ieee754_pow(double x, double y) * yisint = 2 ... y is an even int */ yisint = 0; - if(hx<0) { + if(hx<0) { if(iy>=0x43400000) yisint = 2; /* even integer y */ else if(iy>=0x3ff00000) { k = (iy>>20)-0x3ff; /* exponent */ if(k>20) { j = ly>>(52-k); - if((j<<(52-k))==ly) yisint = 2-(j&1); + if(((u_int32_t)j<<(52-k))==ly) yisint = 2-(j&1); } else if(ly==0) { j = iy>>(20-k); if((j<<(20-k))==iy) yisint = 2-(j&1); } - } - } + } + } /* special value of y */ - if(ly==0) { + if(ly==0) { if (iy==0x7ff00000) { /* y is +-inf */ if(((ix-0x3ff00000)|lx)==0) - return one; /* (-1)**+-inf is NaN */ + return one; /* (-1)**+-inf is 1 */ else if (ix >= 0x3ff00000)/* (|x|>1)**+-inf = inf,0 */ return (hy>=0)? y: zero; else /* (|x|<1)**-,+inf = inf,0 */ return (hy<0)?-y: zero; - } + } if(iy==0x3ff00000) { /* y is +-1 */ if(hy<0) return one/x; else return x; } - if(hy==0x40000000) return x*x; /* y is 2 */ - if(hy==0x40080000) return x*x*x; /* y is 3 */ - if(hy==0x40100000) { /* y is 4 */ - u = x*x; - return u*u; - } - if(hy==0x3fe00000) { /* y is 0.5 */ + if(hy==0x40000000) return x*x; /* y is 2 */ + if(hy==0x3fe00000) { /* y is 0.5 */ if(hx>=0) /* x >= +0 */ - return sqrt(x); + return sqrt(x); } } @@ -173,13 +170,13 @@ __ieee754_pow(double x, double y) if(hx<0) { if(((ix-0x3ff00000)|yisint)==0) { z = (z-z)/(z-z); /* (-1)**non-int is NaN */ - } else if(yisint==1) + } else if(yisint==1) z = -z; /* (x<0)**odd = -(|x|**odd) */ } return z; } } - + /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be n = (hx>>31)+1; but ANSI C says a right shift of a signed negative quantity is @@ -201,10 +198,10 @@ __ieee754_pow(double x, double y) /* over/underflow if x is not close to one */ if(ix<0x3fefffff) return (hy<0)? s*huge*huge:s*tiny*tiny; if(ix>0x3ff00000) return (hy>0)? s*huge*huge:s*tiny*tiny; - /* now |1-x| is tiny <= 2**-20, suffice to compute + /* now |1-x| is tiny <= 2**-20, suffice to compute log(x) by x-x^2/2+x^3/3-x^4/4 */ t = ax-one; /* t has 20 trailing zeros */ - w = (t*t)*(0.5-t*(0.3333333333333333333333-t*0.25)); + w = (t*t)*(half-t*(thrd-t*qrtr)); u = ivln2_h*t; /* ivln2_h has 21 sig. bits */ v = t*ivln2_l-w*ivln2; t1 = u+v; @@ -241,9 +238,9 @@ __ieee754_pow(double x, double y) r = s2*s2*(L1+s2*(L2+s2*(L3+s2*(L4+s2*(L5+s2*L6))))); r += s_l*(s_h+ss); s2 = s_h*s_h; - t_h = 3.0+s2+r; + t_h = 3+s2+r; SET_LOW_WORD(t_h,0); - t_l = r-((t_h-3.0)-s2); + t_l = r-((t_h-3)-s2); /* u+v = ss*(1+...) */ u = s_h*t_h; v = s_l*t_h+t_l*ss; @@ -254,7 +251,7 @@ __ieee754_pow(double x, double y) z_h = cp_h*p_h; /* cp_h+cp_l = 2/(3*log2) */ z_l = cp_l*p_h+p_l*cp+dp_l[k]; /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ - t = (double)n; + t = n; t1 = (((z_h+z_l)+dp_h[k])+t); SET_LOW_WORD(t1,0); t2 = z_l-(((t1-t)-dp_h[k])-z_h); @@ -294,7 +291,7 @@ __ieee754_pow(double x, double y) n = ((n&0x000fffff)|0x00100000)>>(20-k); if(j<0) n = -n; p_h -= t; - } + } t = p_l+p_h; SET_LOW_WORD(t,0); u = t*lg2_h; diff --git a/src/e_scalb.c b/src/e_scalb.c new file mode 100644 index 00000000..c0a7b5b7 --- /dev/null +++ b/src/e_scalb.c @@ -0,0 +1,47 @@ + +/* @(#)e_scalb.c 1.3 95/01/18 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * __ieee754_scalb(x, fn) is provide for + * passing various standard test suite. One + * should use scalbn() instead. + */ + +#include "math.h" +#include "math_private.h" + +#ifdef _SCALB_INT +double +__ieee754_scalb(double x, int fn) +#else +double +__ieee754_scalb(double x, double fn) +#endif +{ +#ifdef _SCALB_INT + return scalbn(x,fn); +#else + if (isnan(x)||isnan(fn)) return x*fn; + if (!finite(fn)) { + if(fn>0.0) return x*fn; + else return x/(-fn); + } + if (rint(fn)!=fn) return (fn-fn)/(fn-fn); + if ( fn > 65000.0) return scalbn(x, 65000); + if (-fn > 65000.0) return scalbn(x,-65000); + return scalbn(x,(int)fn); +#endif +} diff --git a/src/e_scalbf.c b/src/e_scalbf.c new file mode 100644 index 00000000..d49e9041 --- /dev/null +++ b/src/e_scalbf.c @@ -0,0 +1,43 @@ +/* e_scalbf.c -- float version of e_scalb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * 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. + * ==================================================== + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "math.h" +#include "math_private.h" + +#ifdef _SCALB_INT +float +__ieee754_scalbf(float x, int fn) +#else +float +__ieee754_scalbf(float x, float fn) +#endif +{ +#ifdef _SCALB_INT + return scalbnf(x,fn); +#else + if ((isnanf)(x)||(isnanf)(fn)) return x*fn; + if (!finitef(fn)) { + if(fn>(float)0.0) return x*fn; + else return x/(-fn); + } + if (rintf(fn)!=fn) return (fn-fn)/(fn-fn); + if ( fn > (float)65000.0) return scalbnf(x, 65000); + if (-fn > (float)65000.0) return scalbnf(x,-65000); + return scalbnf(x,(int)fn); +#endif +} diff --git a/src/k_cosf.c b/src/k_cosf.c index 6774db46..d07a357d 100644 --- a/src/k_cosf.c +++ b/src/k_cosf.c @@ -16,11 +16,10 @@ #ifndef INLINE_KERNEL_COSDF #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/k_cosf.c,v 1.18 2009/06/03 08:16:34 ed Exp $"); +//__FBSDID("$FreeBSD$"); #endif -#include - +#include "openlibm_math.h" #include "math_private.h" /* |cos(x) - c(x)| < 2**-34.1 (~[-5.37e-11, 5.295e-11]). */ @@ -31,11 +30,10 @@ C1 = 0x155553e1053a42.0p-57, /* 0.0416666233237390631894 */ C2 = -0x16c087e80f1e27.0p-62, /* -0.00138867637746099294692 */ C3 = 0x199342e0ee5069.0p-68; /* 0.0000243904487962774090654 */ -#ifndef INLINE_KERNEL_COSDF -extern +#ifdef INLINE_KERNEL_COSDF +static __inline #endif -//__inline float -OLM_DLLEXPORT float +float __kernel_cosdf(double x) { double r, w, z; diff --git a/src/k_cospi.h b/src/k_cospi.h new file mode 100644 index 00000000..2b99b7b5 --- /dev/null +++ b/src/k_cospi.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * The basic kernel for x in [0,0.25]. To use the kernel for cos(x), the + * argument to __kernel_cospi() must be multiplied by pi. + */ + +static inline double +__kernel_cospi(double x) +{ + double hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_cos(hi, lo)); +} diff --git a/src/k_sinf.c b/src/k_sinf.c index 15c8e031..cde449a9 100644 --- a/src/k_sinf.c +++ b/src/k_sinf.c @@ -16,11 +16,10 @@ #ifndef INLINE_KERNEL_SINDF #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/k_sinf.c,v 1.16 2009/06/03 08:16:34 ed Exp $"); +//__FBSDID("$FreeBSD$"); #endif -#include - +#include "openlibm_math.h" #include "math_private.h" /* |sin(x)/x - s(x)| < 2**-37.5 (~[-4.89e-12, 4.824e-12]). */ @@ -30,11 +29,10 @@ S2 = 0x111110896efbb2.0p-59, /* 0.0083333293858894631756 */ S3 = -0x1a00f9e2cae774.0p-65, /* -0.000198393348360966317347 */ S4 = 0x16cd878c3b46a7.0p-71; /* 0.0000027183114939898219064 */ -#ifndef INLINE_KERNEL_SINDF -extern +#ifdef INLINE_KERNEL_SINDF +static __inline #endif -//__inline float -OLM_DLLEXPORT float +float __kernel_sindf(double x) { double r, s, w, z; diff --git a/src/k_sinpi.h b/src/k_sinpi.h new file mode 100644 index 00000000..c6bb43f7 --- /dev/null +++ b/src/k_sinpi.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * The basic kernel for x in [0,0.25]. To use the kernel for sin(x), the + * argument to __kernel_sinpi() must be multiplied by pi. + */ + +static inline double +__kernel_sinpi(double x) +{ + double hi, lo; + + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + return (__kernel_sin(hi, lo, 1)); +} diff --git a/src/k_tanf.c b/src/k_tanf.c index 9c17d1b2..c7b871b2 100644 --- a/src/k_tanf.c +++ b/src/k_tanf.c @@ -15,11 +15,10 @@ #ifndef INLINE_KERNEL_TANDF #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/k_tanf.c,v 1.23 2009/06/03 08:16:34 ed Exp $"); +//__FBSDID("$FreeBSD$"); #endif -#include - +#include "openlibm_math.h" #include "math_private.h" /* |tan(x)/x - t(x)| < 2**-25.5 (~[-2e-08, 2e-08]). */ @@ -33,11 +32,10 @@ T[] = { 0x1362b9bf971bcd.0p-59, /* 0.00946564784943673166728 */ }; -#ifndef INLINE_KERNEL_TANDF -extern +#ifdef INLINE_KERNEL_TANDF +static __inline #endif -//__inline float -OLM_DLLEXPORT float +float __kernel_tandf(double x, int iy) { double z,r,w,s,t,u; diff --git a/src/math_private.h b/src/math_private.h index 15a27b28..0cda9ab9 100644 --- a/src/math_private.h +++ b/src/math_private.h @@ -225,6 +225,110 @@ do { \ #endif #endif +/* + * 2sum gives the same result as 2sumF without requiring |a| >= |b| or + * a == 0, but is slower. + */ +#define _2sum(a, b) do { \ + __typeof(a) __s, __w; \ + \ + __w = (a) + (b); \ + __s = __w - (a); \ + (b) = ((a) - (__w - __s)) + ((b) - __s); \ + (a) = __w; \ +} while (0) + +/* + * 2sumF algorithm. + * + * "Normalize" the terms in the infinite-precision expression a + b for + * the sum of 2 floating point values so that b is as small as possible + * relative to 'a'. (The resulting 'a' is the value of the expression in + * the same precision as 'a' and the resulting b is the rounding error.) + * |a| must be >= |b| or 0, b's type must be no larger than 'a's type, and + * exponent overflow or underflow must not occur. This uses a Theorem of + * Dekker (1971). See Knuth (1981) 4.2.2 Theorem C. The name "TwoSum" + * is apparently due to Skewchuk (1997). + * + * For this to always work, assignment of a + b to 'a' must not retain any + * extra precision in a + b. This is required by C standards but broken + * in many compilers. The brokenness cannot be worked around using + * STRICT_ASSIGN() like we do elsewhere, since the efficiency of this + * algorithm would be destroyed by non-null strict assignments. (The + * compilers are correct to be broken -- the efficiency of all floating + * point code calculations would be destroyed similarly if they forced the + * conversions.) + * + * Fortunately, a case that works well can usually be arranged by building + * any extra precision into the type of 'a' -- 'a' should have type float_t, + * double_t or long double. b's type should be no larger than 'a's type. + * Callers should use these types with scopes as large as possible, to + * reduce their own extra-precision and efficiciency problems. In + * particular, they shouldn't convert back and forth just to call here. + */ +#ifdef DEBUG +#define _2sumF(a, b) do { \ + __typeof(a) __w; \ + volatile __typeof(a) __ia, __ib, __r, __vw; \ + \ + __ia = (a); \ + __ib = (b); \ + assert(__ia == 0 || fabsl(__ia) >= fabsl(__ib)); \ + \ + __w = (a) + (b); \ + (b) = ((a) - __w) + (b); \ + (a) = __w; \ + \ + /* The next 2 assertions are weak if (a) is already long double. */ \ + assert((long double)__ia + __ib == (long double)(a) + (b)); \ + __vw = __ia + __ib; \ + __r = __ia - __vw; \ + __r += __ib; \ + assert(__vw == (a) && __r == (b)); \ +} while (0) +#else /* !DEBUG */ +#define _2sumF(a, b) do { \ + __typeof(a) __w; \ + \ + __w = (a) + (b); \ + (b) = ((a) - __w) + (b); \ + (a) = __w; \ +} while (0) +#endif /* DEBUG */ + +/* + * Set x += c, where x is represented in extra precision as a + b. + * x must be sufficiently normalized and sufficiently larger than c, + * and the result is then sufficiently normalized. + * + * The details of ordering are that |a| must be >= |c| (so that (a, c) + * can be normalized without extra work to swap 'a' with c). The details of + * the normalization are that b must be small relative to the normalized 'a'. + * Normalization of (a, c) makes the normalized c tiny relative to the + * normalized a, so b remains small relative to 'a' in the result. However, + * b need not ever be tiny relative to 'a'. For example, b might be about + * 2**20 times smaller than 'a' to give about 20 extra bits of precision. + * That is usually enough, and adding c (which by normalization is about + * 2**53 times smaller than a) cannot change b significantly. However, + * cancellation of 'a' with c in normalization of (a, c) may reduce 'a' + * significantly relative to b. The caller must ensure that significant + * cancellation doesn't occur, either by having c of the same sign as 'a', + * or by having |c| a few percent smaller than |a|. Pre-normalization of + * (a, b) may help. + * + * This is is a variant of an algorithm of Kahan (see Knuth (1981) 4.2.2 + * exercise 19). We gain considerable efficiency by requiring the terms to + * be sufficiently normalized and sufficiently increasing. + */ +#define _3sumF(a, b, c) do { \ + __typeof(a) __tmp; \ + \ + __tmp = (c); \ + _2sumF(__tmp, (a)); \ + (b) += (a); \ + (a) = __tmp; \ +} while (0) + /* * Common routine to process the arguments to nan(), nanf(), and nanl(). */ @@ -348,24 +452,22 @@ double __ldexp_exp(double,int); double complex __ldexp_cexp(double complex,int); /* float precision kernel functions */ -#ifdef INLINE_REM_PIO2F -__inline +#ifndef INLINE_REM_PIO2F +int __ieee754_rem_pio2f(float,double*); #endif -int __ieee754_rem_pio2f(float,double*); -#ifdef INLINE_KERNEL_SINDF -__inline +#ifndef INLINE_KERNEL_SINDF +float __kernel_sindf(double); #endif -float __kernel_sindf(double); -#ifdef INLINE_KERNEL_COSDF -__inline +#ifndef INLINE_KERNEL_COSDF +float __kernel_cosdf(double); #endif -float __kernel_cosdf(double); -#ifdef INLINE_KERNEL_TANDF -__inline +#ifndef INLINE_KERNEL_TANDF +float __kernel_tandf(double,int); #endif -float __kernel_tandf(double,int); -float __ldexp_expf(float,int); +float __ldexp_expf(float,int); +#ifdef _COMPLEX_H float complex __ldexp_cexpf(float complex,int); +#endif /* long double precision kernel functions */ long double __kernel_sinl(long double, long double, int); diff --git a/src/s_cospi.c b/src/s_cospi.c new file mode 100644 index 00000000..20899aa3 --- /dev/null +++ b/src/s_cospi.c @@ -0,0 +1,152 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * cospi(x) computes cos(pi*x) without multiplication by pi (almost). First, + * note that cospi(-x) = cospi(x), so the algorithm considers only |x|. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, cospi(x) = 1 with FE_INEXACT raised where a sloppy + * threshold is used. The threshold is |x| < 0x1pN with N = -(P/2+M). + * P is the precision of the floating-point type and M = 2 to 4. + * + * 2. For |x| < 1, argument reduction is not required and sinpi(x) is + * computed by calling a kernel that leverages the kernels for sin(x) + * ans cos(x). See k_sinpi.c and k_cospi.c for details. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * cospi(x) = cos(pi*(j0+r)) + * = cos(pi*j0) * cos(pi*r) - sin(pi*j0) * sin(pi*r) + * = cos(pi*j0) * cos(pi*r) + * = +-cospi(r) + * + * If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1. + * cospi(r) is then computed via an appropriate kernel. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and cospi(x) = 1. + * + * 5. Special cases: + * + * cospi(+-0) = 1. + * cospi(n.5) = 0 for n an integer. + * cospi(+-inf) = nan. Raises the "invalid" floating-point exception. + * cospi(nan) = nan. Raises the "invalid" floating-point exception. + */ + +#include +#include "openlibm_math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +#include "k_cospi.h" +#include "k_sinpi.h" + +volatile static const double vzero = 0; + +double +cospi(double x) +{ + double ax, c; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fd00000) { /* |x| < 0.25 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if ((int)ax == 0) + return (1); + } + return (__kernel_cospi(ax)); + } + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + c = __kernel_sinpi(0.5 - ax); + else if (ix < 0x3fe80000){ /* |x| < 0.75 */ + if (ax == 0.5) + return (0); + c = -__kernel_sinpi(ax - 0.5); + } else + c = -__kernel_cospi(1 - ax); + return (c); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~((uint32_t)0xffffffff >> (j0 - 20)); + } + INSERT_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3fd00000) /* |x| < 0.25 */ + c = ix == 0 ? 1 : __kernel_cospi(ax); + else + c = __kernel_sinpi(0.5 - ax); + } else { + if (ix < 0x3fe80000) { /* |x| < 0.75 */ + if (ax == 0.5) + return (0); + c = -__kernel_sinpi(ax - 0.5); + } else + c = -__kernel_cospi(1 - ax); + } + + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + return (j0 & 1 ? -c : c); + } + + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an even integer, so return 1. + */ + return (1); +} + +#if LDBL_MANT_DIG == 53 +openlibm_weak_reference(cospi, cospil); +#endif diff --git a/src/s_cospif.c b/src/s_cospif.c new file mode 100644 index 00000000..3e1a2b59 --- /dev/null +++ b/src/s_cospif.c @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_cospi.c for implementation details. + */ +#define INLINE_KERNEL_SINDF +#define INLINE_KERNEL_COSDF + +#include "openlibm_math.h" +#include "math_private.h" +#include "k_cosf.c" +#include "k_sinf.c" + +#define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x))) +#define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x))) + +volatile static const float vzero = 0; + +float +cospif(float x) +{ + float ax, c; + uint32_t ix, j0; + + GET_FLOAT_WORD(ix, x); + ix = ix & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3e800000) { /* |x| < 0.25 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + /* Raise inexact iff != 0. */ + if ((int)ax == 0) + return (1); + } + return (__kernel_cospif(ax)); + } + + if (ix < 0x3f000000) /* |x| < 0.5 */ + c = __kernel_sinpif(0.5F - ax); + else if (ix < 0x3f400000) { /* |x| < 0.75 */ + if (ix == 0x3f000000) + return (0); + c = -__kernel_sinpif(ax - 0.5F); + } else + c = -__kernel_cospif(1 - ax); + return (c); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x3e800000) /* |x| < 0.25 */ + c = ix == 0 ? 1 : __kernel_cospif(ax); + else + c = __kernel_sinpif(0.5F - ax); + } else { + if (ix < 0x3f400000) { /* |x| < 0.75 */ + if (ix == 0x3f000000) + return (0); + c = -__kernel_sinpif(ax - 0.5F); + } else + c = -__kernel_cospif(1 - ax); + } + + j0 = (uint32_t)x; + return (j0 & 1 ? -c : c); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an even integer, so return 1. + */ + return (1); +} diff --git a/src/s_fma.c b/src/s_fma.c index 63e529e5..8d1e7050 100644 --- a/src/s_fma.c +++ b/src/s_fma.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * * Copyright (c) 2005-2011 David Schultz * All rights reserved. * @@ -25,7 +27,7 @@ */ #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/s_fma.c,v 1.8 2011/10/21 06:30:43 das Exp $"); +//__FBSDID("$FreeBSD$"); #include #include @@ -33,6 +35,13 @@ #include "math_private.h" +#ifdef USE_BUILTIN_FMA +double +fma(double x, double y, double z) +{ + return (__builtin_fma(x, y, z)); +} +#else /* * A struct dd represents a floating-point number with twice the precision * of a double. We maintain the invariant that "hi" stores the 53 high-order @@ -75,7 +84,7 @@ static inline double add_adjusted(double a, double b) { struct dd sum; - u_int64_t hibits, lobits; + uint64_t hibits, lobits; sum = dd_add(a, b); if (sum.lo != 0) { @@ -99,7 +108,7 @@ static inline double add_and_denormalize(double a, double b, int scale) { struct dd sum; - u_int64_t hibits, lobits; + uint64_t hibits, lobits; int bits_lost; sum = dd_add(a, b); @@ -174,7 +183,7 @@ dd_mul(double a, double b) * Hardware instructions should be used on architectures that support it, * since this implementation will likely be several times slower. */ -OLM_DLLEXPORT double +double fma(double x, double y, double z) { double xs, ys, zs, adj; @@ -216,17 +225,17 @@ fma(double x, double y, double z) case FE_TONEAREST: return (z); case FE_TOWARDZERO: - if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0)) + if (x > 0.0 ^ y < 0.0 ^ z < 0.0) return (z); else return (nextafter(z, 0)); case FE_DOWNWARD: - if ((x > 0.0) ^ (y < 0.0)) + if (x > 0.0 ^ y < 0.0) return (z); else return (nextafter(z, -INFINITY)); default: /* FE_UPWARD */ - if ((x > 0.0) ^ (y < 0.0)) + if (x > 0.0 ^ y < 0.0) return (nextafter(z, INFINITY)); else return (z); @@ -238,6 +247,8 @@ fma(double x, double y, double z) zs = copysign(DBL_MIN, zs); fesetround(FE_TONEAREST); + /* work around clang bug 8100 */ + volatile double vxs = xs; /* * Basic approach for round-to-nearest: @@ -247,7 +258,7 @@ fma(double x, double y, double z) * adj = xy.lo + r.lo (inexact; low bit is sticky) * result = r.hi + adj (correctly rounded) */ - xy = dd_mul(xs, ys); + xy = dd_mul(vxs, ys); r = dd_add(xy.hi, zs); spread = ex + ey; @@ -268,7 +279,9 @@ fma(double x, double y, double z) * rounding modes. */ fesetround(oround); - adj = r.lo + xy.lo; + /* work around clang bug 8100 */ + volatile double vrlo = r.lo; + adj = vrlo + xy.lo; return (ldexp(r.hi + adj, spread)); } @@ -278,6 +291,7 @@ fma(double x, double y, double z) else return (add_and_denormalize(r.hi, adj, spread)); } +#endif /* !USE_BUILTIN_FMA */ #if (LDBL_MANT_DIG == 53) openlibm_weak_reference(fma, fmal); diff --git a/src/s_fmaf.c b/src/s_fmaf.c index b3c8efb5..fc55d195 100644 --- a/src/s_fmaf.c +++ b/src/s_fmaf.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * * Copyright (c) 2005-2011 David Schultz * All rights reserved. * @@ -25,11 +27,10 @@ */ #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/s_fmaf.c,v 1.3 2011/10/15 04:16:58 das Exp $"); #include -#include +#include "math.h" #include "math_private.h" /* @@ -43,7 +44,7 @@ OLM_DLLEXPORT float fmaf(float x, float y, float z) { double xy, result; - u_int32_t hr, lr; + uint32_t hr, lr; xy = (double)x * y; result = xy + z; diff --git a/src/s_fmal.c b/src/s_fmal.c index 2fe30c63..76e12a21 100644 --- a/src/s_fmal.c +++ b/src/s_fmal.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * * Copyright (c) 2005-2011 David Schultz * All rights reserved. * @@ -25,14 +27,13 @@ */ #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/s_fmal.c,v 1.7 2011/10/21 06:30:43 das Exp $"); +//__FBSDID("$FreeBSD$"); #include #include #include #include "fpmath.h" -#include "math_private.h" /* * A struct dd represents a floating-point number with twice the precision @@ -163,7 +164,7 @@ dd_mul(long double a, long double b) * Dekker, T. A Floating-Point Technique for Extending the * Available Precision. Numer. Math. 18, 224-242 (1971). */ -OLM_DLLEXPORT long double +long double fmal(long double x, long double y, long double z) { long double xs, ys, zs, adj; @@ -205,17 +206,17 @@ fmal(long double x, long double y, long double z) case FE_TONEAREST: return (z); case FE_TOWARDZERO: - if ((x > 0.0) ^ (y < 0.0) ^ (z < 0.0)) + if (x > 0.0 ^ y < 0.0 ^ z < 0.0) return (z); else return (nextafterl(z, 0)); case FE_DOWNWARD: - if ((x > 0.0) ^ (y < 0.0)) + if (x > 0.0 ^ y < 0.0) return (z); else return (nextafterl(z, -INFINITY)); default: /* FE_UPWARD */ - if ((x > 0.0) ^ (y < 0.0)) + if (x > 0.0 ^ y < 0.0) return (nextafterl(z, INFINITY)); else return (z); @@ -227,6 +228,8 @@ fmal(long double x, long double y, long double z) zs = copysignl(LDBL_MIN, zs); fesetround(FE_TONEAREST); + /* work around clang bug 8100 */ + volatile long double vxs = xs; /* * Basic approach for round-to-nearest: @@ -236,7 +239,7 @@ fmal(long double x, long double y, long double z) * adj = xy.lo + r.lo (inexact; low bit is sticky) * result = r.hi + adj (correctly rounded) */ - xy = dd_mul(xs, ys); + xy = dd_mul(vxs, ys); r = dd_add(xy.hi, zs); spread = ex + ey; @@ -257,7 +260,9 @@ fmal(long double x, long double y, long double z) * rounding modes. */ fesetround(oround); - adj = r.lo + xy.lo; + /* work around clang bug 8100 */ + volatile long double vrlo = r.lo; + adj = vrlo + xy.lo; return (ldexpl(r.hi + adj, spread)); } diff --git a/src/s_scalbln.c b/src/s_scalbln.c index 8239db56..7d3d8514 100644 --- a/src/s_scalbln.c +++ b/src/s_scalbln.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * * Copyright (c) 2004 David Schultz * All rights reserved. * @@ -25,56 +27,30 @@ */ #include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/s_scalbln.c,v 1.2 2005/03/07 04:57:50 das Exp $"); +//__FBSDID("$FreeBSD$"); -#include -#include +#include "openlibm_math.h" -#include "math_private.h" +#define NMAX 65536 +#define NMIN -65536 -OLM_DLLEXPORT double -scalbln (double x, long n) +double +scalbln(double x, long n) { - int in; - in = (int)n; - if (in != n) { - if (n > 0) - in = INT_MAX; - else - in = INT_MIN; - } - return (scalbn(x, in)); + return (scalbn(x, (n > NMAX) ? NMAX : (n < NMIN) ? NMIN : (int)n)); } -OLM_DLLEXPORT float -scalblnf (float x, long n) +float +scalblnf(float x, long n) { - int in; - in = (int)n; - if (in != n) { - if (n > 0) - in = INT_MAX; - else - in = INT_MIN; - } - return (scalbnf(x, in)); + return (scalbnf(x, (n > NMAX) ? NMAX : (n < NMIN) ? NMIN : (int)n)); } -#ifdef OLM_LONG_DOUBLE -OLM_DLLEXPORT long double -scalblnl (long double x, long n) +long double +scalblnl(long double x, long n) { - int in; - in = (int)n; - if (in != n) { - if (n > 0) - in = INT_MAX; - else - in = INT_MIN; - } - return (scalbnl(x, (int)n)); + return (scalbnl(x, (n > NMAX) ? NMAX : (n < NMIN) ? NMIN : (int)n)); } -#endif diff --git a/src/s_scalbn.c b/src/s_scalbn.c index dc3a0cfd..64b086b7 100644 --- a/src/s_scalbn.c +++ b/src/s_scalbn.c @@ -1,66 +1,47 @@ -/* @(#)s_scalbn.c 5.1 93/09/24 */ /* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * Copyright (c) 2005-2020 Rich Felker, et al. * - * 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. - * ==================================================== - */ - -/* - * scalbn (double x, int n) - * scalbn(x,n) returns x* 2**n computed by exponent - * manipulation rather than by actually performing an - * exponentiation or a multiplication. + * SPDX-License-Identifier: MIT + * + * Please see https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT + * for all contributors to musl. */ - -#include "cdefs-compat.h" - #include -#include - -#include "math_private.h" +#include "openlibm_math.h" +#include -static const double -two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */ -twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */ -huge = 1.0e+300, -tiny = 1.0e-300; - -OLM_DLLEXPORT double -scalbn (double x, int n) +double scalbn(double x, int n) { - int32_t k,hx,lx; - EXTRACT_WORDS(hx,lx,x); - k = (hx&0x7ff00000)>>20; /* extract exponent */ - if (k==0) { /* 0 or subnormal x */ - if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ - x *= two54; - GET_HIGH_WORD(hx,x); - k = ((hx&0x7ff00000)>>20) - 54; - if (n< -50000) return tiny*x; /*underflow*/ - } - if (k==0x7ff) return x+x; /* NaN or Inf */ - k = k+n; - if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */ - if (k > 0) /* normal result */ - {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} - if (k <= -54) { - if (n > 50000) /* in case integer overflow in n+k */ - return huge*copysign(huge,x); /*overflow*/ - else return tiny*copysign(tiny,x); /*underflow*/ - } - k += 54; /* subnormal result */ - SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); - return x*twom54; + union {double f; uint64_t i;} u; + double_t y = x; + + if (n > 1023) { + y *= 0x1p1023; + n -= 1023; + if (n > 1023) { + y *= 0x1p1023; + n -= 1023; + if (n > 1023) + n = 1023; + } + } else if (n < -1022) { + /* make sure final n < -53 to avoid double + rounding in the subnormal range */ + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; + if (n < -1022) { + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; + if (n < -1022) + n = -1022; + } + } + u.i = (uint64_t)(0x3ff+n)<<52; + x = y * u.f; + return x; } -#if (LDBL_MANT_DIG == 53) +#if (LDBL_MANT_DIG == 53) && !defined(scalbn) openlibm_weak_reference(scalbn, ldexpl); openlibm_weak_reference(scalbn, scalbnl); #endif - -openlibm_strong_reference(scalbn, ldexp); diff --git a/src/s_scalbnf.c b/src/s_scalbnf.c index 930ab8fa..dbd06d14 100644 --- a/src/s_scalbnf.c +++ b/src/s_scalbnf.c @@ -1,57 +1,41 @@ -/* s_scalbnf.c -- float version of s_scalbn.c. - * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. - */ - /* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * Copyright (c) 2005-2020 Rich Felker, et al. * - * 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. - * ==================================================== + * SPDX-License-Identifier: MIT + * + * Please see https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT + * for all contributors to musl. */ +#include "openlibm_math.h" +#include - -#include "cdefs-compat.h" - -#include - -#include "math_private.h" - -static const float -two25 = 3.355443200e+07, /* 0x4c000000 */ -twom25 = 2.9802322388e-08, /* 0x33000000 */ -huge = 1.0e+30, -tiny = 1.0e-30; - -OLM_DLLEXPORT float -scalbnf (float x, int n) +float scalbnf(float x, int n) { - int32_t k,ix; - GET_FLOAT_WORD(ix,x); - k = (ix&0x7f800000)>>23; /* extract exponent */ - if (k==0) { /* 0 or subnormal x */ - if ((ix&0x7fffffff)==0) return x; /* +-0 */ - x *= two25; - GET_FLOAT_WORD(ix,x); - k = ((ix&0x7f800000)>>23) - 25; - if (n< -50000) return tiny*x; /*underflow*/ - } - if (k==0xff) return x+x; /* NaN or Inf */ - k = k+n; - if (k > 0xfe) return huge*copysignf(huge,x); /* overflow */ - if (k > 0) /* normal result */ - {SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;} - if (k <= -25) { - if (n > 50000) /* in case integer overflow in n+k */ - return huge*copysignf(huge,x); /*overflow*/ - else return tiny*copysignf(tiny,x); /*underflow*/ - } - k += 25; /* subnormal result */ - SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); - return x*twom25; + union {float f; uint32_t i;} u; + float_t y = x; + + if (n > 127) { + y *= 0x1p127f; + n -= 127; + if (n > 127) { + y *= 0x1p127f; + n -= 127; + if (n > 127) + n = 127; + } + } else if (n < -126) { + y *= 0x1p-126f * 0x1p24f; + n += 126 - 24; + if (n < -126) { + y *= 0x1p-126f * 0x1p24f; + n += 126 - 24; + if (n < -126) + n = -126; + } + } + u.i = (uint32_t)(0x7f+n)<<23; + x = y * u.f; + return x; } openlibm_strong_reference(scalbnf, ldexpf); diff --git a/src/s_scalbnl.c b/src/s_scalbnl.c index 7732944f..4d4003c4 100644 --- a/src/s_scalbnl.c +++ b/src/s_scalbnl.c @@ -1,70 +1,48 @@ -/* @(#)s_scalbn.c 5.1 93/09/24 */ /* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * Copyright (c) 2005-2020 Rich Felker, et al. * - * 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. - * ==================================================== + * SPDX-License-Identifier: MIT + * + * Please see https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT + * for all contributors to musl. */ - +#include "openlibm_math.h" +#include +#include "math_private.h" +#include "fpmath.h" /* * scalbnl (long double x, int n) * scalbnl(x,n) returns x* 2**n computed by exponent * manipulation rather than by actually performing an * exponentiation or a multiplication. */ - -/* - * We assume that a long double has a 15-bit exponent. On systems - * where long double is the same as double, scalbnl() is an alias - * for scalbn(), so we don't use this routine. - */ - -#include "cdefs-compat.h" - -#include -#include - -#include "fpmath.h" -#include "math_private.h" - -#if LDBL_MAX_EXP != 0x4000 -#error "Unsupported long double format" -#endif - -static const long double -huge = 0x1p16000L, -tiny = 0x1p-16000L; - -OLM_DLLEXPORT long double -scalbnl (long double x, int n) +#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 +long double scalbnl(long double x, int n) { union IEEEl2bits u; - int k; - u.e = x; - k = u.bits.exp; /* extract exponent */ - if (k==0) { /* 0 or subnormal x */ - if ((u.bits.manh|u.bits.manl)==0) return x; /* +-0 */ - u.e *= 0x1p+128; - k = u.bits.exp - 128; - if (n< -50000) return tiny*x; /*underflow*/ - } - if (k==0x7fff) return x+x; /* NaN or Inf */ - k = k+n; - if (k >= 0x7fff) return huge*copysignl(huge,x); /* overflow */ - if (k > 0) /* normal result */ - {u.bits.exp = k; return u.e;} - if (k <= -128) { - if (n > 50000) /* in case integer overflow in n+k */ - return huge*copysign(huge,x); /*overflow*/ - else return tiny*copysign(tiny,x); /*underflow*/ - } - k += 128; /* subnormal result */ - u.bits.exp = k; - return u.e*0x1p-128; -} + if (n > 16383) { + x *= 0x1p16383L; + n -= 16383; + if (n > 16383) { + x *= 0x1p16383L; + n -= 16383; + if (n > 16383) + n = 16383; + } + } else if (n < -16382) { + x *= 0x1p-16382L * 0x1p113L; + n += 16382 - 113; + if (n < -16382) { + x *= 0x1p-16382L * 0x1p113L; + n += 16382 - 113; + if (n < -16382) + n = -16382; + } + } + u.e = 1.0; + u.xbits.expsign = 0x3fff + n; + return x * u.e; +} openlibm_strong_reference(scalbnl, ldexpl); +#endif diff --git a/src/s_sinpi.c b/src/s_sinpi.c new file mode 100644 index 00000000..b5652fbd --- /dev/null +++ b/src/s_sinpi.c @@ -0,0 +1,169 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * sinpi(x) computes sin(pi*x) without multiplication by pi (almost). First, + * note that sinpi(-x) = -sinpi(x), so the algorithm considers only |x| and + * includes reflection symmetry by considering the sign of x on output. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, sinpi(x) = pi * x where a sloppy threshold is used. The + * threshold is |x| < 0x1pN with N = -(P/2+M). P is the precision of the + * floating-point type and M = 2 to 4. To achieve high accuracy, pi is + * decomposed into high and low parts with the high part containing a + * number of trailing zero bits. x is also split into high and low parts. + * + * 2. For |x| < 1, argument reduction is not required and sinpi(x) is + * computed by calling a kernel that leverages the kernels for sin(x) + * ans cos(x). See k_sinpi.c and k_cospi.c for details. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * sinpi(x) = sin(pi*(j0+r)) + * = sin(pi*j0) * cos(pi*r) + cos(pi*j0) * sin(pi*r) + * = cos(pi*j0) * sin(pi*r) + * = +-sinpi(r) + * + * If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1. + * sinpi(r) is then computed via an appropriate kernel. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and sinpi(x) = copysign(0,x). + * + * 5. Special cases: + * + * sinpi(+-0) = +-0 + * sinpi(+-n) = +-0, for positive integers n. + * sinpi(+-inf) = nan. Raises the "invalid" floating-point exception. + * sinpi(nan) = nan. Raises the "invalid" floating-point exception. + */ + +#include +#include "openlibm_math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo =-2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +#include "k_cospi.h" +#include "k_sinpi.h" + +volatile static const double vzero = 0; + +double +sinpi(double x) +{ + double ax, hi, lo, s; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fd00000) { /* |x| < 0.25 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if (x == 0) + return (x); + /* + * To avoid issues with subnormal values, + * scale the computation and rescale on + * return. + */ + INSERT_WORDS(hi, hx, 0); + hi *= 0x1p53; + lo = x * 0x1p53 - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (s * 0x1p-53); + } + + s = __kernel_sinpi(ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + s = __kernel_cospi(0.5 - ax); + else if (ix < 0x3fe80000) /* |x| < 0.75 */ + s = __kernel_cospi(ax - 0.5); + else + s = __kernel_sinpi(1 - ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~((uint32_t)0xffffffff >> (j0 - 20)); + } + INSERT_WORDS(x, ix, lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + if (ix == 0) + s = 0; + else { + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3fd00000) /* |x| < 0.25 */ + s = __kernel_sinpi(ax); + else + s = __kernel_cospi(0.5 - ax); + } else { + if (ix < 0x3fe80000) /* |x| < 0.75 */ + s = __kernel_cospi(ax - 0.5); + else + s = __kernel_sinpi(1 - ax); + } + + if (j0 > 30) + x -= 0x1p30; + j0 = (uint32_t)x; + if (j0 & 1) s = -s; + } + + return ((hx & 0x80000000) ? -s : s); + } + + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an integer, so return +-0. + */ + return (copysign(0, x)); +} + +#if LDBL_MANT_DIG == 53 +openlibm_weak_reference(sinpi, sinpil); +#endif diff --git a/src/s_sinpif.c b/src/s_sinpif.c new file mode 100644 index 00000000..063410e9 --- /dev/null +++ b/src/s_sinpif.c @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_sinpi.c for implementation details. + */ + +#define INLINE_KERNEL_SINDF +#define INLINE_KERNEL_COSDF + +#include "openlibm_math.h" +#include "math_private.h" +#include "k_cosf.c" +#include "k_sinf.c" + +#define __kernel_cospif(x) (__kernel_cosdf(M_PI * (x))) +#define __kernel_sinpif(x) (__kernel_sindf(M_PI * (x))) + +static const float +pi_hi = 3.14160156e+00F, /* 0x40491000 */ +pi_lo = -8.90890988e-06F; /* 0xb715777a */ + +volatile static const float vzero = 0; + +float +sinpif(float x) +{ + float ax, hi, lo, s; + uint32_t hx, ix, j0; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3e800000) { /* |x| < 0.25 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + if (x == 0) + return (x); + SET_FLOAT_WORD(hi, hx & 0xffff0000); + hi *= 0x1p23F; + lo = x * 0x1p23F - hi; + s = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (s * 0x1p-23F); + } + + s = __kernel_sinpif(ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x3f000000) /* |x| < 0.5 */ + s = __kernel_cospif(0.5F - ax); + else if (ix < 0x3f400000) /* |x| < 0.75 */ + s = __kernel_cospif(ax - 0.5F); + else + s = __kernel_sinpif(1 - ax); + return ((hx & 0x80000000) ? -s : s); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix == 0) + s = 0; + else { + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x3e800000) /* |x| < 0.25 */ + s = __kernel_sinpif(ax); + else + s = __kernel_cospif(0.5F - ax); + } else { + if (ix < 0x3f400000) /* |x| < 0.75 */ + s = __kernel_cospif(ax - 0.5F); + else + s = __kernel_sinpif(1 - ax); + } + + j0 = (uint32_t)x; + s = (j0 & 1) ? -s : s; + } + return ((hx & 0x80000000) ? -s : s); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an integer, so return +-0. + */ + return (copysignf(0, x)); +} diff --git a/src/s_tanpi.c b/src/s_tanpi.c new file mode 100644 index 00000000..9fb67ab7 --- /dev/null +++ b/src/s_tanpi.c @@ -0,0 +1,177 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * tanpi(x) computes tan(pi*x) without multiplication by pi (almost). First, + * note that tanpi(-x) = -tanpi(x), so the algorithm considers only |x| and + * includes reflection symmetry by considering the sign of x on output. The + * method used depends on the magnitude of x. + * + * 1. For small |x|, tanpi(x) = pi * x where a sloppy threshold is used. The + * threshold is |x| < 0x1pN with N = -(P/2+M). P is the precision of the + * floating-point type and M = 2 to 4. To achieve high accuracy, pi is + * decomposed into high and low parts with the high part containing a + * number of trailing zero bits. x is also split into high and low parts. + * + * 2. For |x| < 1, argument reduction is not required and tanpi(x) is + * computed by a direct call to a kernel, which uses the kernel for + * tan(x). See below. + * + * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where + * |x| = j0 + r with j0 an integer and the remainder r satisfies + * 0 <= r < 1. With the given domain, a simplified inline floor(x) + * is used. Also, note the following identity + * + * tan(pi*j0) + tan(pi*r) + * tanpi(x) = tan(pi*(j0+r)) = ---------------------------- = tanpi(r) + * 1 - tan(pi*j0) * tan(pi*r) + * + * So, after argument reduction, the kernel is again invoked. + * + * 4. For |x| >= 0x1p(P-1), |x| is integral and tanpi(x) = copysign(0,x). + * + * 5. Special cases: + * + * tanpi(+-0) = +-0 + * tanpi(+-n) = +-0, for positive integers n. + * tanpi(+-n+1/4) = +-1, for positive integers n. + * tanpi(+-n+1/2) = NaN, for positive integers n. + * tanpi(+-inf) = NaN. Raises the "invalid" floating-point exception. + * tanpi(nan) = NaN. Raises the "invalid" floating-point exception. + */ + +#include +#include "openlibm_math.h" +#include "math_private.h" + +static const double +pi_hi = 3.1415926814079285e+00, /* 0x400921fb 0x58000000 */ +pi_lo = -2.7818135228334233e-08; /* 0xbe5dde97 0x3dcb3b3a */ + +/* + * The kernel for tanpi(x) multiplies x by an 80-bit approximation of + * pi, where the hi and lo parts are used with with kernel for tan(x). + */ +static inline double +__kernel_tanpi(double x) +{ + double hi, lo, t; + + if (x < 0.25) { + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = __kernel_tan(hi, lo, 1); + } else if (x > 0.25) { + x = 0.5 - x; + hi = (float)x; + lo = x - hi; + lo = lo * (pi_lo + pi_hi) + hi * pi_lo; + hi *= pi_hi; + _2sumF(hi, lo); + t = - __kernel_tan(hi, lo, -1); + } else + t = 1; + + return (t); +} + +volatile static const double vzero = 0; + +double +tanpi(double x) +{ + double ax, hi, lo, t; + uint32_t hx, ix, j0, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + INSERT_WORDS(ax, ix, lx); + + if (ix < 0x3ff00000) { /* |x| < 1 */ + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix < 0x3e200000) { /* |x| < 0x1p-29 */ + if (x == 0) + return (x); + /* + * To avoid issues with subnormal values, + * scale the computation and rescale on + * return. + */ + INSERT_WORDS(hi, hx, 0); + hi *= 0x1p53; + lo = x * 0x1p53 - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (t * 0x1p-53); + } + t = __kernel_tanpi(ax); + } else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpi(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + if (ix < 0x43300000) { /* 1 <= |x| < 0x1p52 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + ix &= ~(0x000fffff >> j0); + lx = 0; + } else { + lx &= ~(((uint32_t)(0xffffffff)) >> (j0 - 20)); + } + INSERT_WORDS(x,ix,lx); + + ax -= x; + EXTRACT_WORDS(ix, lx, ax); + + if (ix < 0x3fe00000) /* |x| < 0.5 */ + t = ax == 0 ? 0 : __kernel_tanpi(ax); + else if (ax == 0.5) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpi(1 - ax); + + return ((hx & 0x80000000) ? -t : t); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p52 is always an integer, so return +-0. + */ + return (copysign(0, x)); +} + +#if LDBL_MANT_DIG == 53 +openlibm_weak_reference(tanpi, tanpil); +#endif diff --git a/src/s_tanpif.c b/src/s_tanpif.c new file mode 100644 index 00000000..519cca14 --- /dev/null +++ b/src/s_tanpif.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2017 Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * See ../src/s_tanpi.c for implementation details. + */ + +#define INLINE_KERNEL_TANDF + +#include "openlibm_math.h" +#include "math_private.h" +#include "k_tanf.c" + +static const float +pi_hi = 3.14160156e+00F, /* 0x40491000 */ +pi_lo = -8.90890988e-06F; /* 0xb715777a */ + +static inline float +__kernel_tanpif(float x) +{ + float t; + + if (x < 0.25F) + t = __kernel_tandf(M_PI * x, 1); + else if (x > 0.25F) + t = -__kernel_tandf(M_PI * (0.5 - x), -1); + else + t = 1; + + return (t); +} + +volatile static const float vzero = 0; + +float +tanpif(float x) +{ + float ax, hi, lo, t; + uint32_t hx, ix, j0; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + SET_FLOAT_WORD(ax, ix); + + if (ix < 0x3f800000) { /* |x| < 1 */ + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix < 0x38800000) { /* |x| < 0x1p-14 */ + if (ix == 0) + return (x); + SET_FLOAT_WORD(hi, hx & 0xffff0000); + hi *= 0x1p23F; + lo = x * 0x1p23F - hi; + t = (pi_lo + pi_hi) * lo + pi_lo * hi + + pi_hi * hi; + return (t * 0x1p-23F); + } + t = __kernel_tanpif(ax); + } else if (ix == 0x3f000000) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpif(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + if (ix < 0x4b000000) { /* 1 <= |x| < 0x1p23 */ + /* Determine integer part of ax. */ + j0 = ((ix >> 23) & 0xff) - 0x7f; + ix &= ~(0x007fffff >> j0); + SET_FLOAT_WORD(x, ix); + + ax -= x; + GET_FLOAT_WORD(ix, ax); + + if (ix < 0x3f000000) /* |x| < 0.5 */ + t = ix == 0 ? 0 : __kernel_tanpif(ax); + else if (ix == 0x3f000000) + return ((ax - ax) / (ax - ax)); + else + t = - __kernel_tanpif(1 - ax); + return ((hx & 0x80000000) ? -t : t); + } + + /* x = +-inf or nan. */ + if (ix >= 0x7f800000) + return (vzero / vzero); + + /* + * |x| >= 0x1p23 is always an integer, so return +-0. + */ + return (copysignf(0, x)); +} diff --git a/src/s_tgammaf.c b/src/s_tgammaf.c index fbfa3fea..1fd46e80 100644 --- a/src/s_tgammaf.c +++ b/src/s_tgammaf.c @@ -1,4 +1,6 @@ /*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * * Copyright (c) 2008 David Schultz * All rights reserved. * @@ -24,12 +26,10 @@ * SUCH DAMAGE. */ -#include "cdefs-compat.h" -//__FBSDID("$FreeBSD: src/lib/msun/src/s_tgammaf.c,v 1.1 2008/02/18 17:27:10 das Exp $"); - -#include +#include +__FBSDID("$FreeBSD$"); -#include "math_private.h" +#include /* * We simply call tgamma() rather than bloating the math library with @@ -37,7 +37,7 @@ * essentially useless, since the function is superexponential and * floats have very limited range. */ -OLM_DLLEXPORT float +float tgammaf(float x) {