blob: 0259bdecb0bf03933d4a890052573a9a76ece68d [file] [log] [blame]
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -07001/*-
2 * Copyright (c) 2010 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27#define _GNU_SOURCE 1
28#include <math.h>
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005929#define INLINE_KERNEL_COSDF
30#define INLINE_KERNEL_SINDF
31#include "src/math_private.h"
32#include "src/k_cosf.c"
33#include "src/k_sinf.c"
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -070034
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005935/* Small multiples of pi/2 rounded to double precision. */
36static const double
37s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
38s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
39s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
40s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
Jing Yud50225a2011-08-16 16:14:34 -070041
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005942/* For implementation details, see src/s_sin.c, src/s_cos.c */
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -070043void sincos(double x, double *psin, double *pcos)
44{
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005945 double y[2], z=0.0;
46 int32_t n, ix;
47
48 /* High word of x. */
49 GET_HIGH_WORD(ix, x);
50
51 /* |x| ~< pi/4 */
52 ix &= 0x7fffffff;
53 if(ix <= 0x3fe921fb) {
54 if(ix < 0x3e400000) { /* \x\ < 2**-27 */
55 if((int)x==0) { /* generate inexact */
56 *psin = x;
57 *pcos = 1.0;
58 return;
59 }
60 }
61 *psin = __kernel_sin(x, z, 0);
62 *pcos = __kernel_cos(x, z);
63 return;
64 } else if(ix>=0x7ff00000) { /* sin(Inf or NaN) and cos(Inf or NaN) is NaN */
65 *psin = *pcos = x-x;
66 return;
67 } else {
68 n = __ieee754_rem_pio2(x, y);
69 switch(n&3) {
70 case 0:
71 *psin = __kernel_sin(y[0],y[1],1);
72 *pcos = __kernel_cos(y[0],y[1]);
73 return;
74 case 1:
75 *psin = __kernel_cos(y[0],y[1]);
76 *pcos = -__kernel_sin(y[0],y[1],1);
77 return;
78 case 2:
79 *psin = -__kernel_sin(y[0],y[1],1);
80 *pcos = -__kernel_cos(y[0],y[1]);
81 return;
82 default:
83 *psin = -__kernel_cos(y[0],y[1]);
84 *pcos = __kernel_sin(y[0],y[1],1);
85 return;
86 }
87 }
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -070088}
89
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005990/* For implementation details, see src/s_sinf.c, src/s_cosf.c */
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -070091void sincosf(float x, float *psin, float *pcos)
92{
Bernhard Rosenkraenzer0c544152011-12-27 02:05:35 +005993 float y[2];
94 int32_t n, hx, ix;
95
96 GET_FLOAT_WORD(hx, x);
97 ix = hx & 0x7fffffff;
98
99 if(ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
100 if(ix < 0x39800000) { /* |x| < 2**-12 */
101 if(((int)x)==0) { /* x with inexact if x != 0 */
102 *psin = x;
103 *pcos = 1.0;
104 return;
105 }
106 }
107 *psin = __kernel_sindf(x);
108 *pcos = __kernel_cosdf(x);
109 return;
110 } else if(ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */
111 if(ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
112 if(hx>0) {
113 *psin = __kernel_cosdf(x - s1pio2);
114 *pcos = __kernel_sindf(s1pio2 - x);
115 return;
116 } else {
117 *psin = -__kernel_cosdf(x + s1pio2);
118 *pcos = __kernel_sindf(x + s1pio2);
119 return;
120 }
121 } else {
122 *psin = __kernel_sindf((hx > 0 ? s2pio2 : -s2pio2) - x);
123 *pcos = -__kernel_cosdf(x + (hx > 0 ? -s2pio2 : s2pio2));
124 return;
125 }
126 } else if(ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */
127 if(ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
128 if(hx>0) {
129 *psin = -__kernel_cosdf(x - s3pio2);
130 *pcos = __kernel_sindf(x - s3pio2);
131 return;
132 } else {
133 *psin = __kernel_cosdf(x + s3pio2);
134 *pcos = __kernel_sindf(-s3pio2 - x);
135 return;
136 }
137 } else {
138 *psin = __kernel_sindf(x + (hx > 0 ? -s4pio2 : s4pio2));
139 *pcos = __kernel_cosdf(x + (hx > 0 ? -s4pio2 : s4pio2));
140 return;
141 }
142 } else if(ix>=0x7f800000) { /* sin and cos (Inf or NaN) is NaN */
143 *psin = *pcos = x-x;
144 return;
145 } else {
146 n = __ieee754_rem_pio2f(x,y);
147 switch(n&3) {
148 case 0:
149 *psin = __kernel_sindf((double)y[0]+y[1]);
150 *pcos = __kernel_cosdf((double)y[0]+y[1]);
151 return;
152 case 1:
153 *psin = __kernel_cosdf((double)y[0]+y[1]);
154 *pcos = __kernel_sindf(-(double)y[0]-y[1]);
155 return;
156 case 2:
157 *psin = __kernel_sindf(-(double)y[0]-y[1]);
158 *pcos = -__kernel_cosdf((double)y[0]+y[1]);
159 return;
160 default:
161 *psin = -__kernel_cosdf((double)y[0]+y[1]);
162 *pcos = __kernel_sindf((double)y[0]+y[1]);
163 return;
164 }
165 }
David 'Digit' Turnerddd235b2010-05-19 11:39:16 -0700166}
Jing Yu99467502010-06-03 14:05:51 -0700167
168void sincosl(long double x, long double *psin, long double *pcos)
169{
170 *psin = sin(x);
171 *pcos = cos(x);
172}