xref: /illumos-gate/usr/src/lib/libm/common/LD/cosl.c (revision d8e10381a0083d7717710b0db7e64707bc0f3ff8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma weak __cosl = cosl
31 
32 /* INDENT OFF */
33 /* cosl(x)
34  * Table look-up algorithm by K.C. Ng, November, 1989.
35  *
36  * kernel function:
37  *	__k_sinl	... sin function on [-pi/4,pi/4]
38  *	__k_cosl	... cos function on [-pi/4,pi/4]
39  *	__rem_pio2l	... argument reduction routine
40  *
41  * Method.
42  *      Let S and C denote the sin and cos respectively on [-PI/4, +PI/4].
43  *      1. Assume the argument x is reduced to y1+y2 = x-k*pi/2 in
44  *	   [-pi/2 , +pi/2], and let n = k mod 4.
45  *	2. Let S=S(y1+y2), C=C(y1+y2). Depending on n, we have
46  *
47  *          n        sin(x)      cos(x)        tan(x)
48  *     ----------------------------------------------------------
49  *	    0	       S	   C		 S/C
50  *	    1	       C	  -S		-C/S
51  *	    2	      -S	  -C		 S/C
52  *	    3	      -C	   S		-C/S
53  *     ----------------------------------------------------------
54  *
55  * Special cases:
56  *      Let trig be any of sin, cos, or tan.
57  *      trig(+-INF)  is NaN, with signals;
58  *      trig(NaN)    is that NaN;
59  *
60  * Accuracy:
61  *	computer TRIG(x) returns trig(x) nearly rounded.
62  */
63 /* INDENT ON */
64 
65 #include "libm.h"
66 #include "longdouble.h"
67 
68 #include <sys/isa_defs.h>
69 
70 long double
71 cosl(long double x) {
72 	long double y[2], z = 0.0L;
73 	int n, ix;
74 	int *px = (int *) &x;
75 
76 	/* trig(Inf or NaN) is NaN */
77 	if (!finitel(x))
78 		return x - x;
79 
80 	/* High word of x. */
81 #if defined(__i386) || defined(__amd64)
82 	XTOI(px, ix);
83 #else
84 	ix = px[0];
85 #endif
86 
87 	/* |x| ~< pi/4 */
88 	ix &= 0x7fffffff;
89 	if (ix <= 0x3ffe9220)
90 		return __k_cosl(x, z);
91 
92 	/* argument reduction needed */
93 	else {
94 		n = __rem_pio2l(x, y);
95 		switch (n & 3) {
96 		case 0:
97 			return __k_cosl(y[0], y[1]);
98 		case 1:
99 			return -__k_sinl(y[0], y[1]);
100 		case 2:
101 			return -__k_cosl(y[0], y[1]);
102 		case 3:
103 			return __k_sinl(y[0], y[1]);
104 		/* NOTREACHED */
105 		}
106 	}
107     return 0.0L;
108 }
109