xref: /freebsd/lib/msun/ld80/s_cospil.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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_cospi.c for implementation details.
29  */
30 
31 #ifdef __i386__
32 #include <ieeefp.h>
33 #endif
34 #include <stdint.h>
35 
36 #include "fpmath.h"
37 #include "math.h"
38 #include "math_private.h"
39 
40 static const double
41 pi_hi = 3.1415926814079285e+00,	/* 0x400921fb 0x58000000 */
42 pi_lo =-2.7818135228334233e-08;	/* 0xbe5dde97 0x3dcb3b3a */
43 
44 #include "k_cospil.h"
45 #include "k_sinpil.h"
46 
47 volatile static const double vzero = 0;
48 
49 long double
50 cospil(long double x)
51 {
52 	long double ax, c;
53 	uint64_t lx, m;
54 	uint32_t j0;
55 	uint16_t hx, ix;
56 
57 	EXTRACT_LDBL80_WORDS(hx, lx, x);
58 	ix = hx & 0x7fff;
59 	INSERT_LDBL80_WORDS(ax, ix, lx);
60 
61 	ENTERI();
62 
63 	if (ix < 0x3fff) {			/* |x| < 1 */
64 		if (ix < 0x3ffd) {		/* |x| < 0.25 */
65 			if (ix < 0x3fdd) {	/* |x| < 0x1p-34 */
66 				if ((int)x == 0)
67 					RETURNI(1);
68 			}
69 			RETURNI(__kernel_cospil(ax));
70 		}
71 
72 		if (ix < 0x3ffe)			/* |x| < 0.5 */
73 			c = __kernel_sinpil(0.5 - ax);
74 		else if (lx < 0xc000000000000000ull) {	/* |x| < 0.75 */
75 			if (ax == 0.5)
76 				RETURNI(0);
77 			c = -__kernel_sinpil(ax - 0.5);
78 		} else
79 			c = -__kernel_cospil(1 - ax);
80 		RETURNI(c);
81 	}
82 
83 	if (ix < 0x403e) {			/* 1 <= |x| < 0x1p63 */
84 		FFLOORL80(x, j0, ix, lx);	/* Integer part of ax. */
85 		ax -= x;
86 		EXTRACT_LDBL80_WORDS(ix, lx, ax);
87 
88 		if (ix < 0x3ffe) {			/* |x| < 0.5 */
89 			if (ix < 0x3ffd)		/* |x| < 0.25 */
90 				c = ix == 0 ? 1 : __kernel_cospil(ax);
91 			else
92 				c = __kernel_sinpil(0.5 - ax);
93 
94 		} else {
95 			if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
96 				if (ax == 0.5)
97 					RETURNI(0);
98 				c = -__kernel_sinpil(ax - 0.5);
99 			} else
100 				c = -__kernel_cospil(1 - ax);
101 		}
102 
103 		if (j0 > 40)
104 			x -= 0x1p40;
105 		if (j0 > 30)
106 			x -= 0x1p30;
107 		j0 = (uint32_t)x;
108 
109 		RETURNI(j0 & 1 ? -c : c);
110 	}
111 
112 	if (ix >= 0x7fff)
113 		RETURNI(vzero / vzero);
114 
115 	/*
116 	 * For 0x1p63 <= |x| < 0x1p64 need to determine if x is an even
117 	 * or odd integer to return t = +1 or -1.
118 	 * For |x| >= 0x1p64, it is always an even integer, so t = 1.
119 	 */
120 	RETURNI(ix >= 0x403f ? 1 : ((lx & 1) ? -1 : 1));
121 }
122