xref: /netbsd-src/lib/libm/noieee_src/n_sincos.c (revision acdb9f9a0775aee846230021ad100a64d65af296)
1 /*	$NetBSD: n_sincos.c,v 1.11 2024/07/16 14:53:17 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 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: n_sincos.c,v 1.11 2024/07/16 14:53:17 riastradh Exp $");
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)sincos.c	8.1 (Berkeley) 6/4/93";
37 #endif
38 #endif /* not lint */
39 
40 #define _LIBM_DECLARE
41 #include "namespace.h"
42 #include "mathimpl.h"
43 #include "trig.h"
44 
__weak_alias(sinl,_sinl)45 __weak_alias(sinl, _sinl)
46 __strong_alias(_sinl, _sin)
47 
48 __weak_alias(cosl, _cosl)
49 __strong_alias(_cosl, _cos)
50 
51 __weak_alias(sin, _sin)
52 double
53 sin(double x)
54 {
55 	double a,c,z;
56 
57         if(!finite(x))		/* sin(NaN) and sin(INF) must be NaN */
58 		return x-x;
59 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
60 	a=copysign(x,__one);
61 	if (a >= PIo4) {
62 		if(a >= PI3o4)		/* ... in [3PI/4,PI] */
63 			x = copysign((a = PI-a),x);
64 		else {			/* ... in [PI/4,3PI/4]  */
65 			a = PIo2-a;		/* rtn. sign(x)*C(PI/2-|x|) */
66 			z = a*a;
67 			c = cos__C(z);
68 			z *= __half;
69 			a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
70 			return copysign(a,x);
71 		}
72 	}
73 
74 	if (a < __small) {		/* rtn. S(x) */
75 		z = __big+a;
76 		return x;
77 	}
78 	return x+x*sin__S(x*x);
79 }
80 
__weak_alias(sinf,_sinf)81 __weak_alias(sinf, _sinf)
82 float
83 sinf(float x)
84 {
85 	return sin(x);
86 }
87 
__weak_alias(cos,_cos)88 __weak_alias(cos, _cos)
89 double
90 cos(double x)
91 {
92 	double a,c,z,s = 1.0;
93 
94 	if(!finite(x))		/* cos(NaN) and cos(INF) must be NaN */
95 		return x-x;
96 	x=drem(x,PI2);		/* reduce x into [-PI,PI] */
97 	a=copysign(x,__one);
98 	if (a >= PIo4) {
99 		if (a >= PI3o4) {	/* ... in [3PI/4,PI] */
100 			a = PI-a;
101 			s = __negone;
102 		}
103 		else {			/* ... in [PI/4,3PI/4] */
104 			a = PIo2-a;
105 			return a+a*sin__S(a*a);	/* rtn. S(PI/2-|x|) */
106 		}
107 	}
108 	if (a < __small) {
109 		z = __big+a;
110 		return s;		/* rtn. s*C(a) */
111 	}
112 	z = a*a;
113 	c = cos__C(z);
114 	z *= __half;
115 	a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
116 	return copysign(a,s);
117 }
118 
__weak_alias(cosf,_cosf)119 __weak_alias(cosf, _cosf)
120 float
121 cosf(float x)
122 {
123 	return cos(x);
124 }
125