diff options
author | nsz <nsz@port70.net> | 2012-03-15 08:17:28 +0100 |
---|---|---|
committer | nsz <nsz@port70.net> | 2012-03-15 08:17:28 +0100 |
commit | 0144b45b71c0b78055b311fe3e7408fee71eb0c1 (patch) | |
tree | 9c7733fd340158aa506ad9b852c16fdd4bcafb5b /src/math/sincos.c | |
parent | 32ca5ef3ff3069bdaae5f95be1900a3c3f831247 (diff) | |
download | musl-0144b45b71c0b78055b311fe3e7408fee71eb0c1.tar.gz musl-0144b45b71c0b78055b311fe3e7408fee71eb0c1.tar.xz musl-0144b45b71c0b78055b311fe3e7408fee71eb0c1.zip |
efficient sincos based on sin and cos
Diffstat (limited to 'src/math/sincos.c')
-rw-r--r-- | src/math/sincos.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/math/sincos.c b/src/math/sincos.c new file mode 100644 index 00000000..442e285e --- /dev/null +++ b/src/math/sincos.c @@ -0,0 +1,68 @@ +/* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "libm.h" + +void sincos(double x, double *sin, double *cos) +{ + double y[2], s, c; + uint32_t n, ix; + + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + + /* |x| ~< pi/4 */ + if (ix <= 0x3fe921fb) { + /* if |x| < 2**-27 * sqrt(2) */ + if (ix < 0x3e46a09e) { + /* raise inexact if x != 0 */ + if ((int)x == 0) { + *sin = x; + *cos = 1.0; + } + return; + } + *sin = __sin(x, 0.0, 0); + *cos = __cos(x, 0.0); + return; + } + + /* sincos(Inf or NaN) is NaN */ + if (ix >= 0x7ff00000) { + *sin = *cos = x - x; + return; + } + + /* argument reduction needed */ + n = __rem_pio2(x, y); + s = __sin(y[0], y[1], 1); + c = __cos(y[0], y[1]); + switch (n&3) { + case 0: + *sin = s; + *cos = c; + break; + case 1: + *sin = c; + *cos = -s; + break; + case 2: + *sin = -s; + *cos = -c; + break; + case 3: + default: + *sin = -c; + *cos = s; + break; + } +} |