macMath.mm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #import "platform/platform.h"
  23. #import "console/console.h"
  24. #import "math/mMath.h"
  25. #import "core/strings/stringFunctions.h"
  26. #include "console/engineAPI.h"
  27. extern void mInstallLibrary_C();
  28. extern void mInstallLibrary_Vec();
  29. extern void mInstall_Library_SSE();
  30. static MRandomLCG sgPlatRandom;
  31. U32 Platform::getMathControlState()
  32. {
  33. #ifdef TORQUE_CPU_X86
  34. U16 cw;
  35. asm("fstcw %0" : "=m" (cw) :);
  36. return cw;
  37. #else
  38. return 0;
  39. #endif
  40. }
  41. void Platform::setMathControlState(U32 state)
  42. {
  43. #ifdef TORQUE_CPU_X86
  44. U16 cw = state;
  45. asm("fldcw %0" : : "m" (cw));
  46. #endif
  47. }
  48. void Platform::setMathControlStateKnown()
  49. {
  50. #ifdef TORQUE_CPU_X86
  51. U16 cw = 0x27F;
  52. asm("fldcw %0" : : "m" (cw));
  53. #endif
  54. }
  55. //--------------------------------------
  56. DefineEngineStringlyVariadicFunction( mathInit, void, 1, 10, "( ... )"
  57. "@brief Install the math library with specified extensions.\n\n"
  58. "Possible parameters are:\n\n"
  59. " - 'DETECT' Autodetect math lib settings.\n\n"
  60. " - 'C' Enable the C math routines. C routines are always enabled.\n\n"
  61. " - 'SSE' Enable SSE math routines.\n\n"
  62. "@ingroup Math")
  63. {
  64. U32 properties = CPU_PROP_C; // C entensions are always used
  65. if (argc == 1)
  66. {
  67. Math::init(0);
  68. return;
  69. }
  70. for (argc--, argv++; argc; argc--, argv++)
  71. {
  72. if (dStricmp(*argv, "DETECT") == 0) {
  73. Math::init(0);
  74. return;
  75. }
  76. if (dStricmp(*argv, "C") == 0) {
  77. properties |= CPU_PROP_C;
  78. continue;
  79. }
  80. if( dStricmp( *argv, "SSE" ) == 0 )
  81. {
  82. properties |= CPU_PROP_SSE;
  83. continue;
  84. }
  85. //Con::printf("Error: MathInit(): ignoring unknown math extension '%s'", *argv);
  86. }
  87. Math::init(properties);
  88. }
  89. //------------------------------------------------------------------------------
  90. void Math::init(U32 properties)
  91. {
  92. if (!properties)
  93. // detect what's available
  94. properties = Platform::SystemInfo.processor.properties;
  95. else
  96. // Make sure we're not asking for anything that's not supported
  97. properties &= Platform::SystemInfo.processor.properties;
  98. Con::printf("Math Init:");
  99. Con::printf(" Installing Standard C extensions");
  100. mInstallLibrary_C();
  101. #ifdef TORQUE_CPU_X86
  102. if( properties & CPU_PROP_SSE )
  103. {
  104. Con::printf( " Installing SSE extensions" );
  105. mInstall_Library_SSE();
  106. }
  107. #endif
  108. Con::printf(" ");
  109. }
  110. //------------------------------------------------------------------------------
  111. F32 Platform::getRandom()
  112. {
  113. return sgPlatRandom.randF();
  114. }