Skip to content

Commit 0cf89fa

Browse files
committed
Add in sincos(), an efficient method of computing the sine and cosine of an angle together
1 parent 29af332 commit 0cf89fa

4 files changed

Lines changed: 388 additions & 44 deletions

File tree

src/Make.files

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
$(CUR_SRCS) = \
2-
e_acos.c e_acosf.c e_acosh.c e_acoshf.c e_asin.c e_asinf.c \
3-
e_atan2.c e_atan2f.c e_atanh.c e_atanhf.c e_cosh.c e_coshf.c e_exp.c \
4-
e_expf.c e_fmod.c e_fmodf.c e_gamma.c e_gamma_r.c e_gammaf.c \
5-
e_gammaf_r.c e_hypot.c e_hypotf.c e_j0.c e_j0f.c e_j1.c e_j1f.c \
6-
e_jn.c e_jnf.c e_lgamma.c e_lgamma_r.c e_lgammaf.c e_lgammaf_r.c \
7-
e_log.c e_log10.c e_log10f.c e_log2.c e_log2f.c e_logf.c \
8-
e_pow.c e_powf.c e_remainder.c e_remainderf.c e_scalb.c e_scalbf.c \
9-
e_rem_pio2.c e_rem_pio2f.c \
10-
e_sinh.c e_sinhf.c e_sqrt.c e_sqrtf.c \
11-
k_cos.c k_exp.c k_expf.c k_rem_pio2.c k_sin.c k_tan.c \
12-
k_cosf.c k_sinf.c k_tanf.c \
13-
s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_carg.c s_cargf.c s_cargl.c \
14-
s_cbrt.c s_cbrtf.c s_ceil.c s_ceilf.c \
15-
s_copysign.c s_copysignf.c s_cos.c s_cosf.c \
16-
s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \
17-
s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabs.c s_fabsf.c s_fdim.c \
18-
s_finite.c s_finitef.c \
19-
s_floor.c s_floorf.c s_fma.c s_fmaf.c \
20-
s_fmax.c s_fmaxf.c s_fmaxl.c s_fmin.c \
21-
s_fminf.c s_fminl.c s_fpclassify.c \
22-
s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.c \
23-
s_ilogbl.c s_isinf.c s_isfinite.c s_isnormal.c s_isnan.c \
24-
s_llrint.c s_llrintf.c s_llround.c s_llroundf.c s_llroundl.c \
25-
s_log1p.c s_log1pf.c s_logb.c s_logbf.c s_lrint.c s_lrintf.c \
26-
s_lround.c s_lroundf.c s_lroundl.c s_modf.c s_modff.c \
27-
s_nearbyint.c s_nextafter.c s_nextafterf.c \
28-
s_nexttowardf.c s_remquo.c s_remquof.c \
29-
s_rint.c s_rintf.c s_round.c s_roundf.c s_roundl.c \
30-
s_scalbln.c s_scalbn.c s_scalbnf.c s_signbit.c \
31-
s_signgam.c s_significand.c s_significandf.c s_sin.c s_sinf.c \
32-
s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_tgammaf.c s_trunc.c s_truncf.c \
33-
s_cpow.c s_cpowf.c s_cpowl.c \
34-
w_cabs.c w_cabsf.c w_drem.c w_dremf.c
2+
e_acos.c e_acosf.c e_acosh.c e_acoshf.c e_asin.c e_asinf.c \
3+
e_atan2.c e_atan2f.c e_atanh.c e_atanhf.c e_cosh.c e_coshf.c e_exp.c \
4+
e_expf.c e_fmod.c e_fmodf.c e_gamma.c e_gamma_r.c e_gammaf.c \
5+
e_gammaf_r.c e_hypot.c e_hypotf.c e_j0.c e_j0f.c e_j1.c e_j1f.c \
6+
e_jn.c e_jnf.c e_lgamma.c e_lgamma_r.c e_lgammaf.c e_lgammaf_r.c \
7+
e_log.c e_log10.c e_log10f.c e_log2.c e_log2f.c e_logf.c \
8+
e_pow.c e_powf.c e_remainder.c e_remainderf.c e_scalb.c e_scalbf.c \
9+
e_rem_pio2.c e_rem_pio2f.c \
10+
e_sinh.c e_sinhf.c e_sqrt.c e_sqrtf.c \
11+
k_cos.c k_exp.c k_expf.c k_rem_pio2.c k_sin.c k_tan.c \
12+
k_cosf.c k_sinf.c k_tanf.c \
13+
s_asinh.c s_asinhf.c s_atan.c s_atanf.c s_carg.c s_cargf.c s_cargl.c \
14+
s_cbrt.c s_cbrtf.c s_ceil.c s_ceilf.c \
15+
s_copysign.c s_copysignf.c s_cos.c s_cosf.c \
16+
s_csqrt.c s_csqrtf.c s_erf.c s_erff.c \
17+
s_exp2.c s_exp2f.c s_expm1.c s_expm1f.c s_fabs.c s_fabsf.c s_fdim.c \
18+
s_finite.c s_finitef.c \
19+
s_floor.c s_floorf.c s_fma.c s_fmaf.c \
20+
s_fmax.c s_fmaxf.c s_fmaxl.c s_fmin.c \
21+
s_fminf.c s_fminl.c s_fpclassify.c \
22+
s_frexp.c s_frexpf.c s_ilogb.c s_ilogbf.c \
23+
s_ilogbl.c s_isinf.c s_isfinite.c s_isnormal.c s_isnan.c \
24+
s_llrint.c s_llrintf.c s_llround.c s_llroundf.c s_llroundl.c \
25+
s_log1p.c s_log1pf.c s_logb.c s_logbf.c s_lrint.c s_lrintf.c \
26+
s_lround.c s_lroundf.c s_lroundl.c s_modf.c s_modff.c \
27+
s_nearbyint.c s_nextafter.c s_nextafterf.c \
28+
s_nexttowardf.c s_remquo.c s_remquof.c \
29+
s_rint.c s_rintf.c s_round.c s_roundf.c s_roundl.c \
30+
s_scalbln.c s_scalbn.c s_scalbnf.c s_signbit.c \
31+
s_signgam.c s_significand.c s_significandf.c s_sin.c s_sincos.c \
32+
s_sinf.c s_sincosf.c s_tan.c s_tanf.c s_tanh.c s_tanhf.c s_tgammaf.c \
33+
s_trunc.c s_truncf.c s_cpow.c s_cpowf.c s_cpowl.c \
34+
w_cabs.c w_cabsf.c w_drem.c w_dremf.c
3535

3636
ifneq ($(OS), WINNT)
3737
$(CUR_SRCS) += s_nan.c
@@ -42,18 +42,18 @@ $(CUR_SRCS) += s_copysignl.c s_fabsl.c s_llrintl.c s_lrintl.c s_modfl.c
4242

4343
# If long double != double use these; otherwise, we alias the double versions.
4444
$(CUR_SRCS) += e_acosl.c e_asinl.c e_atan2l.c e_fmodl.c \
45-
e_hypotl.c e_remainderl.c e_sqrtl.c \
46-
s_atanl.c s_ceill.c s_cosl.c s_cprojl.c \
47-
s_csqrtl.c s_floorl.c s_fmal.c \
48-
s_frexpl.c s_logbl.c s_nexttoward.c \
49-
s_remquol.c \
50-
s_sinl.c s_tanl.c s_truncl.c w_cabsl.c \
51-
s_nextafterl.c s_rintl.c s_scalbnl.c
45+
e_hypotl.c e_remainderl.c e_sqrtl.c \
46+
s_atanl.c s_ceill.c s_cosl.c s_cprojl.c \
47+
s_csqrtl.c s_floorl.c s_fmal.c \
48+
s_frexpl.c s_logbl.c s_nexttoward.c \
49+
s_remquol.c \
50+
s_sinl.c s_sincosl.c s_tanl.c s_truncl.c w_cabsl.c \
51+
s_nextafterl.c s_rintl.c s_scalbnl.c
5252
# s_cbrtl.c
5353

5454
# C99 complex functions
5555
$(CUR_SRCS) += s_ccosh.c s_ccoshf.c s_cexp.c s_cexpf.c \
56-
s_cimag.c s_cimagf.c s_cimagl.c \
57-
s_conj.c s_conjf.c s_conjl.c \
58-
s_cproj.c s_cprojf.c s_creal.c s_crealf.c s_creall.c \
59-
s_csinh.c s_csinhf.c s_ctanh.c s_ctanhf.c
56+
s_cimag.c s_cimagf.c s_cimagl.c \
57+
s_conj.c s_conjf.c s_conjl.c \
58+
s_cproj.c s_cprojf.c s_creal.c s_crealf.c s_creall.c \
59+
s_csinh.c s_csinhf.c s_ctanh.c s_ctanhf.c

src/s_sincos.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/* @(#)s_sincos.c 5.1 13/07/15 */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 2013 Elliot Saba. All rights reserved.
5+
*
6+
* Developed at the University of Washington.
7+
* Permission to use, copy, modify, and distribute this
8+
* software is freely granted, provided that this notice
9+
* is preserved.
10+
* ====================================================
11+
*/
12+
#include "cdefs-compat.h"
13+
14+
/* sincos(x, s, c)
15+
* Several applications need sine and cosine of the same
16+
* angle x. This function computes both at the same time,
17+
* and stores the results in *sin and *cos.
18+
*
19+
* kernel function:
20+
* __kernel_sin ... sine function on [-pi/4,pi/4]
21+
* __kernel_cos ... cose function on [-pi/4,pi/4]
22+
* __ieee754_rem_pio2 ... argument reduction routine
23+
*
24+
* Method.
25+
* Borrow liberally from s_sin.c and s_cos.c, merging
26+
* efforts where applicable and returning their values in
27+
* appropriate variables, thereby slightly reducing the
28+
* amount of work relative to just calling sin/cos(x)
29+
* separately
30+
*
31+
* Special cases:
32+
* Let trig be any of sin, cos, or tan.
33+
* sincos(+-INF, s, c) is NaN, with signals;
34+
* sincos(NaN, s, c) is that NaN;
35+
*/
36+
37+
#include <float.h>
38+
39+
#include "openlibm.h"
40+
//#define INLINE_REM_PIO2
41+
#include "math_private.h"
42+
//#include "e_rem_pio2.c"
43+
44+
/* Constants used in polynomial approximation of sin/cos */
45+
static const double
46+
one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
47+
half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
48+
S1 = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
49+
S2 = 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
50+
S3 = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
51+
S4 = 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
52+
S5 = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
53+
S6 = 1.58969099521155010221e-10, /* 0x3DE5D93A, 0x5ACFD57C */
54+
C1 = 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
55+
C2 = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
56+
C3 = 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
57+
C4 = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
58+
C5 = 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
59+
C6 = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
60+
61+
void
62+
__kernel_sincos( double x, double y, int iy, double * k_s, double * k_c )
63+
{
64+
/* Inline calculation of sin/cos, as we can save
65+
some work, and we will always need to calculate
66+
both values, no matter the result of switch */
67+
double z, w, r, v, hz;
68+
z = x*x;
69+
w = z*z;
70+
71+
/* cos-specific computation; equivalent to calling
72+
__kernel_cos(x,y) and storing in k_c*/
73+
r = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6));
74+
hz = 0.5*z;
75+
v = one-hz;
76+
77+
*k_c = v + (((one-v)-hz) + (z*r-x*y));
78+
79+
/* sin-specific computation; equivalent to calling
80+
__kernel_sin(x,y,1) and storing in k_s*/
81+
r = S2+z*(S3+z*S4) + z*w*(S5+z*S6);
82+
v = z*x;
83+
if(iy == 0)
84+
*k_s = x+v*(S1+z*r);
85+
else
86+
*k_s = x-((z*(half*y-v*r)-y)-v*S1);
87+
}
88+
89+
void
90+
sincos(double x, double * s, double * c)
91+
{
92+
double y[2];
93+
int32_t ix;
94+
95+
/* Store high word of x in ix */
96+
GET_HIGH_WORD(ix,x);
97+
98+
/* |x| ~< pi/4 */
99+
ix &= 0x7fffffff;
100+
if(ix <= 0x3fe921fb) {
101+
/* Check for small x for sin and cos */
102+
if(ix<0x3e46a09e) {
103+
/* Check for exact zero */
104+
if( (int)x==0 ) {
105+
*s = x;
106+
*c = 1.0;
107+
return;
108+
}
109+
}
110+
/* Call kernel function with 0 extra */
111+
__kernel_sincos(x,0.0,0, s, c);
112+
} else if( ix >= 0x7ff00000 ) {
113+
/* sincos(Inf or NaN) is NaN */
114+
*s = x-x;
115+
*c = x-x;
116+
}
117+
118+
/*argument reduction needed*/
119+
else {
120+
double k_c, k_s;
121+
printf( "bleh?\n");
122+
123+
/* Calculate remainer, then sub out to kernel */
124+
int32_t n = __ieee754_rem_pio2(x,y);
125+
__kernel_sincos( y[0], y[1], 1, &k_s, &k_c );
126+
127+
/* Figure out permutation of sin/cos outputs to true outputs */
128+
switch(n&3) {
129+
case 0:
130+
*c = k_c;
131+
*s = k_s;
132+
break;
133+
case 1:
134+
*c = -k_s;
135+
*s = k_c;
136+
break;
137+
case 2:
138+
*c = -k_c;
139+
*s = -k_s;
140+
break;
141+
default:
142+
*c = k_s;
143+
*s = -k_c;
144+
break;
145+
}
146+
}
147+
}
148+
149+
#if (LDBL_MANT_DIG == 53)
150+
__weak_reference(sincos, sincosl);
151+
#endif

0 commit comments

Comments
 (0)