winMath_ASM.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mMath.h"
  23. #if defined(TORQUE_SUPPORTS_VC_INLINE_X86_ASM)
  24. static S32 m_mulDivS32_ASM(S32 a, S32 b, S32 c)
  25. { // a * b / c
  26. S32 r;
  27. _asm
  28. {
  29. mov eax, a
  30. imul b
  31. idiv c
  32. mov r, eax
  33. }
  34. return r;
  35. }
  36. static U32 m_mulDivU32_ASM(S32 a, S32 b, U32 c)
  37. { // a * b / c
  38. S32 r;
  39. _asm
  40. {
  41. mov eax, a
  42. mov edx, 0
  43. mul b
  44. div c
  45. mov r, eax
  46. }
  47. return r;
  48. }
  49. static void m_sincos_ASM( F32 angle, F32 *s, F32 *c )
  50. {
  51. _asm
  52. {
  53. fld angle
  54. fsincos
  55. mov eax, c
  56. fstp dword ptr [eax]
  57. mov eax, s
  58. fstp dword ptr [eax]
  59. }
  60. }
  61. U32 Platform::getMathControlState()
  62. {
  63. U16 cw;
  64. _asm
  65. {
  66. fstcw cw
  67. }
  68. return cw;
  69. }
  70. void Platform::setMathControlState(U32 state)
  71. {
  72. U16 cw = state;
  73. _asm
  74. {
  75. fldcw cw
  76. }
  77. }
  78. void Platform::setMathControlStateKnown()
  79. {
  80. U16 cw = 0x27F;
  81. _asm
  82. {
  83. fldcw cw
  84. }
  85. }
  86. #else
  87. // @source http://msdn.microsoft.com/en-us/library/c9676k6h.aspx
  88. U32 Platform::getMathControlState( )
  89. {
  90. U32 control_word = 0;
  91. S32 error = _controlfp_s( &control_word, _DN_FLUSH, _MCW_DN );
  92. return error ? 0 : control_word;
  93. }
  94. void Platform::setMathControlState( U32 state )
  95. {
  96. U32 control_word = 0;
  97. _controlfp_s( &control_word, state, _MCW_DN );
  98. }
  99. void Platform::setMathControlStateKnown( )
  100. {
  101. U32 control_word = 0;
  102. _controlfp_s (&control_word, _PC_64, _MCW_DN);
  103. }
  104. #endif
  105. //------------------------------------------------------------------------------
  106. void mInstallLibrary_ASM()
  107. {
  108. #if defined(TORQUE_SUPPORTS_VC_INLINE_X86_ASM)
  109. m_mulDivS32 = m_mulDivS32_ASM;
  110. m_mulDivU32 = m_mulDivU32_ASM;
  111. m_sincos = m_sincos_ASM;
  112. #endif
  113. }