macCarbMath.cpp 3.6 KB

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