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