The branch main has been updated by kib:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=dce5f3abed7181cc533ca5ed3de44517775e78dd

commit dce5f3abed7181cc533ca5ed3de44517775e78dd
Author:     Steve Kargl <[email protected]>
AuthorDate: 2021-10-25 13:13:52 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2021-10-25 23:50:20 +0000

    [LIBM] implementations of sinpi[fl], cospi[fl], and tanpi[fl]
    
    Both IEEE-754 2008 and ISO/IEC TS 18661-4 define the half-cycle
    trignometric functions cospi, sinpi, and tanpi.  The attached
    patch implements cospi[fl], sinpi[fl], and tanpi[fl].  Limited
    testing on the cospi and sinpi reveal a max ULP less than 0.89;
    while tanpi is more problematic with a max ULP less than 2.01
    in the interval [0,0.5].  The algorithms used in these functions
    are documented in {ks}_cospi.c, {ks}_sinpi.c, and s_tanpi.c.
    
    Note.  I no longer have access to a system with ld128 and
    adequate support to compile and test the ld128 implementations
    of these functions.  Given the almost complete lack of input from
    others on improvements to libm, I doubt that anyone cares.  If
    someone does care, the ld128 files contain a number of FIXME comments,
    and in particular, while the polynomial coefficients are given
    I did not update the polynomial algorithms to properly use the
    coefficients.
    
    PR:     218514
    MFC after:      2 weeks
---
 lib/msun/Makefile         |  14 +++-
 lib/msun/Symbol.map       |  13 ++++
 lib/msun/ld128/k_cospil.h |  42 +++++++++++
 lib/msun/ld128/k_sinpil.h |  42 +++++++++++
 lib/msun/ld128/s_cospil.c | 109 ++++++++++++++++++++++++++++
 lib/msun/ld128/s_sinpil.c | 118 +++++++++++++++++++++++++++++++
 lib/msun/ld128/s_tanpil.c | 120 +++++++++++++++++++++++++++++++
 lib/msun/ld80/k_cospil.h  |  42 +++++++++++
 lib/msun/ld80/k_sinpil.h  |  42 +++++++++++
 lib/msun/ld80/s_cospil.c  | 129 +++++++++++++++++++++++++++++++++
 lib/msun/ld80/s_sinpil.c  | 140 ++++++++++++++++++++++++++++++++++++
 lib/msun/ld80/s_tanpil.c  | 139 ++++++++++++++++++++++++++++++++++++
 lib/msun/man/cospi.3      | 111 +++++++++++++++++++++++++++++
 lib/msun/man/sinpi.3      | 102 +++++++++++++++++++++++++++
 lib/msun/man/tanpi.3      | 106 ++++++++++++++++++++++++++++
 lib/msun/src/k_cospi.h    |  44 ++++++++++++
 lib/msun/src/k_sinpi.h    |  43 +++++++++++
 lib/msun/src/math.h       |   9 +++
 lib/msun/src/s_cospi.c    | 151 +++++++++++++++++++++++++++++++++++++++
 lib/msun/src/s_cospif.c   | 109 ++++++++++++++++++++++++++++
 lib/msun/src/s_sinpi.c    | 168 +++++++++++++++++++++++++++++++++++++++++++
 lib/msun/src/s_sinpif.c   | 122 ++++++++++++++++++++++++++++++++
 lib/msun/src/s_tanpi.c    | 176 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/msun/src/s_tanpif.c   | 114 ++++++++++++++++++++++++++++++
 24 files changed, 2203 insertions(+), 2 deletions(-)

diff --git a/lib/msun/Makefile b/lib/msun/Makefile
index 7107aad56aa7..dcee5572f949 100644
--- a/lib/msun/Makefile
+++ b/lib/msun/Makefile
@@ -126,6 +126,12 @@ COMMON_SRCS+=      catrigl.c \
 # See also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=130067
 .if ${COMPILER_TYPE} == "gcc"
 CFLAGS.e_powl.c+= -Wno-error=overflow
+
+# IEEE-754 2008 and ISO/IEC TS 18661-4 half-cycle trignometric functions
+COMMON_SRCS+=  s_cospi.c s_cospif.c s_cospil.c \
+       s_sinpi.c s_sinpif.c s_sinpil.c \
+       s_tanpi.c s_tanpif.c s_tanpil.c
+
 .endif
 .endif
 
@@ -154,7 +160,8 @@ INCS+=      fenv.h math.h
 
 MAN=   acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 atanh.3 \
        ceil.3 cacos.3 ccos.3 ccosh.3 cexp.3 \
-       cimag.3 clog.3 copysign.3 cos.3 cosh.3 cpow.3 csqrt.3 erf.3 \
+       cimag.3 clog.3 copysign.3 cos.3 cosh.3 cospi.3 \
+       cpow.3 csqrt.3 erf.3 \
        exp.3 fabs.3 fdim.3 \
        feclearexcept.3 feenableexcept.3 fegetenv.3 \
        fegetround.3 fenv.3 floor.3 \
@@ -162,7 +169,7 @@ MAN=        acos.3 acosh.3 asin.3 asinh.3 atan.3 atan2.3 
atanh.3 \
        lgamma.3 log.3 lrint.3 lround.3 math.3 nan.3 \
        nextafter.3 remainder.3 rint.3 \
        round.3 scalbn.3 signbit.3 sin.3 sincos.3 \
-       sinh.3 sqrt.3 tan.3 tanh.3 trunc.3 \
+       sinh.3 sinpi.3 sqrt.3 tan.3 tanh.3 tanpi.3 trunc.3 \
        complex.3
 
 MLINKS+=acos.3 acosf.3 acos.3 acosl.3
@@ -192,6 +199,7 @@ MLINKS+=clog.3 clogf.3 clog.3 clogl.3
 MLINKS+=copysign.3 copysignf.3 copysign.3 copysignl.3
 MLINKS+=cos.3 cosf.3 cos.3 cosl.3
 MLINKS+=cosh.3 coshf.3 cosh.3 coshl.3
+MLINKS+=cospi.3 cospif.3 cospi.3 cospil.3
 MLINKS+=cpow.3 cpowf.3 cpow.3 cpowl.3
 MLINKS+=csqrt.3 csqrtf.3 csqrt.3 csqrtl.3
 MLINKS+=erf.3 erfc.3 erf.3 erff.3 erf.3 erfcf.3 erf.3 erfl.3 erf.3 erfcl.3
@@ -244,10 +252,12 @@ MLINKS+=scalbn.3 scalbnf.3 scalbn.3 scalbnl.3
 MLINKS+=sin.3 sinf.3 sin.3 sinl.3
 MLINKS+=sincos.3 sincosf.3 sin.3 sincosl.3
 MLINKS+=sinh.3 sinhf.3 sinh.3 sinhl.3
+MLINKS+=sinpi.3 sinpif.3 sinpi.3 sinpil.3
 MLINKS+=sqrt.3 cbrt.3 sqrt.3 cbrtf.3 sqrt.3 cbrtl.3 sqrt.3 sqrtf.3 \
        sqrt.3 sqrtl.3
 MLINKS+=tan.3 tanf.3 tan.3 tanl.3
 MLINKS+=tanh.3 tanhf.3 tanh.3 tanhl.3
+MLINKS+=tanpi.3 tanpif.3 tanpi.3 tanpil.3
 MLINKS+=trunc.3 truncf.3 trunc.3 truncl.3
 
 .include <src.opts.mk>
diff --git a/lib/msun/Symbol.map b/lib/msun/Symbol.map
index 51deded732f0..7229e7ef31fd 100644
--- a/lib/msun/Symbol.map
+++ b/lib/msun/Symbol.map
@@ -304,3 +304,16 @@ FBSD_1.5 {
        sincosf;
        sincosl;
 };
+
+/* First added in 14.0-CURRENT */
+FBSD_1.7 {
+       cospi;
+       cospif;
+       cospil;
+       sinpi;
+       sinpif;
+       sinpil;
+       tanpi;
+       tanpif;
+       tanpil;
+};
diff --git a/lib/msun/ld128/k_cospil.h b/lib/msun/ld128/k_cospil.h
new file mode 100644
index 000000000000..592f19229532
--- /dev/null
+++ b/lib/msun/ld128/k_cospil.h
@@ -0,0 +1,42 @@
+/*-
+ * 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/k_cospi.c for implementation details.
+ */
+
+static inline long double
+__kernel_cospil(long double x)
+{
+       long double hi, lo;
+
+       hi = (double)x;
+       lo = x - hi;
+       lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
+       hi *= pi_hi;
+       _2sumF(hi, lo);
+       return (__kernel_cosl(hi, lo));
+}
diff --git a/lib/msun/ld128/k_sinpil.h b/lib/msun/ld128/k_sinpil.h
new file mode 100644
index 000000000000..fa4e7d6336d7
--- /dev/null
+++ b/lib/msun/ld128/k_sinpil.h
@@ -0,0 +1,42 @@
+/*-
+ * 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/k_sinpi.c for implementation details.
+ */
+
+static inline long double
+__kernel_sinpil(long double x)
+{
+       long double hi, lo;
+
+       hi = (double)x;
+       lo = x - hi;
+       lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
+       hi *= pi_hi;
+       _2sumF(hi, lo);
+       return (__kernel_sinl(hi, lo, 1));
+}
diff --git a/lib/msun/ld128/s_cospil.c b/lib/msun/ld128/s_cospil.c
new file mode 100644
index 000000000000..b4bc50bb4d89
--- /dev/null
+++ b/lib/msun/ld128/s_cospil.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.
+ *
+ * FIXME:  This has not been compiled nor has it been tested for accuracy.
+ * FIXME:  This should use bit twiddling.
+ */
+
+#include "math.h"
+#include "math_private.h"
+
+/*
+ * pi_hi contains the leading 56 bits of a 169 bit approximation for pi.
+ */
+static const long double
+pi_hi = 3.14159265358979322702026593105983920e+00L,
+pi_lo = 1.14423774522196636802434264184180742e-17L;
+
+#include "k_cospil.h"
+#include "k_sinpil.h"
+
+volatile static const double vzero = 0;
+
+long double
+cospil(long double x)
+{
+       long double ax, c, xf;
+       uint32_t ix;
+
+       ax = fabsl(x);
+
+       if (ax < 1) {
+               if (ax < 0.25) {
+                       if (ax < 0x1p-60) {
+                               if ((int)x == 0)
+                                       return (1);
+                       }
+                       return (__kernel_cospil(ax));
+               }
+
+               if (ax < 0.5)
+                       c = __kernel_sinpil(0.5 - ax);
+               else if (ax < 0.75) {
+                       if (ax == 0.5)
+                               return (0);
+                       c = -__kernel_sinpil(ax - 0.5);
+               } else
+                       c = -__kernel_cospil(1 - ax);
+               return (c);
+       }
+
+       if (ax < 0x1p112) {
+               xf = floorl(ax);
+               ax -= xf;
+               if (x < 0.5) {
+                       if (x < 0.25)
+                               c = ax == 0 ? 1 : __kernel_cospil(ax);
+                       else
+                               c = __kernel_sinpil(0.5 - ax);
+               } else {
+                       if (x < 0.75) {
+                               if (ax == 0.5)
+                                       return (0);
+                               c = -__kernel_sinpil(ax - 0.5);
+                       } else
+                               c = -__kernel_cospil(1 - ax);
+               }
+
+               if (xf > 0x1p50)
+                       xf -= 0x1p50;
+               if (xf > 0x1p30)
+                       xf -= 0x1p30;
+               ix = (uint32_t)xf;
+               return (ix & 1 ? -c : c);
+       }
+
+       if (isinf(x) || isnan(x))
+               return (vzero / vzero);
+
+       /*
+        * |x| >= 0x1p112 is always an even integer, so return 1.
+        */
+       return (1);
+}
diff --git a/lib/msun/ld128/s_sinpil.c b/lib/msun/ld128/s_sinpil.c
new file mode 100644
index 000000000000..39eed9b007bc
--- /dev/null
+++ b/lib/msun/ld128/s_sinpil.c
@@ -0,0 +1,118 @@
+/*-
+ * 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.
+ *
+ * FIXME:  This has not been compiled nor has it been tested for accuracy.
+ * FIXME:  This should use bit twiddling.
+ */
+
+#include "math.h"
+#include "math_private.h"
+
+/*
+ * pi_hi contains the leading 56 bits of a 169 bit approximation for pi.
+ */
+static const long double
+pi_hi = 3.14159265358979322702026593105983920e+00L,
+pi_lo = 1.14423774522196636802434264184180742e-17L;
+
+#include "k_cospil.h"
+#include "k_sinpil.h"
+
+volatile static const double vzero = 0;
+
+long double
+sinpil(long double x)
+{
+       long double ax, hi, lo, s, xf, xhi, xlo;
+       uint32_t ix;
+
+       ax = fabsl(x);
+
+       if (ax < 1) {
+               if (ax < 0.25) {
+                       if (ax < 0x1p-60) {
+                               if (x == 0)
+                                       return (x);
+                               hi = (double)x;
+                               hi *= 0x1p113L;
+                               lo = x * 0x1p113L - hi;
+                               s = (pi_lo + pi_hi) * lo + pi_lo * lo +
+                                   pi_hi * hi;
+                               return (s * 0x1p-113L);
+                       }
+
+                       s = __kernel_sinpil(ax);
+                       return (copysignl(s, x));
+               }
+
+               if (ax < 0.5)
+                       s = __kernel_cospil(0.5 - ax);
+               else if (ax < 0.75)
+                       s = __kernel_cospil(ax - 0.5);
+               else
+                       s = __kernel_sinpil(1 - ax);
+               return (copysignl(s, x));
+       }
+
+       if (ax < 0x1p112) {
+               xf = floorl(ax);
+               ax -= xf;
+               if (ax == 0) {
+                       s = 0;
+               } else {
+                       if (ax < 0.5) {
+                               if (ax <= 0.25)
+                                       s = __kernel_sinpil(ax);
+                               else
+                                       s = __kernel_cospil(0.5 - ax);
+                       } else {
+                               if (ax < 0.75)
+                                       s = __kernel_cospil(ax - 0.5);
+                               else
+                                       s = __kernel_sinpil(1 - ax);
+                       }
+
+                       if (xf > 0x1p50)
+                               xf -= 0x1p50;
+                       if (xf > 0x1p30)
+                               xf -= 0x1p30;
+                       ix = (uint32_t)xf;
+                       if (ix & 1) s = -s;
+               }
+               return (copysignl(s, x));
+       }
+
+       if (isinf(x) || isnan(x))
+               return (vzero / vzero);
+
+       /*
+        * |x| >= 0x1p112 is always an integer, so return +-0.
+        */
+       return (copysignl(0, x));
+}
diff --git a/lib/msun/ld128/s_tanpil.c b/lib/msun/ld128/s_tanpil.c
new file mode 100644
index 000000000000..33a61cf3115d
--- /dev/null
+++ b/lib/msun/ld128/s_tanpil.c
@@ -0,0 +1,120 @@
+/*-
+ * 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.
+ *
+ * FIXME: This has not been compiled nor has it been tested for accuracy.
+ * FIXME: This should use bit twiddling.
+ */
+
+#include "math.h"
+#include "math_private.h"
+
+/*
+ * pi_hi contains the leading 56 bits of a 169 bit approximation for pi.
+ */
+static const long double
+pi_hi = 3.14159265358979322702026593105983920e+00L,
+pi_lo = 1.14423774522196636802434264184180742e-17L;
+
+static inline long double
+__kernel_tanpi(long double x)
+{
+       long double hi, lo, t;
+
+       if (x < 0.25) {
+               hi = (double)x;
+               lo = x - hi;
+               lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
+               hi *= pi_hi;
+               _2sumF(hi, lo);
+               t = __kernel_tanl(hi, lo, -1);
+       } else if (x > 0.25) {
+               x = 0.5 - x;
+               hi = (double)x;
+               lo = x - hi;
+               lo = lo * (pi_lo + pi_hi) + hi * pi_lo;
+               hi *= pi_hi;
+               _2sumF(hi, lo);
+               t = - __kernel_tanl(hi, lo, 1);
+       } else
+               t = 1;
+
+       return (t);
+}
+
+volatile static const double vzero = 0;
+
+long double
+tanpil(long double x)
+{
+       long double ax, hi, lo, xf;
+       uint32_t ix;
+
+       ax = fabsl(ax);
+
+       if (ax < 1) {
+               if (ax < 0.5) {
+                       if (ax < 0x1p-60) {
+                               if (x == 0)
+                                       return (x);
+                               hi = (double)x;
+                               hi *= 0x1p113L
+                               lo = x * 0x1p113L - hi;
+                               t = (pi_lo + pi_hi) * lo + pi_lo * lo +
+                                   pi_hi * hi;
+                               return (t * 0x1p-113L);
+                       }
+                       t = __kernel_tanpil(ax);
+               } else if (ax == 0.5)
+                       return ((ax - ax) / (ax - ax));
+               else
+                       t = -__kernel_tanpil(1 - ax);
+               return (copysignl(t, x));
+       }
+
+       if (ix < 0x1p112) {
+               xf = floorl(ax);
+               ax -= xf;
+               if (ax < 0.5)
+                       t = ax == 0 ? 0 : __kernel_tanpil(ax);
+               else if (ax == 0.5)
+                       return ((ax - ax) / (ax - ax));
+               else
+                       t = -__kernel_tanpil(1 - ax);
+               return (copysignl(t, x));
+       }
+
+       /* x = +-inf or nan. */
+       if (isinf(x) || isnan(x))
+               return (vzero / vzero);
+
+       /*
+        * |x| >= 0x1p53 is always an integer, so return +-0.
+        */
+       return (copysignl(0, x));
+}
diff --git a/lib/msun/ld80/k_cospil.h b/lib/msun/ld80/k_cospil.h
new file mode 100644
index 000000000000..6e13ef02aea2
--- /dev/null
+++ b/lib/msun/ld80/k_cospil.h
@@ -0,0 +1,42 @@
+/*-
+ * 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/k_cospi.c for implementation details.
+ */
+
+static inline long double
+__kernel_cospil(long double x)
+{
+       long 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_cosl(hi, lo));
+}
diff --git a/lib/msun/ld80/k_sinpil.h b/lib/msun/ld80/k_sinpil.h
new file mode 100644
index 000000000000..00241b932e9e
--- /dev/null
+++ b/lib/msun/ld80/k_sinpil.h
@@ -0,0 +1,42 @@
+/*-
+ * 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/k_sinpi.c for implementation details.
+ */
+
+static inline long double
+__kernel_sinpil(long double x)
+{
+       long 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_sinl(hi, lo, 1));
+}
diff --git a/lib/msun/ld80/s_cospil.c b/lib/msun/ld80/s_cospil.c
new file mode 100644
index 000000000000..199479e9eaf9
--- /dev/null
+++ b/lib/msun/ld80/s_cospil.c
@@ -0,0 +1,129 @@
+/*-
+ * 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.
+ */
+
+#ifdef __i386__
+#include <ieeefp.h>
+#endif
+#include <stdint.h>
+
+#include "fpmath.h"
+#include "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_cospil.h"
+#include "k_sinpil.h"
+
+volatile static const double vzero = 0;
+
+long double
+cospil(long double x)
+{
+       long double ax, c;
+       uint64_t lx, m;
+       uint32_t j0;
+       uint16_t hx, ix;
+
+       EXTRACT_LDBL80_WORDS(hx, lx, x);
+       ix = hx & 0x7fff;
+       INSERT_LDBL80_WORDS(ax, ix, lx);
+
+       ENTERI();
+
+       if (ix < 0x3fff) {                      /* |x| < 1 */
+               if (ix < 0x3ffd) {              /* |x| < 0.25 */
+                       if (ix < 0x3fdd) {      /* |x| < 0x1p-34 */
+                               if ((int)x == 0)
+                                       RETURNI(1);
+                       }
+                       RETURNI(__kernel_cospil(ax));
+               }
+
+               if (ix < 0x3ffe)                        /* |x| < 0.5 */
+                       c = __kernel_sinpil(0.5 - ax);
+               else if (lx < 0xc000000000000000ull) {  /* |x| < 0.75 */
+                       if (ax == 0.5)
+                               RETURNI(0);
+                       c = -__kernel_sinpil(ax - 0.5);
+               } else
+                       c = -__kernel_cospil(1 - ax);
+               RETURNI(c);
+       }
+
+       if (ix < 0x403e) {              /* 1 <= |x| < 0x1p63 */
+               /* Determine integer part of ax. */
+               j0 = ix - 0x3fff + 1;
+               if (j0 < 32) {
+                       lx = (lx >> 32) << 32;
+                       lx &= ~(((lx << 32)-1) >> j0);
+               } else {
+                       m = (uint64_t)-1 >> (j0 + 1);
+                       if (lx & m) lx &= ~m;
+               }
+               INSERT_LDBL80_WORDS(x, ix, lx);
+
+               ax -= x;
+               EXTRACT_LDBL80_WORDS(ix, lx, ax);
+
+               if (ix < 0x3ffe) {                      /* |x| < 0.5 */
+                       if (ix < 0x3ffd)                /* |x| < 0.25 */
+                               c = ix == 0 ? 1 : __kernel_cospil(ax);
+                       else
+                               c = __kernel_sinpil(0.5 - ax);
+
+               } else {
+                       if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
+                               if (ax == 0.5)
+                                       RETURNI(0);
+                               c = -__kernel_sinpil(ax - 0.5);
+                       } else
+                               c = -__kernel_cospil(1 - ax);
+               }
+
+               if (j0 > 40)
+                       x -= 0x1p40;
+               if (j0 > 30)
+                       x -= 0x1p30;
+               j0 = (uint32_t)x;
+
+               RETURNI(j0 & 1 ? -c : c);
+       }
+
+       if (ix >= 0x7fff)
+               RETURNI(vzero / vzero);
+
+       /*
+        * |x| >= 0x1p63 is always an even integer, so return 1.
+        */
+       RETURNI(1);
+}
diff --git a/lib/msun/ld80/s_sinpil.c b/lib/msun/ld80/s_sinpil.c
new file mode 100644
index 000000000000..4cefa92352e1
--- /dev/null
+++ b/lib/msun/ld80/s_sinpil.c
@@ -0,0 +1,140 @@
+/*-
+ * 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.
+ */
+
+#ifdef __i386__
+#include <ieeefp.h>
+#endif
+#include <stdint.h>
+
+#include "fpmath.h"
+#include "math.h"
+#include "math_private.h"
+
+static const union IEEEl2bits
+pi_hi_u = LD80C(0xc90fdaa200000000,   1, 3.14159265346825122833e+00L),
+pi_lo_u = LD80C(0x85a308d313198a2e, -33, 1.21542010130123852029e-10L);
+#define        pi_hi   (pi_hi_u.e)
+#define        pi_lo   (pi_lo_u.e)
+
+#include "k_cospil.h"
+#include "k_sinpil.h"
+
+volatile static const double vzero = 0;
+
+long double
+sinpil(long double x)
+{
+       long double ax, hi, lo, s;
+       uint64_t lx, m;
+       uint32_t j0;
+       uint16_t hx, ix;
+
+       EXTRACT_LDBL80_WORDS(hx, lx, x);
+       ix = hx & 0x7fff;
+       INSERT_LDBL80_WORDS(ax, ix, lx);
+
+       ENTERI();
+
+       if (ix < 0x3fff) {                      /* |x| < 1 */
+               if (ix < 0x3ffd) {              /* |x| < 0.25 */
+                       if (ix < 0x3fdd) {      /* |x| < 0x1p-34 */
+                               if (x == 0)
+                                       RETURNI(x);
+                               INSERT_LDBL80_WORDS(hi, hx,
+                                   lx & 0xffffffff00000000ull);
+                               hi *= 0x1p63L;
+                               lo = x * 0x1p63L - hi;
+                               s = (pi_lo + pi_hi) * lo + pi_lo * hi +
+                                   pi_hi * hi;
+                               RETURNI(s * 0x1p-63L);
+                       }
+                       s = __kernel_sinpil(ax);
+                       RETURNI((hx & 0x8000) ? -s : s);
+               }
+
+               if (ix < 0x3ffe)                        /* |x| < 0.5 */
+                       s = __kernel_cospil(0.5 - ax);
+               else if (lx < 0xc000000000000000ull)    /* |x| < 0.75 */
+                       s = __kernel_cospil(ax - 0.5);
+               else
+                       s = __kernel_sinpil(1 - ax);
+               RETURNI((hx & 0x8000) ? -s : s);
+       }
+
+       if (ix < 0x403e) {              /* 1 <= |x| < 0x1p63 */
+               /* Determine integer part of ax. */
+               j0 = ix - 0x3fff + 1;
+               if (j0 < 32) {
+                       lx = (lx >> 32) << 32;
+                       lx &= ~(((lx << 32)-1) >> j0);
+               } else {
+                       m = (uint64_t)-1 >> (j0 + 1);
+                       if (lx & m) lx &= ~m;
+               }
+               INSERT_LDBL80_WORDS(x, ix, lx);
+
+               ax -= x;
+               EXTRACT_LDBL80_WORDS(ix, lx, ax);
+
+               if (ix == 0) {
+                       s = 0;
+               } else {
+                       if (ix < 0x3ffe) {              /* |x| < 0.5 */
+                               if (ix < 0x3ffd)        /* |x| < 0.25 */
+                                       s = __kernel_sinpil(ax);
+                               else 
+                                       s = __kernel_cospil(0.5 - ax);
+                       } else {
+                                                       /* |x| < 0.75 */
+                               if (lx < 0xc000000000000000ull)
+                                       s = __kernel_cospil(ax - 0.5);
+                               else
+                                       s = __kernel_sinpil(1 - ax);
+                       }
+
+                       if (j0 > 40)
+                               x -= 0x1p40;
+                       if (j0 > 30)
+                               x -= 0x1p30;
+                       j0 = (uint32_t)x;
+                       if (j0 & 1) s = -s;
+               }
+               RETURNI((hx & 0x8000) ? -s : s);
+       }
+
+       /* x = +-inf or nan. */
+       if (ix >= 0x7fff)
+               RETURNI(vzero / vzero);
+
+       /*
+        * |x| >= 0x1p63 is always an integer, so return +-0.
+        */
+       RETURNI(copysignl(0, x));
+}
diff --git a/lib/msun/ld80/s_tanpil.c b/lib/msun/ld80/s_tanpil.c
new file mode 100644
index 000000000000..02451e562025
--- /dev/null
+++ b/lib/msun/ld80/s_tanpil.c
@@ -0,0 +1,139 @@
+/*-
+ * 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:
*** 1464 LINES SKIPPED ***

Reply via email to