2 * Copyright (c) 2007 Steven G. Kargl
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Compute sin(x) for x where x is reduced to y = x - k * pi / 2.
37 #include "math_private.h"
40 #if LDBL_MANT_DIG == 64
43 #elif LDBL_MANT_DIG == 113
47 #error "Unsupported long double format"
50 static const long double two24 = 1.67772160000000000000e+07L;
57 double xd[NX], yd[PREC];
64 /* If x = +-0 or x is a subnormal number, then sin(x) = x */
68 /* If x = NaN or Inf, then sin(x) = NaN. */
69 if (z.bits.exp == 32767)
70 return ((x - x) / (x - x));
72 /* Optimize the case where x is already within range. */
74 hi = __kernel_sinl(z.e, 0, 0);
75 return (s ? -hi : hi);
78 /* Split z.e into a 24-bit representation. */
79 e0 = ilogbl(z.e) - 23;
80 z.e = scalbnl(z.e, -e0);
81 for (i = 0; i < NX; i++) {
82 xd[i] = (double)((int32_t)z.e);
83 z.e = (z.e - xd[i]) * two24;
86 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
87 e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
90 hi = (long double)yd[0] + yd[1];
91 lo = yd[1] - (hi - yd[0]);
94 t = (long double)yd[2] + yd[1];
96 lo = yd[0] - (hi - t);
101 hi = __kernel_sinl(hi, lo, 1);
104 hi = __kernel_cosl(hi, lo);
107 hi = - __kernel_sinl(hi, lo, 1);
110 hi = - __kernel_cosl(hi, lo);
114 return (s ? -hi : hi);