Skip to content

Instantly share code, notes, and snippets.

@orlp
Created March 5, 2012 12:09
Show Gist options
  • Select an option

  • Save orlp/1978092 to your computer and use it in GitHub Desktop.

Select an option

Save orlp/1978092 to your computer and use it in GitHub Desktop.
Ultra fast IEEE754 double-precision mode sin function
double cc_sin(double x) {
const double a = 0.00735246819687011731341356165096815;
const double b = -0.16528911397014738207016302002888890;
const double c = 0.99969198629596757779830113868360584;
double x2;
union {
double d;
long i;
} dtoi_hack;
/* find offset of x from the range -pi to pi */
dtoi_hack.d = CC_1_PI * x + 103079215104.5;
dtoi_hack.i >>= 16;
x -= dtoi_hack.i * CC_PI;
x2 = x*x;
x *= (c + x2*(b + a*x2));
/* if x is in an odd pi count we must flip */
x -= (2 * (dtoi_hack.i & 1)) * x; /* trick for r = (k % 2) == 0 ? r : -r; */
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment