xref: /freebsd/lib/msun/src/s_sinl.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*-
2  * Copyright (c) 2007 Steven G. Kargl
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Compute sin(x) for x where x is reduced to y = x - k * pi / 2.
32  */
33 
34 #include <float.h>
35 
36 #include "math.h"
37 #include "math_private.h"
38 #include "fpmath.h"
39 
40 #if LDBL_MANT_DIG == 64
41 #define	NX	3
42 #define	PREC	2
43 #elif LDBL_MANT_DIG == 113
44 #define	NX	5
45 #define	PREC	3
46 #else
47 #error "Unsupported long double format"
48 #endif
49 
50 static const long double two24 = 1.67772160000000000000e+07L;
51 
52 long double
53 sinl(long double x)
54 {
55 	union IEEEl2bits z;
56 	int i, e0, s;
57 	double xd[NX], yd[PREC];
58 	long double hi, lo;
59 
60 	z.e = x;
61 	s = z.bits.sign;
62 	z.bits.sign = 0;
63 
64 	/* If x = +-0 or x is a subnormal number, then sin(x) = x */
65 	if (z.bits.exp == 0)
66 		return (x);
67 
68 	/* If x = NaN or Inf, then sin(x) = NaN. */
69 	if (z.bits.exp == 32767)
70 		return ((x - x) / (x - x));
71 
72 	/* Optimize the case where x is already within range. */
73 	if (z.e < M_PI_4) {
74 		hi = __kernel_sinl(z.e, 0, 0);
75 		return  (s ? -hi : hi);
76 	}
77 
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;
84 	}
85 
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);
88 
89 #if PREC == 2
90 	hi = (long double)yd[0] + yd[1];
91 	lo = yd[1] - (hi - yd[0]);
92 #else /* PREC == 3 */
93 	long double t;
94 	t = (long double)yd[2] + yd[1];
95 	hi = t + yd[0];
96 	lo = yd[0] - (hi - t);
97 #endif
98 
99 	switch (e0 & 3) {
100 	case 0:
101 	    hi = __kernel_sinl(hi, lo, 1);
102 	    break;
103 	case 1:
104 	    hi = __kernel_cosl(hi, lo);
105 	    break;
106 	case 2:
107 	    hi = - __kernel_sinl(hi, lo, 1);
108 	    break;
109 	case 3:
110 	    hi = - __kernel_cosl(hi, lo);
111 	    break;
112 	}
113 
114 	return (s ? -hi : hi);
115 }
116