xref: /netbsd-src/lib/libm/ld80/s_sinpil.c (revision cfe182f36bde4c4d81e1607954ce22a67cf35d7a)
1 /*-
2  * Copyright (c) 2017, 2023 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 /*
28  * See ../src/s_sinpi.c for implementation details.
29  */
30 
31 #ifdef __i386__
32 #include <ieeefp.h>
33 #endif
34 #include <stdint.h>
35 
36 #include "math.h"
37 #include "math_private.h"
38 
39 static const union ieee_ext_u
40 pi_hi_u = LD80C(0xc90fdaa200000000,   1, 3.14159265346825122833e+00L),
41 pi_lo_u = LD80C(0x85a308d313198a2e, -33, 1.21542010130123852029e-10L);
42 #define	pi_hi	(pi_hi_u.extu_ld)
43 #define	pi_lo	(pi_lo_u.extu_ld)
44 
45 #include "k_cospil.h"
46 #include "k_sinpil.h"
47 
48 static volatile const double vzero = 0;
49 
50 long double
51 sinpil(long double x)
52 {
53 	long double ax, hi, lo, s;
54 	uint64_t lx;
55 	uint32_t j0;
56 	uint16_t hx, ix;
57 
58 	EXTRACT_LDBL80_WORDS(hx, lx, x);
59 	ix = hx & 0x7fff;
60 	INSERT_LDBL80_WORDS(ax, ix, lx);
61 
62 	ENTERI();
63 
64 	if (ix < 0x3fff) {			/* |x| < 1 */
65 		if (ix < 0x3ffd) {		/* |x| < 0.25 */
66 			if (ix < 0x3fdd) {	/* |x| < 0x1p-34 */
67 				if (x == 0)
68 					RETURNI(x);
69 				INSERT_LDBL80_WORDS(hi, hx,
70 				    lx & 0xffffffff00000000ull);
71 				hi *= 0x1p63L;
72 				lo = x * 0x1p63L - hi;
73 				s = (pi_lo + pi_hi) * lo + pi_lo * hi +
74 				    pi_hi * hi;
75 				RETURNI(s * 0x1p-63L);
76 			}
77 			s = __kernel_sinpil(ax);
78 			RETURNI((hx & 0x8000) ? -s : s);
79 		}
80 
81 		if (ix < 0x3ffe)			/* |x| < 0.5 */
82 			s = __kernel_cospil(0.5 - ax);
83 		else if (lx < 0xc000000000000000ull)	/* |x| < 0.75 */
84 			s = __kernel_cospil(ax - 0.5);
85 		else
86 			s = __kernel_sinpil(1 - ax);
87 		RETURNI((hx & 0x8000) ? -s : s);
88 	}
89 
90 	if (ix < 0x403e) {			/* 1 <= |x| < 0x1p63 */
91 		FFLOORL80(x, j0, ix, lx);	/* Integer part of ax. */
92 		ax -= x;
93 		EXTRACT_LDBL80_WORDS(ix, lx, ax);
94 
95 		if (ix == 0) {
96 			s = 0;
97 		} else {
98 			if (ix < 0x3ffe) {		/* |x| < 0.5 */
99 				if (ix < 0x3ffd)	/* |x| < 0.25 */
100 					s = __kernel_sinpil(ax);
101 				else
102 					s = __kernel_cospil(0.5 - ax);
103 			} else {
104 							/* |x| < 0.75 */
105 				if (lx < 0xc000000000000000ull)
106 					s = __kernel_cospil(ax - 0.5);
107 				else
108 					s = __kernel_sinpil(1 - ax);
109 			}
110 
111 			if (j0 > 40)
112 				x -= 0x1p40;
113 			if (j0 > 30)
114 				x -= 0x1p30;
115 			j0 = (uint32_t)x;
116 			if (j0 & 1) s = -s;
117 		}
118 		RETURNI((hx & 0x8000) ? -s : s);
119 	}
120 
121 	/* x = +-inf or nan. */
122 	if (ix >= 0x7fff)
123 		RETURNI(vzero / vzero);
124 
125 	/*
126 	 * |x| >= 0x1p63 is always an integer, so return +-0.
127 	 */
128 	RETURNI(copysignl(0, x));
129 }
130