1*0a6a1f1dSLionel Sambuc /* $NetBSD: n_sincos.c,v 1.7 2014/10/10 20:58:09 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1987, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)sincos.c 8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #define _LIBM_DECLARE
382fe8fb19SBen Gras #include "mathimpl.h"
392fe8fb19SBen Gras #include "trig.h"
402fe8fb19SBen Gras
41*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
42*0a6a1f1dSLionel Sambuc __weak_alias(_sinl, sin);
43*0a6a1f1dSLionel Sambuc __weak_alias(_cosl, cos);
44*0a6a1f1dSLionel Sambuc #endif
45*0a6a1f1dSLionel Sambuc
462fe8fb19SBen Gras double
sin(double x)472fe8fb19SBen Gras sin(double x)
482fe8fb19SBen Gras {
492fe8fb19SBen Gras double a,c,z;
502fe8fb19SBen Gras
512fe8fb19SBen Gras if(!finite(x)) /* sin(NaN) and sin(INF) must be NaN */
522fe8fb19SBen Gras return x-x;
532fe8fb19SBen Gras x=drem(x,PI2); /* reduce x into [-PI,PI] */
542fe8fb19SBen Gras a=copysign(x,__one);
552fe8fb19SBen Gras if (a >= PIo4) {
562fe8fb19SBen Gras if(a >= PI3o4) /* ... in [3PI/4,PI] */
572fe8fb19SBen Gras x = copysign((a = PI-a),x);
582fe8fb19SBen Gras else { /* ... in [PI/4,3PI/4] */
592fe8fb19SBen Gras a = PIo2-a; /* rtn. sign(x)*C(PI/2-|x|) */
602fe8fb19SBen Gras z = a*a;
612fe8fb19SBen Gras c = cos__C(z);
622fe8fb19SBen Gras z *= __half;
632fe8fb19SBen Gras a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
642fe8fb19SBen Gras return copysign(a,x);
652fe8fb19SBen Gras }
662fe8fb19SBen Gras }
672fe8fb19SBen Gras
682fe8fb19SBen Gras if (a < __small) { /* rtn. S(x) */
692fe8fb19SBen Gras z = __big+a;
702fe8fb19SBen Gras return x;
712fe8fb19SBen Gras }
722fe8fb19SBen Gras return x+x*sin__S(x*x);
732fe8fb19SBen Gras }
742fe8fb19SBen Gras
752fe8fb19SBen Gras float
sinf(float x)762fe8fb19SBen Gras sinf(float x)
772fe8fb19SBen Gras {
782fe8fb19SBen Gras return sin(x);
792fe8fb19SBen Gras }
802fe8fb19SBen Gras
812fe8fb19SBen Gras double
cos(double x)822fe8fb19SBen Gras cos(double x)
832fe8fb19SBen Gras {
842fe8fb19SBen Gras double a,c,z,s = 1.0;
852fe8fb19SBen Gras
862fe8fb19SBen Gras if(!finite(x)) /* cos(NaN) and cos(INF) must be NaN */
872fe8fb19SBen Gras return x-x;
882fe8fb19SBen Gras x=drem(x,PI2); /* reduce x into [-PI,PI] */
892fe8fb19SBen Gras a=copysign(x,__one);
902fe8fb19SBen Gras if (a >= PIo4) {
912fe8fb19SBen Gras if (a >= PI3o4) { /* ... in [3PI/4,PI] */
922fe8fb19SBen Gras a = PI-a;
932fe8fb19SBen Gras s = __negone;
942fe8fb19SBen Gras }
952fe8fb19SBen Gras else { /* ... in [PI/4,3PI/4] */
962fe8fb19SBen Gras a = PIo2-a;
972fe8fb19SBen Gras return a+a*sin__S(a*a); /* rtn. S(PI/2-|x|) */
982fe8fb19SBen Gras }
992fe8fb19SBen Gras }
1002fe8fb19SBen Gras if (a < __small) {
1012fe8fb19SBen Gras z = __big+a;
1022fe8fb19SBen Gras return s; /* rtn. s*C(a) */
1032fe8fb19SBen Gras }
1042fe8fb19SBen Gras z = a*a;
1052fe8fb19SBen Gras c = cos__C(z);
1062fe8fb19SBen Gras z *= __half;
1072fe8fb19SBen Gras a = (z >= thresh ? __half-((z-__half)-c) : __one-(z-c));
1082fe8fb19SBen Gras return copysign(a,s);
1092fe8fb19SBen Gras }
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras float
cosf(float x)1122fe8fb19SBen Gras cosf(float x)
1132fe8fb19SBen Gras {
1142fe8fb19SBen Gras return cos(x);
1152fe8fb19SBen Gras }
116