xref: /freebsd/lib/msun/ld80/s_cospil.c (revision 81b22a98)
1 /*-
2  * Copyright (c) 2017 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 		/* Determine integer part of ax. */
85 		j0 = ix - 0x3fff + 1;
86 		if (j0 < 32) {
87 			lx = (lx >> 32) << 32;
88 			lx &= ~(((lx << 32)-1) >> j0);
89 		} else {
90 			m = (uint64_t)-1 >> (j0 + 1);
91 			if (lx & m) lx &= ~m;
92 		}
93 		INSERT_LDBL80_WORDS(x, ix, lx);
94 
95 		ax -= x;
96 		EXTRACT_LDBL80_WORDS(ix, lx, ax);
97 
98 		if (ix < 0x3ffe) {			/* |x| < 0.5 */
99 			if (ix < 0x3ffd)		/* |x| < 0.25 */
100 				c = ix == 0 ? 1 : __kernel_cospil(ax);
101 			else
102 				c = __kernel_sinpil(0.5 - ax);
103 
104 		} else {
105 			if (lx < 0xc000000000000000ull) { /* |x| < 0.75 */
106 				if (ax == 0.5)
107 					RETURNI(0);
108 				c = -__kernel_sinpil(ax - 0.5);
109 			} else
110 				c = -__kernel_cospil(1 - ax);
111 		}
112 
113 		if (j0 > 40)
114 			x -= 0x1p40;
115 		if (j0 > 30)
116 			x -= 0x1p30;
117 		j0 = (uint32_t)x;
118 
119 		RETURNI(j0 & 1 ? -c : c);
120 	}
121 
122 	if (ix >= 0x7fff)
123 		RETURNI(vzero / vzero);
124 
125 	/*
126 	 * |x| >= 0x1p63 is always an even integer, so return 1.
127 	 */
128 	RETURNI(1);
129 }
130