xref: /netbsd-src/lib/libm/src/s_cospi.c (revision cfe182f36bde4c4d81e1607954ce22a67cf35d7a)
1*cfe182f3Schristos /*-
2*cfe182f3Schristos  * Copyright (c) 2017, 2023 Steven G. Kargl
3*cfe182f3Schristos  * All rights reserved.
4*cfe182f3Schristos  *
5*cfe182f3Schristos  * Redistribution and use in source and binary forms, with or without
6*cfe182f3Schristos  * modification, are permitted provided that the following conditions
7*cfe182f3Schristos  * are met:
8*cfe182f3Schristos  * 1. Redistributions of source code must retain the above copyright
9*cfe182f3Schristos  *    notice unmodified, this list of conditions, and the following
10*cfe182f3Schristos  *    disclaimer.
11*cfe182f3Schristos  * 2. Redistributions in binary form must reproduce the above copyright
12*cfe182f3Schristos  *    notice, this list of conditions and the following disclaimer in the
13*cfe182f3Schristos  *    documentation and/or other materials provided with the distribution.
14*cfe182f3Schristos  *
15*cfe182f3Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*cfe182f3Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*cfe182f3Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*cfe182f3Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*cfe182f3Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*cfe182f3Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*cfe182f3Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*cfe182f3Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*cfe182f3Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*cfe182f3Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*cfe182f3Schristos  */
26*cfe182f3Schristos 
27*cfe182f3Schristos /**
28*cfe182f3Schristos  * cospi(x) computes cos(pi*x) without multiplication by pi (almost).  First,
29*cfe182f3Schristos  * note that cospi(-x) = cospi(x), so the algorithm considers only |x|.  The
30*cfe182f3Schristos  * method used depends on the magnitude of x.
31*cfe182f3Schristos  *
32*cfe182f3Schristos  * 1. For small |x|, cospi(x) = 1 with FE_INEXACT raised where a sloppy
33*cfe182f3Schristos  *    threshold is used.  The threshold is |x| < 0x1pN with N = -(P/2+M).
34*cfe182f3Schristos  *    P is the precision of the floating-point type and M = 2 to 4.
35*cfe182f3Schristos  *
36*cfe182f3Schristos  * 2. For |x| < 1, argument reduction is not required and sinpi(x) is
37*cfe182f3Schristos  *    computed by calling a kernel that leverages the kernels for sin(x)
38*cfe182f3Schristos  *    ans cos(x).  See k_sinpi.c and k_cospi.c for details.
39*cfe182f3Schristos  *
40*cfe182f3Schristos  * 3. For 1 <= |x| < 0x1p(P-1), argument reduction is required where
41*cfe182f3Schristos  *    |x| = jj0 + r with jj0 an integer and the remainder r satisfies
42*cfe182f3Schristos  *    0 <= r < 1.  With the given domain, a simplified inline floor(x)
43*cfe182f3Schristos  *    is used.  Also, note the following identity
44*cfe182f3Schristos  *
45*cfe182f3Schristos  *    cospi(x) = cos(pi*(jj0+r))
46*cfe182f3Schristos  *             = cos(pi*jj0) * cos(pi*r) - sin(pi*jj0) * sin(pi*r)
47*cfe182f3Schristos  *             = cos(pi*jj0) * cos(pi*r)
48*cfe182f3Schristos  *             = +-cospi(r)
49*cfe182f3Schristos  *
50*cfe182f3Schristos  *    If jj0 is even, then cos(pi*jj0) = 1. If jj0 is odd, then cos(pi*jj0) = -1.
51*cfe182f3Schristos  *    cospi(r) is then computed via an appropriate kernel.
52*cfe182f3Schristos  *
53*cfe182f3Schristos  * 4. For |x| >= 0x1p(P-1), |x| is integral and cospi(x) = 1.
54*cfe182f3Schristos  *
55*cfe182f3Schristos  * 5. Special cases:
56*cfe182f3Schristos  *
57*cfe182f3Schristos  *    cospi(+-0) = 1.
58*cfe182f3Schristos  *    cospi(n.5) = 0 for n an integer.
59*cfe182f3Schristos  *    cospi(+-inf) = nan.  Raises the "invalid" floating-point exception.
60*cfe182f3Schristos  *    cospi(nan) = nan.  Raises the "invalid" floating-point exception.
61*cfe182f3Schristos  */
62*cfe182f3Schristos 
63*cfe182f3Schristos #include <sys/cdefs.h>
64*cfe182f3Schristos 
65*cfe182f3Schristos #include "namespace.h"
66*cfe182f3Schristos __weak_alias(cospi, _cospi)
67*cfe182f3Schristos 
68*cfe182f3Schristos #include <float.h>
69*cfe182f3Schristos #include "math.h"
70*cfe182f3Schristos #include "math_private.h"
71*cfe182f3Schristos 
72*cfe182f3Schristos static const double
73*cfe182f3Schristos pi_hi = 3.1415926814079285e+00,	/* 0x400921fb 0x58000000 */
74*cfe182f3Schristos pi_lo =-2.7818135228334233e-08;	/* 0xbe5dde97 0x3dcb3b3a */
75*cfe182f3Schristos 
76*cfe182f3Schristos #include "k_cospi.h"
77*cfe182f3Schristos #include "k_sinpi.h"
78*cfe182f3Schristos 
79*cfe182f3Schristos static volatile const double vzero = 0;
80*cfe182f3Schristos 
81*cfe182f3Schristos double
cospi(double x)82*cfe182f3Schristos cospi(double x)
83*cfe182f3Schristos {
84*cfe182f3Schristos 	double ax, c;
85*cfe182f3Schristos 	uint32_t hx, ix, jj0, lx;
86*cfe182f3Schristos 
87*cfe182f3Schristos 	EXTRACT_WORDS(hx, lx, x);
88*cfe182f3Schristos 	ix = hx & 0x7fffffff;
89*cfe182f3Schristos 	INSERT_WORDS(ax, ix, lx);
90*cfe182f3Schristos 
91*cfe182f3Schristos 	if (ix < 0x3ff00000) {			/* |x| < 1 */
92*cfe182f3Schristos 		if (ix < 0x3fd00000) {		/* |x| < 0.25 */
93*cfe182f3Schristos 			if (ix < 0x3e200000) {	/* |x| < 0x1p-29 */
94*cfe182f3Schristos 				if ((int)ax == 0)
95*cfe182f3Schristos 					return (1);
96*cfe182f3Schristos 			}
97*cfe182f3Schristos 			return (__kernel_cospi(ax));
98*cfe182f3Schristos 		}
99*cfe182f3Schristos 
100*cfe182f3Schristos 		if (ix < 0x3fe00000)		/* |x| < 0.5 */
101*cfe182f3Schristos 			c = __kernel_sinpi(0.5 - ax);
102*cfe182f3Schristos 		else if (ix < 0x3fe80000){	/* |x| < 0.75 */
103*cfe182f3Schristos 			if (ax == 0.5)
104*cfe182f3Schristos 				return (0);
105*cfe182f3Schristos 			c = -__kernel_sinpi(ax - 0.5);
106*cfe182f3Schristos 		} else
107*cfe182f3Schristos 			c = -__kernel_cospi(1 - ax);
108*cfe182f3Schristos 		return (c);
109*cfe182f3Schristos 	}
110*cfe182f3Schristos 
111*cfe182f3Schristos 	if (ix < 0x43300000) {		/* 1 <= |x| < 0x1p52 */
112*cfe182f3Schristos 		FFLOOR(x, jj0, ix, lx);	/* Integer part of ax. */
113*cfe182f3Schristos 		ax -= x;
114*cfe182f3Schristos 		EXTRACT_WORDS(ix, lx, ax);
115*cfe182f3Schristos 
116*cfe182f3Schristos 		if (ix < 0x3fe00000) {		/* |x| < 0.5 */
117*cfe182f3Schristos 			if (ix < 0x3fd00000)	/* |x| < 0.25 */
118*cfe182f3Schristos 				c = ix == 0 ? 1 : __kernel_cospi(ax);
119*cfe182f3Schristos 			else
120*cfe182f3Schristos 				c = __kernel_sinpi(0.5 - ax);
121*cfe182f3Schristos 		} else {
122*cfe182f3Schristos 			if (ix < 0x3fe80000) {	/* |x| < 0.75 */
123*cfe182f3Schristos 				if (ax == 0.5)
124*cfe182f3Schristos 					return (0);
125*cfe182f3Schristos 				c = -__kernel_sinpi(ax - 0.5);
126*cfe182f3Schristos 			} else
127*cfe182f3Schristos 				c = -__kernel_cospi(1 - ax);
128*cfe182f3Schristos 		}
129*cfe182f3Schristos 
130*cfe182f3Schristos 		if (jj0 > 30)
131*cfe182f3Schristos 			x -= 0x1p30;
132*cfe182f3Schristos 		jj0 = (uint32_t)x;
133*cfe182f3Schristos 		return (jj0 & 1 ? -c : c);
134*cfe182f3Schristos 	}
135*cfe182f3Schristos 
136*cfe182f3Schristos 	/* x = +-inf or nan. */
137*cfe182f3Schristos 	if (ix >= 0x7ff00000)
138*cfe182f3Schristos 		return (vzero / vzero);
139*cfe182f3Schristos 
140*cfe182f3Schristos 	/*
141*cfe182f3Schristos 	 * For 0x1p52 <= |x| < 0x1p53 need to determine if x is an even
142*cfe182f3Schristos 	 * or odd integer to return +1 or -1.
143*cfe182f3Schristos 	 * For |x| >= 0x1p53, it is always an even integer, so return 1.
144*cfe182f3Schristos 	 */
145*cfe182f3Schristos 	return (ix < 0x43400000 ? ((lx & 1) ? -1 : 1) : 1);
146*cfe182f3Schristos }
147