xref: /illumos-gate/usr/src/lib/libm/common/Q/__cosl.c (revision 25c28e83)
1*25c28e83SPiotr Jasiukajtis /*
2*25c28e83SPiotr Jasiukajtis  * CDDL HEADER START
3*25c28e83SPiotr Jasiukajtis  *
4*25c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
5*25c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
6*25c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
7*25c28e83SPiotr Jasiukajtis  *
8*25c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
10*25c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
11*25c28e83SPiotr Jasiukajtis  * and limitations under the License.
12*25c28e83SPiotr Jasiukajtis  *
13*25c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
14*25c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
16*25c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
17*25c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25c28e83SPiotr Jasiukajtis  *
19*25c28e83SPiotr Jasiukajtis  * CDDL HEADER END
20*25c28e83SPiotr Jasiukajtis  */
21*25c28e83SPiotr Jasiukajtis 
22*25c28e83SPiotr Jasiukajtis /*
23*25c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24*25c28e83SPiotr Jasiukajtis  */
25*25c28e83SPiotr Jasiukajtis /*
26*25c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*25c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
28*25c28e83SPiotr Jasiukajtis  */
29*25c28e83SPiotr Jasiukajtis 
30*25c28e83SPiotr Jasiukajtis /*
31*25c28e83SPiotr Jasiukajtis  * __k_cosl(long double x, long double y)
32*25c28e83SPiotr Jasiukajtis  * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
33*25c28e83SPiotr Jasiukajtis  * Input x is assumed to be bounded by ~pi/4 in magnitude.
34*25c28e83SPiotr Jasiukajtis  * Input y is the tail of x.
35*25c28e83SPiotr Jasiukajtis  *
36*25c28e83SPiotr Jasiukajtis  * Table look up algorithm
37*25c28e83SPiotr Jasiukajtis  *	1. by cos(-x) = cos(x), we may replace x by |x|
38*25c28e83SPiotr Jasiukajtis  *	2. if x < 25/128 = [0x3ffc4000, 0] = 0.15625 , then
39*25c28e83SPiotr Jasiukajtis  *	     if x < 2^-57 (hx < 0x3fc60000 0), return 1.0 with inexact if x !=  0
40*25c28e83SPiotr Jasiukajtis  *	     z = x*x;
41*25c28e83SPiotr Jasiukajtis  *	     if x <= 1/128 = 2**-7 = 0.0078125
42*25c28e83SPiotr Jasiukajtis  *		cos(x)=1.0+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
43*25c28e83SPiotr Jasiukajtis  *	     else
44*25c28e83SPiotr Jasiukajtis  *	        cos(x)=1.0+z*(q1+ ... z*q8)
45*25c28e83SPiotr Jasiukajtis  *	3. else
46*25c28e83SPiotr Jasiukajtis  *		ht = (hx + 0x400)&0x7ffff800	(round x to a break point t)
47*25c28e83SPiotr Jasiukajtis  *		lt = 0
48*25c28e83SPiotr Jasiukajtis  *		i  = (hy-0x3ffc4000)>>11;	(i<=64)
49*25c28e83SPiotr Jasiukajtis  *		x' = (x - t)+y 			(|x'| ~<= 2^-7
50*25c28e83SPiotr Jasiukajtis  *	   By
51*25c28e83SPiotr Jasiukajtis  *		cos(t+x')
52*25c28e83SPiotr Jasiukajtis  *		  = cos(t)cos(x')-sin(t)sin(x')
53*25c28e83SPiotr Jasiukajtis  *		  = cos(t)(1+z*(qq1+z*qq2))-[sin(t)]*x*(1+z*(pp1+z*pp2))
54*25c28e83SPiotr Jasiukajtis  *		  = cos(t) + [cos(t)]*(z*(qq1+z*qq2))-
55*25c28e83SPiotr Jasiukajtis  *				[sin(t)]*x*(1+z*(pp1+z*pp2))
56*25c28e83SPiotr Jasiukajtis  *
57*25c28e83SPiotr Jasiukajtis  *	   Thus,
58*25c28e83SPiotr Jasiukajtis  *		let a= _TBL_cos_hi[i], b = _TBL_cos_lo[i], c= _TBL_sin_hi[i],
59*25c28e83SPiotr Jasiukajtis  *		x = (x-t)+y
60*25c28e83SPiotr Jasiukajtis  *		z = x*x;
61*25c28e83SPiotr Jasiukajtis  *		cos(t+x) = a+(b+ (-c*x*(1+z*(pp1+z*pp2))+a*(z*(qq1+z*qq2)))
62*25c28e83SPiotr Jasiukajtis  */
63*25c28e83SPiotr Jasiukajtis 
64*25c28e83SPiotr Jasiukajtis #include "libm.h"
65*25c28e83SPiotr Jasiukajtis 
66*25c28e83SPiotr Jasiukajtis extern const long double _TBL_cosl_hi[], _TBL_cosl_lo[], _TBL_sinl_hi[];
67*25c28e83SPiotr Jasiukajtis static const long double
68*25c28e83SPiotr Jasiukajtis 	one	= 1.0L,
69*25c28e83SPiotr Jasiukajtis /*
70*25c28e83SPiotr Jasiukajtis  *                   3           11       -122.32
71*25c28e83SPiotr Jasiukajtis  * |sin(x) - (x+pp1*x +...+ pp5*x  )| <= 2        for |x|<1/64
72*25c28e83SPiotr Jasiukajtis  */
73*25c28e83SPiotr Jasiukajtis 	pp1	= -1.666666666666666666666666666586782940810e-0001L,
74*25c28e83SPiotr Jasiukajtis 	pp2	= +8.333333333333333333333003723660929317540e-0003L,
75*25c28e83SPiotr Jasiukajtis 	pp3	= -1.984126984126984076045903483778337804470e-0004L,
76*25c28e83SPiotr Jasiukajtis 	pp4	= +2.755731922361906641319723106210900949413e-0006L,
77*25c28e83SPiotr Jasiukajtis 	pp5	= -2.505198398570947019093998469135012057673e-0008L,
78*25c28e83SPiotr Jasiukajtis /*
79*25c28e83SPiotr Jasiukajtis  *		    2	         16       -117.11
80*25c28e83SPiotr Jasiukajtis  * |cos(x) - (1+q1*x + ... + q8*x  )| <= 2        for |x|<= 0.15625
81*25c28e83SPiotr Jasiukajtis  */
82*25c28e83SPiotr Jasiukajtis 	q1	= -4.999999999999999999999999999999756416975e-0001L,
83*25c28e83SPiotr Jasiukajtis 	q2	= +4.166666666666666666666666664006066577258e-0002L,
84*25c28e83SPiotr Jasiukajtis 	q3	= -1.388888888888888888888877700363937169637e-0003L,
85*25c28e83SPiotr Jasiukajtis 	q4	= +2.480158730158730158494468463031814083559e-0005L,
86*25c28e83SPiotr Jasiukajtis 	q5	= -2.755731922398586276322819250356005542871e-0007L,
87*25c28e83SPiotr Jasiukajtis 	q6	= +2.087675698767424261441959760729854017855e-0009L,
88*25c28e83SPiotr Jasiukajtis 	q7	= -1.147074481239662089072452129010790774761e-0011L,
89*25c28e83SPiotr Jasiukajtis 	q8	= +4.777761647399651599730663422263531034782e-0014L,
90*25c28e83SPiotr Jasiukajtis /*
91*25c28e83SPiotr Jasiukajtis  *		     2	         10       -123.84
92*25c28e83SPiotr Jasiukajtis  * |cos(x) - (1+qq1*x +...+ qq5*x  )| <= 2        for |x|<=1/128
93*25c28e83SPiotr Jasiukajtis  */
94*25c28e83SPiotr Jasiukajtis 	qq1	= -4.999999999999999999999999999999378373641e-0001L,
95*25c28e83SPiotr Jasiukajtis 	qq2	= +4.166666666666666666666665478399327703130e-0002L,
96*25c28e83SPiotr Jasiukajtis 	qq3	= -1.388888888888888888058211230618051613494e-0003L,
97*25c28e83SPiotr Jasiukajtis 	qq4	= +2.480158730156105377771585658905303111866e-0005L,
98*25c28e83SPiotr Jasiukajtis 	qq5	= -2.755728099762526325736488376695157008736e-0007L;
99*25c28e83SPiotr Jasiukajtis 
100*25c28e83SPiotr Jasiukajtis #define	i0	0
101*25c28e83SPiotr Jasiukajtis 
102*25c28e83SPiotr Jasiukajtis long double
__k_cosl(long double x,long double y)103*25c28e83SPiotr Jasiukajtis __k_cosl(long double x, long double y) {
104*25c28e83SPiotr Jasiukajtis 	long double a, t, z, w;
105*25c28e83SPiotr Jasiukajtis 	int *pt = (int *) &t, *px = (int *) &x;
106*25c28e83SPiotr Jasiukajtis 	int i, j, hx, ix;
107*25c28e83SPiotr Jasiukajtis 
108*25c28e83SPiotr Jasiukajtis 	t = 1.0L;
109*25c28e83SPiotr Jasiukajtis 	hx = px[i0];
110*25c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
111*25c28e83SPiotr Jasiukajtis 	if (ix < 0x3ffc4000) {
112*25c28e83SPiotr Jasiukajtis 		if (ix < 0x3fc60000)
113*25c28e83SPiotr Jasiukajtis 			if ((i = (int) x) == 0)
114*25c28e83SPiotr Jasiukajtis 				return (one);	/* generate inexact */
115*25c28e83SPiotr Jasiukajtis 		z = x * x;
116*25c28e83SPiotr Jasiukajtis 
117*25c28e83SPiotr Jasiukajtis 		if (ix < 0x3ff80000)	/* 0.0078125 */
118*25c28e83SPiotr Jasiukajtis 			return one + z * (qq1 + z * (qq2 + z * (qq3 +
119*25c28e83SPiotr Jasiukajtis 				z * (qq4 + z * qq5))));
120*25c28e83SPiotr Jasiukajtis 		else
121*25c28e83SPiotr Jasiukajtis 			return one + z * (q1 + z * (q2 + z * (q3 +
122*25c28e83SPiotr Jasiukajtis 				z * (q4 + z * (q5 + z * (q6 + z * (q7 +
123*25c28e83SPiotr Jasiukajtis 				z * q8)))))));
124*25c28e83SPiotr Jasiukajtis 	}
125*25c28e83SPiotr Jasiukajtis 	j = (ix + 0x400) & 0x7ffff800;
126*25c28e83SPiotr Jasiukajtis 	i = (j - 0x3ffc4000) >> 11;
127*25c28e83SPiotr Jasiukajtis 	pt[i0] = j;
128*25c28e83SPiotr Jasiukajtis 	if (hx > 0)
129*25c28e83SPiotr Jasiukajtis 		x = y - (t - x);
130*25c28e83SPiotr Jasiukajtis 	else
131*25c28e83SPiotr Jasiukajtis 		x = (-y) - (t + x);
132*25c28e83SPiotr Jasiukajtis 	a = _TBL_cosl_hi[i];
133*25c28e83SPiotr Jasiukajtis 	z = x * x;
134*25c28e83SPiotr Jasiukajtis 	t = z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))));
135*25c28e83SPiotr Jasiukajtis 	w = x * (one + z * (pp1 + z * (pp2 + z * (pp3 + z * (pp4 + z * pp5)))));
136*25c28e83SPiotr Jasiukajtis 	t = _TBL_cosl_lo[i] - (_TBL_sinl_hi[i] * w - a * t);
137*25c28e83SPiotr Jasiukajtis 	return (a + t);
138*25c28e83SPiotr Jasiukajtis }
139