daily pastebin goal
50%
SHARE
TWEET

Untitled

a guest Apr 25th, 2017 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Some externally defined stuff
  2. //#define SCALE (sizeof(BASE) * 8)
  3. //#define RADIX 16
  4. //#define BASIS ((BASE)1 << RADIX)
  5. //#define PI (BASE)(3.1415926536897932384626 * BASIS)
  6. //typedef Sclr int64_t;
  7. //typedef Angl int64_t;
  8.  
  9. Sclr
  10. engineer_math_sin(Angl input)
  11. {
  12.    // A sine approximation via a fifth-order polynomial.
  13.    int64_t c, x, x2, o;
  14.    static const int64_t
  15.       A  = PI/2,                        // PI/2
  16.       B  = PI - (5 * BASIS)/2,          // PI - 5/2
  17.       C  = (PI - 3 * BASIS)/2,          // (PI - 3)/2
  18.       S  = (BASIS << (RADIX + 1)) / PI; // 2/PI
  19.  
  20.     x = MULT(input, S);
  21.  
  22.     x = x << (62 - RADIX);         // Shift to full s64 range (Q16->Q62)
  23.     if( (x ^ (x << 1)) < 0)        // Test for quadrant 1 or 2
  24.        x = ((int64_t)1 << 63) - x;
  25.     x = x >> (62 - RADIX);
  26.  
  27.     x2 =     MULT(x, x);   // Ax - Bx^3 + Cx^5
  28.     o  = B - MULT(C, x2);  // Ax - x(Bx^2 - Cx^4)
  29.     o  = A - MULT(o, x2);  // x(A - (Bx^2 - Cx^4)
  30.     o  =     MULT(o, x);   // x(A - x^2(B - Cx^2))
  31.  
  32.     return o;
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top