xref: /netbsd-src/lib/libm/noieee_src/n_sincos.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*	$NetBSD: n_sincos.c,v 1.10 2024/05/08 01:40:27 riastradh Exp $	*/
2 /*
3  * Copyright (c) 1987, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following 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  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef lint
32 #if 0
33 static char sccsid[] = "@(#)sincos.c	8.1 (Berkeley) 6/4/93";
34 #endif
35 #endif /* not lint */
36 
37 #define _LIBM_DECLARE
38 #include "namespace.h"
39 #include "mathimpl.h"
40 #include "trig.h"
41 
42 __weak_alias(sinl, _sinl)
43 __strong_alias(_sinl, _sin)
44 
45 __weak_alias(cosl, _cosl)
46 __strong_alias(_cosl, _cos)
47 
48 __weak_alias(sin, _sin)
49 double
50 sin(double x)
51 {
52 	double a,c,z;
53 
54         if(!finite(x))		/* sin(NaN) and sin(INF) must be NaN */
55 		return x-x;
56 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
57 	a=copysign(x,__one);
58 	if (a >= PIo4) {
59 		if(a >= PI3o4)		/* ... in [3PI/4,PI] */
60 			x = copysign((a = PI-a),x);
61 		else {			/* ... in [PI/4,3PI/4]  */
62 			a = PIo2-a;		/* rtn. sign(x)*C(PI/2-|x|) */
63 			z = a*a;
64 			c = cos__C(z);
65 			z *= __half;
66 			a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
67 			return copysign(a,x);
68 		}
69 	}
70 
71 	if (a < __small) {		/* rtn. S(x) */
72 		z = __big+a;
73 		return x;
74 	}
75 	return x+x*sin__S(x*x);
76 }
77 
78 __weak_alias(sinf, _sinf)
79 float
80 sinf(float x)
81 {
82 	return sin(x);
83 }
84 
85 __weak_alias(cos, _cos)
86 double
87 cos(double x)
88 {
89 	double a,c,z,s = 1.0;
90 
91 	if(!finite(x))		/* cos(NaN) and cos(INF) must be NaN */
92 		return x-x;
93 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
94 	a=copysign(x,__one);
95 	if (a >= PIo4) {
96 		if (a >= PI3o4) {	/* ... in [3PI/4,PI] */
97 			a = PI-a;
98 			s = __negone;
99 		}
100 		else {			/* ... in [PI/4,3PI/4] */
101 			a = PIo2-a;
102 			return a+a*sin__S(a*a);	/* rtn. S(PI/2-|x|) */
103 		}
104 	}
105 	if (a < __small) {
106 		z = __big+a;
107 		return s;		/* rtn. s*C(a) */
108 	}
109 	z = a*a;
110 	c = cos__C(z);
111 	z *= __half;
112 	a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
113 	return copysign(a,s);
114 }
115 
116 __weak_alias(cosf, _cosf)
117 float
118 cosf(float x)
119 {
120 	return cos(x);
121 }
122 
123 void
124 sincos(double x, double *s, double *c)
125 {
126 	*s = sin(x);
127 	*c = cos(x);
128 }
129 
130 void
131 sincosf(float x, float *s, float *c)
132 {
133 	*s = sinf(x);
134 	*c = cosf(x);
135 }
136