POSIXMath.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef __APPLE__
  23. #include "platform/platform.h"
  24. #include "console/console.h"
  25. #include "math/mMath.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "console/engineAPI.h"
  28. extern void mInstallLibrary_C();
  29. extern void mInstallLibrary_ASM();
  30. extern void mInstall_AMD_Math();
  31. extern void mInstall_Library_SSE();
  32. //--------------------------------------
  33. DefineEngineStringlyVariadicFunction( mathInit, void, 1, 10, "( ... )"
  34. "@brief Install the math library with specified extensions.\n\n"
  35. "Possible parameters are:\n\n"
  36. " - 'DETECT' Autodetect math lib settings.\n\n"
  37. " - 'C' Enable the C math routines. C routines are always enabled.\n\n"
  38. " - 'SSE' Enable SSE math routines.\n\n"
  39. "@ingroup Math")
  40. {
  41. U32 properties = CPU_PROP_C; // C entensions are always used
  42. if (argc == 1)
  43. {
  44. Math::init(0);
  45. return;
  46. }
  47. for (argc--, argv++; argc; argc--, argv++)
  48. {
  49. if (dStricmp(*argv, "DETECT") == 0) {
  50. Math::init(0);
  51. return;
  52. }
  53. if (dStricmp(*argv, "C") == 0) {
  54. properties |= CPU_PROP_C;
  55. continue;
  56. }
  57. if (dStricmp(*argv, "FPU") == 0) {
  58. properties |= CPU_PROP_FPU;
  59. continue;
  60. }
  61. if (dStricmp(*argv, "MMX") == 0) {
  62. properties |= CPU_PROP_MMX;
  63. continue;
  64. }
  65. if (dStricmp(*argv, "3DNOW") == 0) {
  66. properties |= CPU_PROP_3DNOW;
  67. continue;
  68. }
  69. if (dStricmp(*argv, "SSE") == 0) {
  70. properties |= CPU_PROP_SSE;
  71. continue;
  72. }
  73. Con::printf("Error: MathInit(): ignoring unknown math extension '%s'", (const char*)argv[0]);
  74. }
  75. Math::init(properties);
  76. }
  77. //------------------------------------------------------------------------------
  78. void Math::init(U32 properties)
  79. {
  80. if (!properties)
  81. // detect what's available
  82. properties = Platform::SystemInfo.processor.properties;
  83. else
  84. // Make sure we're not asking for anything that's not supported
  85. properties &= Platform::SystemInfo.processor.properties;
  86. Con::printf("Math Init:");
  87. Con::printf(" Installing Standard C extensions");
  88. mInstallLibrary_C();
  89. #if defined(TORQUE_CPU_X32) || defined(TORQUE_CPU_X64)
  90. Con::printf(" Installing Assembly extensions");
  91. mInstallLibrary_ASM();
  92. #endif
  93. if (properties & CPU_PROP_FPU)
  94. {
  95. Con::printf(" Installing FPU extensions");
  96. }
  97. if (properties & CPU_PROP_MMX)
  98. {
  99. Con::printf(" Installing MMX extensions");
  100. if (properties & CPU_PROP_3DNOW)
  101. {
  102. Con::printf(" Installing 3DNow extensions");
  103. mInstall_AMD_Math();
  104. }
  105. }
  106. #if !defined(__MWERKS__) || (__MWERKS__ >= 0x2400)
  107. if (properties & CPU_PROP_SSE)
  108. {
  109. Con::printf(" Installing SSE extensions");
  110. mInstall_Library_SSE();
  111. }
  112. #endif //mwerks>2.4
  113. Con::printf(" ");
  114. }
  115. //------------------------------------------------------------------------------
  116. static MRandomLCG sgPlatRandom;
  117. F32 Platform::getRandom()
  118. {
  119. return sgPlatRandom.randF();
  120. }
  121. #if defined(TORQUE_CPU_X86) || defined(__x86_64__)
  122. U32 Platform::getMathControlState()
  123. {
  124. U16 cw;
  125. asm("fstcw %0" : "=m" (cw) :);
  126. return cw;
  127. }
  128. void Platform::setMathControlState(U32 state)
  129. {
  130. U16 cw = state;
  131. asm("fldcw %0" : : "m" (cw));
  132. }
  133. void Platform::setMathControlStateKnown()
  134. {
  135. U16 cw = 0x27F;
  136. asm("fldcw %0" : : "m" (cw));
  137. }
  138. #else
  139. U32 Platform::getMathControlState()
  140. {
  141. return 0;
  142. }
  143. void Platform::setMathControlState(U32 state)
  144. {
  145. }
  146. void Platform::setMathControlStateKnown()
  147. {
  148. }
  149. #endif
  150. #endif