xref: /freebsd/lib/msun/src/s_cospi.c (revision 9768746b)
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  * cospi(x) computes cos(pi*x) without multiplication by pi (almost).  First,
29  * note that cospi(-x) = cospi(x), so the algorithm considers only |x|.  The
30  * method used depends on the magnitude of x.
31  *
32  * 1. For small |x|, cospi(x) = 1 with FE_INEXACT raised where a sloppy
33  *    threshold is used.  The threshold is |x| < 0x1pN with N = -(P/2+M).
34  *    P is the precision of the floating-point type and M = 2 to 4.
35  *
36  * 2. For |x| < 1, argument reduction is not required and sinpi(x) is
37  *    computed by calling a kernel that leverages the kernels for sin(x)
38  *    ans cos(x).  See k_sinpi.c and k_cospi.c for details.
39  *
40  * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where
41  *    |x| = j0 + r with j0 an integer and the remainder r satisfies
42  *    0 <= r < 1.  With the given domain, a simplified inline floor(x)
43  *    is used.  Also, note the following identity
44  *
45  *    cospi(x) = cos(pi*(j0+r))
46  *             = cos(pi*j0) * cos(pi*r) - sin(pi*j0) * sin(pi*r)
47  *             = cos(pi*j0) * cos(pi*r)
48  *             = +-cospi(r)
49  *
50  *    If j0 is even, then cos(pi*j0) = 1. If j0 is odd, then cos(pi*j0) = -1.
51  *    cospi(r) is then computed via an appropriate kernel.
52  *
53  * 4. For |x| >= 0x1p(P-1), |x| is integral and cospi(x) = 1.
54  *
55  * 5. Special cases:
56  *
57  *    cospi(+-0) = 1.
58  *    cospi(n.5) = 0 for n an integer.
59  *    cospi(+-inf) = nan.  Raises the "invalid" floating-point exception.
60  *    cospi(nan) = nan.  Raises the "invalid" floating-point exception.
61  */
62 
63 #include <float.h>
64 #include "math.h"
65 #include "math_private.h"
66 
67 static const double
68 pi_hi = 3.1415926814079285e+00,	/* 0x400921fb 0x58000000 */
69 pi_lo =-2.7818135228334233e-08;	/* 0xbe5dde97 0x3dcb3b3a */
70 
71 #include "k_cospi.h"
72 #include "k_sinpi.h"
73 
74 volatile static const double vzero = 0;
75 
76 double
77 cospi(double x)
78 {
79 	double ax, c;
80 	uint32_t hx, ix, j0, lx;
81 
82 	EXTRACT_WORDS(hx, lx, x);
83 	ix = hx & 0x7fffffff;
84 	INSERT_WORDS(ax, ix, lx);
85 
86 	if (ix < 0x3ff00000) {			/* |x| < 1 */
87 		if (ix < 0x3fd00000) {		/* |x| < 0.25 */
88 			if (ix < 0x3e200000) {	/* |x| < 0x1p-29 */
89 				if ((int)ax == 0)
90 					return (1);
91 			}
92 			return (__kernel_cospi(ax));
93 		}
94 
95 		if (ix < 0x3fe00000)		/* |x| < 0.5 */
96 			c = __kernel_sinpi(0.5 - ax);
97 		else if (ix < 0x3fe80000){	/* |x| < 0.75 */
98 			if (ax == 0.5)
99 				return (0);
100 			c = -__kernel_sinpi(ax - 0.5);
101 		} else
102 			c = -__kernel_cospi(1 - ax);
103 		return (c);
104 	}
105 
106 	if (ix < 0x43300000) {		/* 1 <= |x| < 0x1p52 */
107 		/* Determine integer part of ax. */
108 		j0 = ((ix >> 20) & 0x7ff) - 0x3ff;
109 		if (j0 < 20) {
110 			ix &= ~(0x000fffff >> j0);
111 			lx = 0;
112 		} else {
113 			lx &= ~((uint32_t)0xffffffff >> (j0 - 20));
114 		}
115 		INSERT_WORDS(x, ix, lx);
116 
117 		ax -= x;
118 		EXTRACT_WORDS(ix, lx, ax);
119 
120 
121 		if (ix < 0x3fe00000) {		/* |x| < 0.5 */
122 			if (ix < 0x3fd00000)	/* |x| < 0.25 */
123 				c = ix == 0 ? 1 : __kernel_cospi(ax);
124 			else
125 				c = __kernel_sinpi(0.5 - ax);
126 		} else {
127 			if (ix < 0x3fe80000) {	/* |x| < 0.75 */
128 				if (ax == 0.5)
129 					return (0);
130 				c = -__kernel_sinpi(ax - 0.5);
131 			} else
132 				c = -__kernel_cospi(1 - ax);
133 		}
134 
135 		if (j0 > 30)
136 			x -= 0x1p30;
137 		j0 = (uint32_t)x;
138 		return (j0 & 1 ? -c : c);
139 	}
140 
141 	if (ix >= 0x7f800000)
142 		return (vzero / vzero);
143 
144 	/*
145 	 * |x| >= 0x1p52 is always an even integer, so return 1.
146 	 */
147 	return (1);
148 }
149 
150 #if LDBL_MANT_DIG == 53
151 __weak_reference(cospi, cospil);
152 #endif
153