Max 5 API Reference
00001 /* 00002 jit.math.c 00003 Copyright 2001-2005 - Cycling '74 00004 Joshua Kit Clayton jkc@cycling74.com 00005 */ 00006 00007 #include "jit.common.h" 00008 #include <math.h> 00009 #include <float.h> 00010 00011 #ifdef WIN_VERSION 00012 double acosh(double x); 00013 double asinh(double x); 00014 double atanh(double x); 00015 double expm1(double x); 00016 double exp2(double x); 00017 double log2(double x); 00018 double round(double x); 00019 double trunc(double x); 00020 #endif //WIN_VERSION 00021 00022 /** 00023 * @defgroup mathmod Math Module 00024 @ingroup jitter 00025 */ 00026 00027 00028 /** 00029 * Calculates the cosine. 00030 * 00031 * @ingroup mathmod 00032 * 00033 * @param x input 00034 * @return output 00035 * 00036 */ 00037 double jit_math_cos(double x) 00038 { 00039 return cos(x); 00040 } 00041 00042 /** 00043 * Calculates the sine. 00044 * 00045 * @ingroup mathmod 00046 * 00047 * @param x input 00048 * @return output 00049 * 00050 */ 00051 double jit_math_sin(double x) 00052 { 00053 return sin(x); 00054 } 00055 00056 /** 00057 * Calculates the tangent. 00058 * 00059 * @ingroup mathmod 00060 * 00061 * @param x input 00062 * @return output 00063 * 00064 */ 00065 double jit_math_tan(double x) 00066 { 00067 return tan(x); 00068 } 00069 00070 /** 00071 * Calculates the arccosine. 00072 * 00073 * @ingroup mathmod 00074 * 00075 * @param x input 00076 * @return output 00077 * 00078 */ 00079 double jit_math_acos(double x) 00080 { 00081 return acos(x); 00082 } 00083 00084 /** 00085 * Calculates the arcsine. 00086 * 00087 * @ingroup mathmod 00088 * 00089 * @param x input 00090 * @return output 00091 * 00092 */ 00093 double jit_math_asin(double x) 00094 { 00095 return asin(x); 00096 } 00097 00098 /** 00099 * Calculates the arctangent. 00100 * 00101 * @ingroup mathmod 00102 * 00103 * @param x input 00104 * @return output 00105 * 00106 */ 00107 double jit_math_atan(double x) 00108 { 00109 return atan(x); 00110 } 00111 00112 /** 00113 * Calculates the four quadrant arctangent. 00114 * 00115 * @ingroup mathmod 00116 * 00117 * @param y input 00118 * @param x input 00119 * @return output 00120 * 00121 */ 00122 double jit_math_atan2(double y, double x) 00123 { 00124 return atan2(y,x); 00125 } 00126 00127 /** 00128 * Calculates the hyperbolic cosine. 00129 * 00130 * @ingroup mathmod 00131 * 00132 * @param x input 00133 * @return output 00134 * 00135 */ 00136 double jit_math_cosh(double x) 00137 { 00138 return cosh(x); 00139 } 00140 00141 /** 00142 * Calculates the hyperbolic sine. 00143 * 00144 * @ingroup mathmod 00145 * 00146 * @param x input 00147 * @return output 00148 * 00149 */ 00150 double jit_math_sinh(double x) 00151 { 00152 return sinh(x); 00153 } 00154 00155 /** 00156 * Calculates the hyperbolic tangent. 00157 * 00158 * @ingroup mathmod 00159 * 00160 * @param x input 00161 * @return output 00162 * 00163 */ 00164 double jit_math_tanh(double x) 00165 { 00166 return tanh(x); 00167 } 00168 00169 /** 00170 * Calculates the hyperbolic arccosine. 00171 * 00172 * @ingroup mathmod 00173 * 00174 * @param x input 00175 * @return output 00176 * 00177 */ 00178 double jit_math_acosh(double x) 00179 { 00180 return acosh(x); 00181 } 00182 00183 /** 00184 * Calculates the hyperbolic arcsine. 00185 * 00186 * @ingroup mathmod 00187 * 00188 * @param x input 00189 * @return output 00190 * 00191 */ 00192 double jit_math_asinh(double x) 00193 { 00194 return asinh(x); 00195 } 00196 00197 /** 00198 * Calculates the hyperbolic arctangent. 00199 * 00200 * @ingroup mathmod 00201 * 00202 * @param x input 00203 * @return output 00204 * 00205 */ 00206 double jit_math_atanh(double x) 00207 { 00208 return atanh(x); 00209 } 00210 00211 /** 00212 * Calculates the exponent. 00213 * 00214 * @ingroup mathmod 00215 * 00216 * @param x input 00217 * @return output 00218 * 00219 */ 00220 double jit_math_exp(double x) 00221 { 00222 return exp(x); 00223 } 00224 00225 /** 00226 * Calculates the exponent minus 1. 00227 * 00228 * @ingroup mathmod 00229 * 00230 * @param x input 00231 * @return output 00232 * 00233 */ 00234 double jit_math_expm1(double x) 00235 { 00236 return expm1(x); 00237 } 00238 00239 /** 00240 * Calculates the exponent base 2. 00241 * 00242 * @ingroup mathmod 00243 * 00244 * @param x input 00245 * @return output 00246 * 00247 */ 00248 double jit_math_exp2(double x) 00249 { 00250 return exp2(x); 00251 } 00252 00253 /** 00254 * Calculates the logarithm. 00255 * 00256 * @ingroup mathmod 00257 * 00258 * @param x input 00259 * @return output 00260 * 00261 */ 00262 double jit_math_log(double x) 00263 { 00264 return log(x); 00265 } 00266 00267 /** 00268 * Calculates the logarithm base 2. 00269 * 00270 * @ingroup mathmod 00271 * 00272 * @param x input 00273 * @return output 00274 * 00275 */ 00276 double jit_math_log2(double x) 00277 { 00278 return log2(x); 00279 } 00280 00281 /** 00282 * Calculates the logarithm base 10. 00283 * 00284 * @ingroup mathmod 00285 * 00286 * @param x input 00287 * @return output 00288 * 00289 */ 00290 double jit_math_log10(double x) 00291 { 00292 return log10(x); 00293 } 00294 00295 /** 00296 * Calculates the hypotenuse. 00297 * 00298 * @ingroup mathmod 00299 * 00300 * @param x input 00301 * @param y input 00302 * @return output 00303 * 00304 */ 00305 double jit_math_hypot(double x, double y) 00306 { 00307 return hypot(x,y); 00308 } 00309 00310 /** 00311 * Calculates x raised to the y power. 00312 * 00313 * @ingroup mathmod 00314 * 00315 * @param x input 00316 * @param y input 00317 * @return output 00318 * 00319 */ 00320 double jit_math_pow(double x, double y) 00321 { 00322 return pow(x,y); 00323 } 00324 00325 /** 00326 * Calculates the square root. 00327 * 00328 * @ingroup mathmod 00329 * 00330 * @param x input 00331 * @return output 00332 * 00333 */ 00334 double jit_math_sqrt(double x) 00335 { 00336 return sqrt(x); 00337 } 00338 00339 /** 00340 * Calculates the ceiling. 00341 * 00342 * @ingroup mathmod 00343 * 00344 * @param x input 00345 * @return output 00346 * 00347 */ 00348 double jit_math_ceil(double x) 00349 { 00350 return ceil(x); 00351 } 00352 00353 /** 00354 * Calculates the floor. 00355 * 00356 * @ingroup mathmod 00357 * 00358 * @param x input 00359 * @return output 00360 * 00361 */ 00362 double jit_math_floor(double x) 00363 { 00364 return floor(x); 00365 } 00366 00367 /** 00368 * Rounds the input. 00369 * 00370 * @ingroup mathmod 00371 * 00372 * @param x input 00373 * @return output 00374 * 00375 */ 00376 double jit_math_round(double x) 00377 { 00378 return round(x); 00379 } 00380 00381 /** 00382 * Truncates the input. 00383 * 00384 * @ingroup mathmod 00385 * 00386 * @param x input 00387 * @return output 00388 * 00389 */ 00390 double jit_math_trunc(double x) 00391 { 00392 return trunc(x); 00393 } 00394 00395 /** 00396 * Calculates the floating point x modulo y. 00397 * 00398 * @ingroup mathmod 00399 * 00400 * @param x input 00401 * @param y input 00402 * @return output 00403 * 00404 */ 00405 double jit_math_fmod(double x, double y) 00406 { 00407 double d; 00408 00409 if (y) { 00410 if (y<0) y = -y; // modulus needs to be absolute value 00411 //don't do float->int->float if can be done w/ one add or sub 00412 if (x>=y) { 00413 if (x>=(y*2.)) { 00414 d = x / y; 00415 d = d - (long) d; 00416 x = d * y; 00417 } else { 00418 x -= y; 00419 } 00420 } else if (x<=(-y)) { 00421 if (x<=(-y*2.)) { 00422 d = x / y; 00423 d = d - (long) d; 00424 x = d * y; 00425 } else { 00426 x += y; 00427 } 00428 } 00429 } else x = 0.; //don't divide by zero 00430 00431 return x; 00432 } 00433 00434 00435 /** 00436 * Calculates the fold of x between lo and hi. 00437 * 00438 * @ingroup mathmod 00439 * 00440 * @param x input 00441 * @param lo lower bound 00442 * @param hi upper bound 00443 * @return output 00444 * 00445 */ 00446 double jit_math_fold(double x, double lo, double hi) 00447 { 00448 long di; 00449 double m,d,tmp; 00450 00451 if (lo > hi) { 00452 tmp = lo; 00453 lo = hi; 00454 hi = tmp; 00455 } 00456 if (lo) 00457 x -= lo; 00458 m = hi-lo; 00459 if (m) { 00460 if (x < 0.) 00461 x = -x; 00462 if (x>m) { 00463 if (x>(m*2.)) { 00464 d = x / m; 00465 di = (long) d; 00466 d = d - (double) di; 00467 if (di%2) { 00468 if (d < 0) { 00469 d = -1. - d; 00470 } else { 00471 d = 1. - d; 00472 } 00473 } 00474 x = d * m; 00475 if (x < 0.) 00476 x = m+x; 00477 } else { 00478 x = m-(x-m); 00479 } 00480 } 00481 } else x = 0.; //don't divide by zero 00482 00483 return x + lo; 00484 } 00485 00486 /** 00487 * Calculates the wrap of x between lo and hi. 00488 * 00489 * @ingroup mathmod 00490 * 00491 * @param x input 00492 * @param lo lower bound 00493 * @param hi upper bound 00494 * @return output 00495 * 00496 */ 00497 double jit_math_wrap(double x, double lo, double hi) 00498 { 00499 double m,d,tmp; 00500 long di; 00501 00502 if (lo > hi) { 00503 tmp = lo; 00504 lo = hi; 00505 hi = tmp; 00506 } 00507 if (lo) 00508 x -= lo; 00509 m = hi-lo; 00510 if (m) { 00511 if (x>m) { 00512 if (x>(m*2.)) { 00513 d = x / m; 00514 di = (long) d; 00515 d = d - (double) di; 00516 x = d * m; 00517 } else { 00518 x -= m; 00519 } 00520 } else if (x<0.) { 00521 if (x<(-m)) { 00522 d = x / m; 00523 di = (long) d; 00524 d = d - (double) di; 00525 x = d * m; 00526 x += m; 00527 } else { 00528 x += m; 00529 } 00530 } 00531 } else x = 0.; //don't divide by zero 00532 00533 return x + lo; 00534 } 00535 00536 00537 /* 00538 * Derek Gerstmann - derek@cycling74.com 00539 * 00540 * The following Bessel functions were taken from the free NUMLIBC 00541 * library since CodeWarrior doesn't define them: 00542 * 00543 * http://www.math.ntnu.no/num/nnm/Program/Numlibc/#header 00544 * 00545 * -[dg] 00546 */ 00547 00548 /** 00549 * Calcuates the j1_0 Bessel function. 00550 * 00551 * @ingroup mathmod 00552 * 00553 * @param x input 00554 * @return output 00555 * 00556 */ 00557 double jit_math_j1_0(double x) 00558 { 00559 static int J1_MAX = 16; 00560 00561 static double a[ 16] = { 1.29671754121052984, 00562 -1.19180116054121687, 00563 1.28799409885767762, 00564 -0.66144393413454325, 00565 0.17770911723972828, 00566 -0.02917552480615421, 00567 0.00324027018268386, 00568 -0.00026044438934858, 00569 0.00001588701923993, 00570 -0.00000076175878054, 00571 0.00000002949707007, 00572 -0.00000000094242130, 00573 0.00000000002528124, 00574 -0.00000000000057774, 00575 0.00000000000001139, 00576 -0.00000000000000020 }; 00577 00578 int k; 00579 double arg, t, value, b0, b1, b2; 00580 00581 arg = .125 * x; 00582 t = 2. * (2.*arg*arg - 1.); 00583 00584 b2 = 0.; 00585 b1 = 0.; 00586 b0 = a[J1_MAX-1]; 00587 00588 for (k = J1_MAX-2; k >= 0; k--) { 00589 b2 = b1; 00590 b1 = b0; 00591 b0 = t * b1 - b2 + a[k]; 00592 } 00593 00594 value = .5 * (b0 - b2); 00595 00596 return(arg * value); 00597 00598 } 00599 00600 /** 00601 * Calcuates the p1 Bessel function. 00602 * 00603 * @ingroup mathmod 00604 * 00605 * @param x input 00606 * @return output 00607 * 00608 */ 00609 double jit_math_p1( double x) 00610 { 00611 static int P1_MAX = 13; 00612 00613 static double a[ 13] = { 2.00180608172002740, 00614 0.00089898983308594, 00615 -0.00000398728430049, 00616 0.00000006177633961, 00617 -0.00000000187189075, 00618 0.00000000008816899, 00619 -0.00000000000570486, 00620 0.00000000000046992, 00621 -0.00000000000004684, 00622 0.00000000000000545, 00623 -0.00000000000000072, 00624 0.00000000000000011, 00625 -0.00000000000000002 }; 00626 00627 int k; 00628 double arg, t, value, b0, b1, b2; 00629 00630 arg = 8. / x; 00631 t = 2. * (2.*arg*arg - 1.); 00632 00633 b2 = 0.; 00634 b1 = 0.; 00635 b0 = a[P1_MAX-1]; 00636 00637 for (k = P1_MAX-2; k >= 0; k--) { 00638 b2 = b1; 00639 b1 = b0; 00640 b0 = t * b1 - b2 + a[k]; 00641 } 00642 00643 value = .5 * (b0 - b2); 00644 00645 return(value); 00646 } 00647 00648 /** 00649 * Calcuates the q1 Bessel function. 00650 * 00651 * @ingroup mathmod 00652 * 00653 * @param x input 00654 * @return output 00655 * 00656 */ 00657 double jit_math_q1(double x) 00658 { 00659 static int Q1_MAX = 13; 00660 00661 static double a[ 13] = { 0.09355557413907065, 00662 -0.00009627723549157, 00663 0.00000091386152580, 00664 -0.00000002095978138, 00665 0.00000000082291933, 00666 -0.00000000004683664, 00667 0.00000000000351522, 00668 -0.00000000000032643, 00669 0.00000000000003597, 00670 -0.00000000000000456, 00671 0.00000000000000065, 00672 -0.00000000000000010, 00673 0.00000000000000002 }; 00674 00675 int k; 00676 double arg, t, value, b0, b1, b2; 00677 00678 arg = 8. / x; 00679 t = 2. * (2.*arg*arg - 1.); 00680 00681 b2 = 0.; 00682 b1 = 0.; 00683 b0 = a[Q1_MAX-1]; 00684 00685 for (k = Q1_MAX-2; k >= 0; k--) { 00686 b2 = b1; 00687 b1 = b0; 00688 b0 = t * b1 - b2 + a[k]; 00689 } 00690 00691 value = .5 * (b0 - b2); 00692 00693 return(arg * value); 00694 } 00695 00696 /** 00697 * Calcuates the j1 Bessel function. 00698 * 00699 * @ingroup mathmod 00700 * 00701 * @param x input 00702 * @return output 00703 * 00704 */ 00705 double jit_math_j1(double x) 00706 { 00707 static double cof1 = .79788456080286536; /* sqrt(2/pi) */ 00708 static double cof2 = 2.35619449019234493; /* 3*pi/4 */ 00709 00710 double value, ksi1; 00711 int flag; 00712 00713 flag = 1; 00714 if (x >= -8. && x <= 8.) 00715 value = jit_math_j1_0(x); 00716 else if (x > 8.) { 00717 ksi1 = x - cof2; 00718 value = jit_math_p1(x) * jit_math_cos(ksi1) - jit_math_q1(x) * jit_math_sin(ksi1); 00719 value *= cof1/sqrt(x); 00720 } 00721 else { 00722 flag = 0; 00723 value = 0.; 00724 } 00725 return(value); 00726 00727 } 00728 00729 // -------------------------------------------------------------------------- 00730 00731 /** 00732 * Rounds up to the nearest power of two. 00733 * 00734 * @ingroup mathmod 00735 * 00736 * @param x input 00737 * @return output 00738 * 00739 */ 00740 unsigned long jit_math_roundup_poweroftwo(unsigned long x) 00741 { 00742 x--; 00743 x |= x >> 1; 00744 x |= x >> 2; 00745 x |= x >> 4; 00746 x |= x >> 8; 00747 x |= x >> 16; 00748 return x+1; 00749 } 00750 00751 /** 00752 * Checks if input is finite. 00753 * 00754 * @ingroup mathmod 00755 * 00756 * @param v input 00757 * @return 1 if finite. Otherwise, 0. 00758 * 00759 */ 00760 long jit_math_is_finite(float v) 00761 { 00762 #ifdef WIN_VERSION 00763 return _finite(v); 00764 #else 00765 return !isinf(v); 00766 #endif 00767 } 00768 00769 /** 00770 * Checks if input is not a number (NaN). 00771 * 00772 * @ingroup mathmod 00773 * 00774 * @param v input 00775 * @return 1 if not a number. Otherwise, 0. 00776 * 00777 */ 00778 long jit_math_is_nan(float v) 00779 { 00780 #ifdef WIN_VERSION 00781 return _isnan(v); 00782 #else 00783 return isnan(v); 00784 #endif 00785 } 00786 00787 /** 00788 * Checks if input is both finite and a number. 00789 * 00790 * @ingroup mathmod 00791 * 00792 * @param v input 00793 * @return 1 if vaild. Otherwise, 0. 00794 * 00795 */ 00796 long jit_math_is_valid(float v) 00797 { 00798 return (!jit_math_is_nan(v) && jit_math_is_finite(v)); 00799 } 00800 00801 /** 00802 * Checks if input is a power of two. 00803 * 00804 * @ingroup mathmod 00805 * 00806 * @param x input 00807 * @return 1 if finite. Otherwise, 0. 00808 * 00809 */ 00810 long jit_math_is_poweroftwo(long x) 00811 { 00812 return (x & (x - 1)) == 0; 00813 } 00814 00815 float jit_math_project_to_sphere(float r, float x, float y) 00816 { 00817 float d, t, z; 00818 00819 d = jit_math_sqrt(x*x + y*y); 00820 if (d < r * 0.70710678118654752440) 00821 { 00822 // inside sphere 00823 z = jit_math_sqrt(r*r - d*d); 00824 } else { 00825 // on hyperbola 00826 t = r / (float)1.41421356237309504880; 00827 z = t*t / d; 00828 } 00829 return z; 00830 } 00831 00832 // -------------------------------------------------------------------------- 00833 00834 /* 00835 * Derek Gerstmann - derek@cycling74.com 00836 * 00837 * The following square root table methods were adapted from examples 00838 * from NVidia's public domain fast math utility functions: 00839 * 00840 * http://www.nvidia.com/object/fast_math_routines.html 00841 * 00842 * -[dg] 00843 */ 00844 00845 static unsigned int g_jit_math_fast_sqrt_table[0x10000]; 00846 static long g_jit_math_fast_sqrt_table_init = FALSE; 00847 00848 typedef union _jit_math_fast_sqrt_union 00849 { 00850 float f; 00851 unsigned int i; 00852 } t_jit_math_fast_sqrt_union; 00853 00854 void jit_math_fast_sqrt_table_init() 00855 { 00856 unsigned int i; 00857 t_jit_math_fast_sqrt_union s; 00858 00859 for (i = 0; i <= 0x7FFF; i++) 00860 { 00861 // build a float with the bit pattern i as mantissa and an exponent of 0, stored as 127 00862 s.i = (i << 8) | (0x7F << 23); 00863 s.f = (float)jit_math_sqrt(s.f); 00864 00865 // take the square root then strip the first 7 bits of the mantissa into the table 00866 g_jit_math_fast_sqrt_table[i + 0x8000] = (s.i & 0x7FFFFF); 00867 00868 // repeat the process, this time with an exponent of 1, stored as 128 00869 s.i = (i << 8) | (0x80 << 23); 00870 s.f = (float)jit_math_sqrt(s.f); 00871 00872 g_jit_math_fast_sqrt_table[i] = (s.i & 0x7FFFFF); 00873 } 00874 00875 g_jit_math_fast_sqrt_table_init = TRUE; 00876 } 00877 00878 /** 00879 * Calculates the square root by fast approximation. 00880 * 00881 * @ingroup mathmod 00882 * 00883 * @param n input 00884 * @return output 00885 * 00886 */ 00887 float jit_math_fast_sqrt(float n) 00888 { 00889 if(g_jit_math_fast_sqrt_table_init == FALSE) 00890 jit_math_fast_sqrt_table_init(); 00891 00892 if (JIT_MATH_F32_BITS(n) == 0) 00893 return 0.0f; 00894 00895 JIT_MATH_F32_BITS(n) = g_jit_math_fast_sqrt_table[(JIT_MATH_F32_BITS(n) >> 8) & 0xFFFF] | ((((JIT_MATH_F32_BITS(n) - 0x3F800000) >> 1) + 0x3F800000) & 0x7F800000); 00896 return n; 00897 } 00898 00899 /** 00900 * Calculates the inverse square root by fast approximation. 00901 * 00902 * @ingroup mathmod 00903 * 00904 * @param x input 00905 * @return output 00906 * 00907 */ 00908 float jit_math_fast_invsqrt(float x) 00909 { 00910 float half = 0.5f*x; 00911 int i = *(int*)&x; 00912 i = 0x5f3759df - (i >> 1); 00913 x = *(float*)&i; 00914 x = x*(1.5f - half*x*x); 00915 return x; 00916 } 00917 00918 // -------------------------------------------------------------------------- 00919 00920 /* 00921 * Derek Gerstmann - derek@cycling74.com 00922 * 00923 * The following polynomial approximations for the various trig 00924 * functions use constants derived by David Eberly of 00925 * Magic Software in his free Wild Magic Library: 00926 * 00927 * http://www.magic-software.com/Math.html 00928 * 00929 * -[dg] 00930 */ 00931 00932 /** 00933 * Calculates the sine by fast approximation. 00934 * 00935 * Absolute error of 1.7e-04 for [0, PI/2] 00936 * 00937 * @ingroup mathmod 00938 * 00939 * @param x input 00940 * @return output 00941 * 00942 */ 00943 float jit_math_fast_sin(float x) 00944 { 00945 float x2 = x*x; 00946 float rv = (float)7.61e-03; 00947 rv *= x2; 00948 rv -= (float)1.6605e-01; 00949 rv *= x2; 00950 rv += (float)1.0; 00951 rv *= x; 00952 return rv; 00953 } 00954 00955 /** 00956 * Calculates the cosine by fast approximation. 00957 * 00958 * Absolute error of 1.2e-03 for [0, PI/2] 00959 * 00960 * @ingroup mathmod 00961 * 00962 * @param x input 00963 * @return output 00964 * 00965 */ 00966 float jit_math_fast_cos(float x) 00967 { 00968 float x2 = x*x; 00969 float rv = (float)3.705e-02; 00970 rv *= x2; 00971 rv -= (float)4.967e-01; 00972 rv *= x2; 00973 rv += (float)1.0; 00974 return rv; 00975 } 00976 00977 /** 00978 * Calculates the tangent by fast approximation. 00979 * 00980 * Absolute error of 1.9e-00 for [0, PI/4] 00981 * 00982 * @ingroup mathmod 00983 * 00984 * @param x input 00985 * @return output 00986 * 00987 */ 00988 float jit_math_fast_tan(float x) 00989 { 00990 float x2 = x*x; 00991 float rv = (float)9.5168091e-03; 00992 rv *= x2; 00993 rv += (float)2.900525e-03; 00994 rv *= x2; 00995 rv += (float)2.45650893e-02; 00996 rv *= x2; 00997 rv += (float)5.33740603e-02; 00998 rv *= x2; 00999 rv += (float)1.333923995e-01; 01000 rv *= x2; 01001 rv += (float)3.333314036e-01; 01002 rv *= x2; 01003 rv += (float)1.0; 01004 rv *= x; 01005 return rv; 01006 } 01007 01008 /** 01009 * Calculates the arcsine by fast approximation. 01010 * 01011 * Absolute error of 6.8e-05 for [0, 1] 01012 * 01013 * @ingroup mathmod 01014 * 01015 * @param x input 01016 * @return output 01017 * 01018 */ 01019 float jit_math_fast_asin(float x) 01020 { 01021 float root = jit_math_sqrt(((float)1.0)-x); 01022 float rv = -(float)0.0187293; 01023 rv *= x; 01024 rv += (float)0.0742610; 01025 rv *= x; 01026 rv -= (float)0.2121144; 01027 rv *= x; 01028 rv += (float)1.5707288; 01029 rv = JIT_MATH_F32_HALF_PI - root*rv; 01030 return rv; 01031 } 01032 01033 /** 01034 * Calculates the arccosine by fast approximation. 01035 * 01036 * Absolute error of 6.8e-05 for [0, 1] 01037 * 01038 * @ingroup mathmod 01039 * 01040 * @param x input 01041 * @return output 01042 * 01043 */ 01044 float jit_math_fast_acos(float x) 01045 { 01046 float root = jit_math_sqrt(((float)1.0)-x); 01047 float rv = -(float)0.0187293; 01048 rv *= x; 01049 rv += (float)0.0742610; 01050 rv *= x; 01051 rv -= (float)0.2121144; 01052 rv *= x; 01053 rv += (float)1.5707288; 01054 rv *= root; 01055 return rv; 01056 } 01057 01058 /** 01059 * Calculates the arctangent by fast approximation. 01060 * 01061 * Absolute error of 1.43-08 for [-1, 1] 01062 * 01063 * @ingroup mathmod 01064 * 01065 * @param x input 01066 * @return output 01067 * 01068 */ 01069 float jit_math_fast_atan(float x) 01070 { 01071 float x2 = x*x; 01072 float rv = (float)0.0028662257; 01073 rv *= x2; 01074 rv -= (float)0.0161657367; 01075 rv *= x2; 01076 rv += (float)0.0429096138; 01077 rv *= x2; 01078 rv -= (float)0.0752896400; 01079 rv *= x2; 01080 rv += (float)0.1065626393; 01081 rv *= x2; 01082 rv -= (float)0.1420889944; 01083 rv *= x2; 01084 rv += (float)0.1999355085; 01085 rv *= x2; 01086 rv -= (float)0.3333314528; 01087 rv *= x2; 01088 rv += (float)1.0; 01089 rv *= x; 01090 return rv; 01091 } 01092 01093 // -------------------------------------------------------------------------- 01094 01095 #ifdef WIN_VERSION 01096 double acosh(double x) 01097 { 01098 return log(x + sqrt(x * x - 1)); 01099 } 01100 01101 double asinh(double x) 01102 { 01103 return log(x + sqrt(x * x + 1)); 01104 } 01105 01106 double atanh(double x) 01107 { 01108 return log((1 + x) / (1 - x)) / 2; 01109 } 01110 01111 double expm1(double x) 01112 { 01113 return exp(x)-1; 01114 } 01115 01116 double exp2(double x) 01117 { 01118 return pow(2,x); 01119 } 01120 01121 double log2(double x) 01122 { 01123 return log(x)/log(2); 01124 } 01125 01126 double round(double x) 01127 { 01128 long c=x; 01129 if (x<0) 01130 return ((x-c)<=-0.5)?c-1:c; 01131 else 01132 return ((x-c)>=0.5)?c+1:c; 01133 } 01134 01135 double trunc(double x) 01136 { 01137 long c=x; 01138 return c; 01139 } 01140 01141 #endif //WIN_VERSION 01142
Copyright © 2008, Cycling '74