xref: /freebsd-src/lib/msun/ld80/s_cospil.c (revision 2d3b0a687b910c84606e4bc36176945ad5c60406)
1dce5f3abSSteve Kargl /*-
2*2d3b0a68SSteve Kargl  * Copyright (c) 2017, 2023 Steven G. Kargl
3dce5f3abSSteve Kargl  * All rights reserved.
4dce5f3abSSteve Kargl  *
5dce5f3abSSteve Kargl  * Redistribution and use in source and binary forms, with or without
6dce5f3abSSteve Kargl  * modification, are permitted provided that the following conditions
7dce5f3abSSteve Kargl  * are met:
8dce5f3abSSteve Kargl  * 1. Redistributions of source code must retain the above copyright
9dce5f3abSSteve Kargl  *    notice unmodified, this list of conditions, and the following
10dce5f3abSSteve Kargl  *    disclaimer.
11dce5f3abSSteve Kargl  * 2. Redistributions in binary form must reproduce the above copyright
12dce5f3abSSteve Kargl  *    notice, this list of conditions and the following disclaimer in the
13dce5f3abSSteve Kargl  *    documentation and/or other materials provided with the distribution.
14dce5f3abSSteve Kargl  *
15dce5f3abSSteve Kargl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16dce5f3abSSteve Kargl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17dce5f3abSSteve Kargl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18dce5f3abSSteve Kargl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19dce5f3abSSteve Kargl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20dce5f3abSSteve Kargl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21dce5f3abSSteve Kargl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22dce5f3abSSteve Kargl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23dce5f3abSSteve Kargl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24dce5f3abSSteve Kargl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25dce5f3abSSteve Kargl  */
26dce5f3abSSteve Kargl 
27dce5f3abSSteve Kargl /*
28dce5f3abSSteve Kargl  * See ../src/s_cospi.c for implementation details.
29dce5f3abSSteve Kargl  */
30dce5f3abSSteve Kargl 
31dce5f3abSSteve Kargl #ifdef __i386__
32dce5f3abSSteve Kargl #include <ieeefp.h>
33dce5f3abSSteve Kargl #endif
34dce5f3abSSteve Kargl #include <stdint.h>
35dce5f3abSSteve Kargl 
36dce5f3abSSteve Kargl #include "fpmath.h"
37dce5f3abSSteve Kargl #include "math.h"
38dce5f3abSSteve Kargl #include "math_private.h"
39dce5f3abSSteve Kargl 
40dce5f3abSSteve Kargl static const double
41dce5f3abSSteve Kargl pi_hi = 3.1415926814079285e+00,	/* 0x400921fb 0x58000000 */
42dce5f3abSSteve Kargl pi_lo =-2.7818135228334233e-08;	/* 0xbe5dde97 0x3dcb3b3a */
43dce5f3abSSteve Kargl 
44dce5f3abSSteve Kargl #include "k_cospil.h"
45dce5f3abSSteve Kargl #include "k_sinpil.h"
46dce5f3abSSteve Kargl 
47dce5f3abSSteve Kargl volatile static const double vzero = 0;
48dce5f3abSSteve Kargl 
49dce5f3abSSteve Kargl long double
cospil(long double x)50dce5f3abSSteve Kargl cospil(long double x)
51dce5f3abSSteve Kargl {
52dce5f3abSSteve Kargl 	long double ax, c;
53dce5f3abSSteve Kargl 	uint64_t lx, m;
54dce5f3abSSteve Kargl 	uint32_t j0;
55dce5f3abSSteve Kargl 	uint16_t hx, ix;
56dce5f3abSSteve Kargl 
57dce5f3abSSteve Kargl 	EXTRACT_LDBL80_WORDS(hx, lx, x);
58dce5f3abSSteve Kargl 	ix = hx & 0x7fff;
59dce5f3abSSteve Kargl 	INSERT_LDBL80_WORDS(ax, ix, lx);
60dce5f3abSSteve Kargl 
61dce5f3abSSteve Kargl 	ENTERI();
62dce5f3abSSteve Kargl 
63dce5f3abSSteve Kargl 	if (ix < 0x3fff) {			/* |x| < 1 */
64dce5f3abSSteve Kargl 		if (ix < 0x3ffd) {		/* |x| < 0.25 */
65dce5f3abSSteve Kargl 			if (ix < 0x3fdd) {	/* |x| < 0x1p-34 */
66dce5f3abSSteve Kargl 				if ((int)x == 0)
67dce5f3abSSteve Kargl 					RETURNI(1);
68dce5f3abSSteve Kargl 			}
69dce5f3abSSteve Kargl 			RETURNI(__kernel_cospil(ax));
70dce5f3abSSteve Kargl 		}
71dce5f3abSSteve Kargl 
72dce5f3abSSteve Kargl 		if (ix < 0x3ffe)			/* |x| < 0.5 */
73dce5f3abSSteve Kargl 			c = __kernel_sinpil(0.5 - ax);
74dce5f3abSSteve Kargl 		else if (lx < 0xc000000000000000ull) {	/* |x| < 0.75 */
75dce5f3abSSteve Kargl 			if (ax == 0.5)
76dce5f3abSSteve Kargl 				RETURNI(0);
77dce5f3abSSteve Kargl 			c = -__kernel_sinpil(ax - 0.5);
78dce5f3abSSteve Kargl 		} else
79dce5f3abSSteve Kargl 			c = -__kernel_cospil(1 - ax);
80dce5f3abSSteve Kargl 		RETURNI(c);
81dce5f3abSSteve Kargl 	}
82dce5f3abSSteve Kargl 
83dce5f3abSSteve Kargl 	if (ix < 0x403e) {			/* 1 <= |x| < 0x1p63 */
84*2d3b0a68SSteve Kargl 		FFLOORL80(x, j0, ix, lx);	/* Integer part of ax. */
85dce5f3abSSteve Kargl 		ax -= x;
86dce5f3abSSteve Kargl 		EXTRACT_LDBL80_WORDS(ix, lx, ax);
87dce5f3abSSteve Kargl 
88dce5f3abSSteve Kargl 		if (ix < 0x3ffe) {			/* |x| < 0.5 */
89dce5f3abSSteve Kargl 			if (ix < 0x3ffd)		/* |x| < 0.25 */
90dce5f3abSSteve Kargl 				c = ix == 0 ? 1 : __kernel_cospil(ax);
91dce5f3abSSteve Kargl 			else
92dce5f3abSSteve Kargl 				c = __kernel_sinpil(0.5 - ax);
93dce5f3abSSteve Kargl 
94dce5f3abSSteve Kargl 		} else {
95dce5f3abSSteve Kargl 			if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
96dce5f3abSSteve Kargl 				if (ax == 0.5)
97dce5f3abSSteve Kargl 					RETURNI(0);
98dce5f3abSSteve Kargl 				c = -__kernel_sinpil(ax - 0.5);
99dce5f3abSSteve Kargl 			} else
100dce5f3abSSteve Kargl 				c = -__kernel_cospil(1 - ax);
101dce5f3abSSteve Kargl 		}
102dce5f3abSSteve Kargl 
103dce5f3abSSteve Kargl 		if (j0 > 40)
104dce5f3abSSteve Kargl 			x -= 0x1p40;
105dce5f3abSSteve Kargl 		if (j0 > 30)
106dce5f3abSSteve Kargl 			x -= 0x1p30;
107dce5f3abSSteve Kargl 		j0 = (uint32_t)x;
108dce5f3abSSteve Kargl 
109dce5f3abSSteve Kargl 		RETURNI(j0 & 1 ? -c : c);
110dce5f3abSSteve Kargl 	}
111dce5f3abSSteve Kargl 
112dce5f3abSSteve Kargl 	if (ix >= 0x7fff)
113dce5f3abSSteve Kargl 		RETURNI(vzero / vzero);
114dce5f3abSSteve Kargl 
115dce5f3abSSteve Kargl 	/*
116*2d3b0a68SSteve Kargl 	 * For 0x1p63 <= |x| < 0x1p64 need to determine if x is an even
117*2d3b0a68SSteve Kargl 	 * or odd integer to return t = +1 or -1.
118*2d3b0a68SSteve Kargl 	 * For |x| >= 0x1p64, it is always an even integer, so t = 1.
119dce5f3abSSteve Kargl 	 */
120*2d3b0a68SSteve Kargl 	RETURNI(ix >= 0x403f ? 1 : ((lx & 1) ? -1 : 1));
121dce5f3abSSteve Kargl }
122