TMath.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2010 Nokia Corporation.
  3. */
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include "TMath.h"
  7. #if defined __SYMBIAN32__
  8. inline float sinf( float arg )
  9. {
  10. return sin(arg);
  11. }
  12. inline float cosf( float arg )
  13. {
  14. return cos(arg);
  15. }
  16. #endif
  17. float tmath_sin( float angle ) { return sinf( angle ); }
  18. float tmath_cos( float angle ) { return cosf(angle ); }
  19. float tmath_realsqrt( float val ) {
  20. return sqrtf( val );
  21. };
  22. CTMath::CTMath()
  23. {
  24. for (TSDWORD f=0; f<TMATH_TABLE_LENGTH; f++) {
  25. cos_table[f] = (int)( cosf( (float)f /((float)TMATH_TABLE_LENGTH) *3.14159f * 2.0f ) * 65536.0f );
  26. }
  27. }
  28. CTMath::~CTMath()
  29. {
  30. }
  31. TSDWORD CTMath::cos( TSDWORD i )
  32. {
  33. return cos_table[i&TMATH_TABLE_AND];
  34. }
  35. TSDWORD CTMath::sin( TSDWORD i )
  36. {
  37. return cos_table[(i+256)&TMATH_TABLE_AND];
  38. }
  39. #ifndef WIN32
  40. TSDWORD tmath_sqrt(__int64 r)
  41. {
  42. __int64 t;
  43. __int64 b;
  44. __int64 c=0;
  45. for (b=0x100000000000000LL;b!=0;b>>=2) {
  46. t = c + b;
  47. c >>= 1;
  48. if (t <= r) {
  49. r -= t;
  50. c += b;
  51. }
  52. }
  53. return (int)(c);
  54. }
  55. #else
  56. TSDWORD tmath_sqrt(__int64 r)
  57. {
  58. __int64 t;
  59. __int64 b;
  60. __int64 c=0;
  61. for (b=0x100000000000000;b!=0;b>>=2) {
  62. t = c + b;
  63. c >>= 1;
  64. if (t <= r) {
  65. r -= t;
  66. c += b;
  67. }
  68. }
  69. return (int)(c);
  70. }
  71. #endif
  72. TSDWORD tmath_fpmul( const TSDWORD i1, const TSDWORD i2 ) {
  73. return (int)( ((__int64)i1*(__int64)i2)>>FP_BITS );
  74. }
  75. TSDWORD tmath_fpdiv( const TSDWORD i1, const TSDWORD i2 ) {
  76. if (i2==0) return i1; else
  77. return (int)( (((__int64)i1)<<(__int64)(FP_BITS)) / ((__int64)i2) );
  78. //return (int)( (double)i1 / ((double)i2 / (double)FP_VAL) );
  79. }
  80. TSDWORD tmath_fpsqrt( const TSDWORD i ) {
  81. return tmath_sqrt( ((__int64)i)<<FP_BITS );
  82. }
  83. TSDWORD tmath_fpmuldiv( const TSDWORD m1, const TSDWORD m2, const TSDWORD div ) {
  84. return (int)( ((__int64)m1 * (__int64)m2) / div ) ;
  85. }
  86. TSDWORD tmath_interpolate( const TSDWORD m1, const TSDWORD m2, const TSDWORD shr ) {
  87. return (int)(( (__int64)m1 * (__int64)m2) >> shr );
  88. }
  89. TSDWORD tmath_angle(TSDWORD vx, TSDWORD vy)
  90. {
  91. return (TSDWORD)( (atan2((float)vy, (float)vx ) + 3.14159f ) / (3.14159f) *32768.0f );
  92. }