Advertisement
Guest User

Untitled

a guest
Feb 6th, 2020
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.53 KB | None | 0 0
  1. //
  2. //    FILE: __functions.h
  3. //
  4. //    fast and approximate transcendental functions
  5. //
  6. //    copyright (c) 2004  Robert Bristow-Johnson
  7. //
  8. //
  9.  
  10.  
  11. #ifndef __FUNCTIONS_H
  12. #define __FUNCTIONS_H
  13.  
  14. #define TINY 1.0e-8
  15. #define HUGE 1.0e8
  16.  
  17. #define PI              (3.1415926535897932384626433832795028841972)        /* pi */
  18. #define ONE_OVER_PI     (0.3183098861837906661338147750939)
  19. #define TWOPI           (6.2831853071795864769252867665590057683943)        /* 2*pi */
  20. #define ONE_OVER_TWOPI  (0.15915494309189535682609381638)
  21. #define PI_2            (1.5707963267948966192313216916397514420986)        /* pi/2 */
  22. #define TWO_OVER_PI     (0.636619772367581332267629550188)
  23. #define LN2             (0.6931471805599453094172321214581765680755)        /* ln(2) */
  24. #define ONE_OVER_LN2    (1.44269504088896333066907387547)
  25. #define LN10            (2.3025850929940456840179914546843642076011)        /* ln(10) */
  26. #define ONE_OVER_LN10   (0.43429448190325177635683940025)
  27. #define ROOT2           (1.4142135623730950488016887242096980785697)        /* sqrt(2) */
  28. #define ONE_OVER_ROOT2  (0.707106781186547438494264988549)
  29.  
  30. #define DB_LOG2_ENERGY          (3.01029995663981154631945610163)           /* dB = DB_LOG2_ENERGY*__log2(energy) */
  31. #define DB_LOG2_AMPL            (6.02059991327962309263891220326)           /* dB = DB_LOG2_AMPL*__log2(amplitude) */
  32. #define ONE_OVER_DB_LOG2_AMPL   (0.16609640474436811218256075335)           /* amplitude = __exp2(ONE_OVER_DB_LOG2_AMPL*dB) */
  33.  
  34. #define LONG_OFFSET     4096L
  35. #define FLOAT_OFFSET    4096.0
  36.  
  37.  
  38.  
  39. float   __sqrt(float x);
  40.  
  41. float   __log2(float x);
  42. float   __exp2(float x);
  43.  
  44. float   __log(float x);
  45. float   __exp(float x);
  46.  
  47. float   __pow(float x, float y);
  48.  
  49. float   __sin_pi(float x);
  50. float   __cos_pi(float x);
  51.  
  52. float   __sin(float x);
  53. float   __cos(float x);
  54. float   __tan(float x);
  55.  
  56. float   __atan(float x);
  57. float   __asin(float x);
  58. float   __acos(float x);
  59.  
  60. float   __arg(float Imag, float Real);
  61.  
  62. float   __poly(float *a, int order, float x);
  63. float   __map(float *f, float scaler, float x);
  64. float   __discreteMap(float *f, float scaler, float x);
  65.  
  66. unsigned long __random();
  67.  
  68. #endif
  69.  
  70.  
  71.  
  72.  
  73. //
  74. //    FILE: __functions.c
  75. //
  76. //    fast and approximate transcendental functions
  77. //
  78. //    copyright (c) 2004  Robert Bristow-Johnson
  79. //
  80. //
  81.  
  82. #define STD_MATH_LIB 0
  83.  
  84. #include "__functions.h"
  85.  
  86. #if STD_MATH_LIB
  87. #include "math.h"   // angle brackets don't work with SE markup
  88. #endif
  89.  
  90.  
  91.  
  92.  
  93. float   __sqrt(register float x)
  94. {
  95. #if STD_MATH_LIB
  96.     return (float)sqrt((double)x);
  97. #else
  98.     if (x > 5.877471754e-39)
  99.     {
  100.         register float accumulator, xPower;
  101.         register long intPart;
  102.         register union { float f; long i; } xBits;
  103.  
  104.         xBits.f = x;
  105.  
  106.         intPart = ((xBits.i) >> 23);                  /* get biased exponent */
  107.         intPart -= 127;                             /* unbias it */
  108.  
  109.         x = (float)(xBits.i & 0x007FFFFF);          /* mask off exponent leaving 0x800000*(mantissa - 1) */
  110.         x *= 1.192092895507812e-07;                 /* divide by 0x800000 */
  111.  
  112.         accumulator = 1.0 + 0.49959804148061*x;
  113.         xPower = x * x;
  114.         accumulator += -0.12047308243453*xPower;
  115.         xPower *= x;
  116.         accumulator += 0.04585425015501*xPower;
  117.         xPower *= x;
  118.         accumulator += -0.01076564682800*xPower;
  119.  
  120.         if (intPart & 0x00000001)
  121.         {
  122.             accumulator *= ROOT2;                   /* an odd input exponent means an extra sqrt(2) in the output */
  123.         }
  124.  
  125.         xBits.i = intPart >> 1;                     /* divide exponent by 2, lose LSB */
  126.         xBits.i += 127;                             /* rebias exponent */
  127.         xBits.i <<= 23;                             /* move biased exponent into exponent bits */
  128.  
  129.         return accumulator * xBits.f;
  130.     }
  131.     else
  132.     {
  133.         return 0.0;
  134.     }
  135. #endif
  136. }
  137.  
  138.  
  139.  
  140.  
  141. float   __log2(register float x)
  142. {
  143. #if STD_MATH_LIB
  144.     return (float)(ONE_OVER_LN2*log((double)x));
  145. #else
  146.     if (x > 5.877471754e-39)
  147.     {
  148.         register float accumulator, xPower;
  149.         register long intPart;
  150.  
  151.         register union { float f; long i; } xBits;
  152.  
  153.         xBits.f = x;
  154.  
  155.         intPart = ((xBits.i) >> 23);                  /* get biased exponent */
  156.         intPart -= 127;                             /* unbias it */
  157.  
  158.         x = (float)(xBits.i & 0x007FFFFF);          /* mask off exponent leaving 0x800000*(mantissa - 1) */
  159.         x *= 1.192092895507812e-07;                 /* divide by 0x800000 */
  160.  
  161.         accumulator = 1.44254494359510*x;
  162.         xPower = x * x;
  163.         accumulator += -0.71814525675041*xPower;
  164.         xPower *= x;
  165.         accumulator += 0.45754919692582*xPower;
  166.         xPower *= x;
  167.         accumulator += -0.27790534462866*xPower;
  168.         xPower *= x;
  169.         accumulator += 0.12179791068782*xPower;
  170.         xPower *= x;
  171.         accumulator += -0.02584144982967*xPower;
  172.  
  173.         return accumulator + (float)intPart;
  174.     }
  175.     else
  176.     {
  177.         return -HUGE;
  178.     }
  179. #endif
  180. }
  181.  
  182.  
  183. float   __exp2(register float x)
  184. {
  185. #if STD_MATH_LIB
  186.     return (float)exp(LN2*(double)x);
  187. #else
  188.     if (x >= -127.0)
  189.     {
  190.         register float accumulator, xPower;
  191.         register union { float f; long i; } xBits;
  192.  
  193.         xBits.i = (long)(x + FLOAT_OFFSET) - LONG_OFFSET;       /* integer part */
  194.         x -= (float)(xBits.i);                                  /* fractional part */
  195.  
  196.         accumulator = 1.0 + 0.69303212081966*x;
  197.         xPower = x * x;
  198.         accumulator += 0.24137976293709*xPower;
  199.         xPower *= x;
  200.         accumulator += 0.05203236900844*xPower;
  201.         xPower *= x;
  202.         accumulator += 0.01355574723481*xPower;
  203.  
  204.         xBits.i += 127;                                         /* bias integer part */
  205.         xBits.i <<= 23;                                         /* move biased int part into exponent bits */
  206.  
  207.         return accumulator * xBits.f;
  208.     }
  209.     else
  210.     {
  211.         return 0.0;
  212.     }
  213. #endif
  214. }
  215.  
  216.  
  217. float   __log(register float x)
  218. {
  219. #if STD_MATH_LIB
  220.     return (float)log((double)x);
  221. #else
  222.     return LN2 * __log2(x);
  223. #endif
  224. }
  225.  
  226. float   __exp(register float x)
  227. {
  228. #if STD_MATH_LIB
  229.     return (float)exp((double)x);
  230. #else
  231.     return __exp2(ONE_OVER_LN2*x);
  232. #endif
  233. }
  234.  
  235. float   __pow(float x, float y)
  236. {
  237. #if STD_MATH_LIB
  238.     return (float)pow((double)x, (double)y);
  239. #else
  240.     return __exp2(y*__log2(x));
  241. #endif
  242. }
  243.  
  244.  
  245.  
  246.  
  247. float   __sin_pi(register float x)
  248. {
  249. #if STD_MATH_LIB
  250.     return (float)sin(PI*(double)x);
  251. #else
  252.     register float accumulator, xPower, xSquared;
  253.  
  254.     register long evenIntPart = ((long)(0.5*x + 1024.5) - 1024) << 1;
  255.     x -= (float)evenIntPart;
  256.  
  257.     xSquared = x * x;
  258.     accumulator = 3.14159265358979*x;
  259.     xPower = xSquared * x;
  260.     accumulator += -5.16731953364340*xPower;
  261.     xPower *= xSquared;
  262.     accumulator += 2.54620566822659*xPower;
  263.     xPower *= xSquared;
  264.     accumulator += -0.586027023087261*xPower;
  265.     xPower *= xSquared;
  266.     accumulator += 0.06554823491427*xPower;
  267.  
  268.     return accumulator;
  269. #endif
  270. }
  271.  
  272.  
  273. float   __cos_pi(register float x)
  274. {
  275. #if STD_MATH_LIB
  276.     return (float)cos(PI*(double)x);
  277. #else
  278.     register float accumulator, xPower, xSquared;
  279.  
  280.     register long evenIntPart = ((long)(0.5*x + 1024.5) - 1024) << 1;
  281.     x -= (float)evenIntPart;
  282.  
  283.     xSquared = x * x;
  284.     accumulator = 1.57079632679490*x;                       /* series for sin(PI/2*x) */
  285.     xPower = xSquared * x;
  286.     accumulator += -0.64596406188166*xPower;
  287.     xPower *= xSquared;
  288.     accumulator += 0.07969158490912*xPower;
  289.     xPower *= xSquared;
  290.     accumulator += -0.00467687997706*xPower;
  291.     xPower *= xSquared;
  292.     accumulator += 0.00015303015470*xPower;
  293.  
  294.     return 1.0 - 2.0*accumulator*accumulator;               /* cos(w) = 1 - 2*(sin(w/2))^2 */
  295. #endif
  296. }
  297.  
  298.  
  299. float   __sin(register float x)
  300. {
  301. #if STD_MATH_LIB
  302.     return (float)sin((double)x);
  303. #else
  304.     x *= ONE_OVER_PI;
  305.     return __sin_pi(x);
  306. #endif
  307. }
  308.  
  309. float   __cos(register float x)
  310. {
  311. #if STD_MATH_LIB
  312.     return (float)cos((double)x);
  313. #else
  314.     x *= ONE_OVER_PI;
  315.     return __cos_pi(x);
  316. #endif
  317. }
  318.  
  319. float   __tan(register float x)
  320. {
  321. #if STD_MATH_LIB
  322.     return (float)tan((double)x);
  323. #else
  324.     x *= ONE_OVER_PI;
  325.     return __sin_pi(x) / __cos_pi(x);
  326. #endif
  327. }
  328.  
  329.  
  330.  
  331.  
  332. float   __atan(register float x)
  333. {
  334. #if STD_MATH_LIB
  335.     return (float)atan((double)x);
  336. #else
  337.     register float accumulator, xPower, xSquared, offset;
  338.  
  339.     offset = 0.0;
  340.  
  341.     if (x < -1.0)
  342.     {
  343.         offset = -PI_2;
  344.         x = -1.0 / x;
  345.     }
  346.     else if (x > 1.0)
  347.     {
  348.         offset = PI_2;
  349.         x = -1.0 / x;
  350.     }
  351.     xSquared = x * x;
  352.     accumulator = 1.0;
  353.     xPower = xSquared;
  354.     accumulator += 0.33288950512027*xPower;
  355.     xPower *= xSquared;
  356.     accumulator += -0.08467922817644*xPower;
  357.     xPower *= xSquared;
  358.     accumulator += 0.03252232640125*xPower;
  359.     xPower *= xSquared;
  360.     accumulator += -0.00749305860992*xPower;
  361.  
  362.     return offset + x / accumulator;
  363. #endif
  364. }
  365.  
  366.  
  367. float   __asin(register float x)
  368. {
  369. #if STD_MATH_LIB
  370.     return (float)asin((double)x);
  371. #else
  372.     return __atan(x / __sqrt(1.0 - x * x));
  373. #endif
  374. }
  375.  
  376. float   __acos(register float x)
  377. {
  378. #if STD_MATH_LIB
  379.     return (float)acos((double)x);
  380. #else
  381.     return __atan(__sqrt(1.0 - x * x) / x);
  382. #endif
  383. }
  384.  
  385.  
  386. float   __arg(float Imag, float Real)
  387. {
  388. #if STD_MATH_LIB
  389.     return (float)atan2((double)Imag, (double)Real);
  390. #else
  391.     register float accumulator, xPower, xSquared, offset, x;
  392.  
  393.     if (Imag > 0.0)
  394.     {
  395.         if (Imag <= -Real)
  396.         {
  397.             offset = PI;
  398.             x = Imag / Real;
  399.         }
  400.         else if (Imag > Real)
  401.         {
  402.             offset = PI_2;
  403.             x = -Real / Imag;
  404.         }
  405.         else
  406.         {
  407.             offset = 0.0;
  408.             x = Imag / Real;
  409.         }
  410.     }
  411.     else
  412.     {
  413.         if (Imag >= Real)
  414.         {
  415.             offset = -PI;
  416.             x = Imag / Real;
  417.         }
  418.         else if (Imag < -Real)
  419.         {
  420.             offset = -PI_2;
  421.             x = -Real / Imag;
  422.         }
  423.         else
  424.         {
  425.             offset = 0.0;
  426.             x = Imag / Real;
  427.         }
  428.     }
  429.  
  430.     xSquared = x * x;
  431.     accumulator = 1.0;
  432.     xPower = xSquared;
  433.     accumulator += 0.33288950512027*xPower;
  434.     xPower *= xSquared;
  435.     accumulator += -0.08467922817644*xPower;
  436.     xPower *= xSquared;
  437.     accumulator += 0.03252232640125*xPower;
  438.     xPower *= xSquared;
  439.     accumulator += -0.00749305860992*xPower;
  440.  
  441.     return offset + x / accumulator;
  442. #endif
  443. }
  444.  
  445.  
  446.  
  447.  
  448. float   __poly(float *a, int order, float x)
  449. {
  450.     register float accumulator = 0.0, xPower;
  451.     register int n;
  452.  
  453.     accumulator = a[0];
  454.     xPower = x;
  455.     for (n = 1; n <= order; n++)
  456.     {
  457.         accumulator += a[n] * xPower;
  458.         xPower *= x;
  459.     }
  460.  
  461.     return accumulator;
  462. }
  463.  
  464.  
  465. float   __map(float *f, float scaler, float x)
  466. {
  467.     register long i;
  468.  
  469.     x *= scaler;
  470.  
  471.     i = (long)(x + FLOAT_OFFSET) - LONG_OFFSET;         /* round down without floor() */
  472.  
  473.     return f[i] + (f[i + 1] - f[i])*(x - (float)i);       /* linear interpolate between points */
  474. }
  475.  
  476.  
  477. float   __discreteMap(float *f, float scaler, float x)
  478. {
  479.     register long i;
  480.  
  481.     x *= scaler;
  482.  
  483.     i = (long)(x + (FLOAT_OFFSET + 0.5)) - LONG_OFFSET;   /* round to nearest */
  484.  
  485.     return f[i];
  486. }
  487.  
  488.  
  489. unsigned long __random()
  490. {
  491.     static unsigned long seed0 = 0x5B7A2775, seed1 = 0x80C7169F;
  492.  
  493.     seed0 += seed1;
  494.     seed1 += seed0;
  495.  
  496.     return seed1;
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement