macMath.mm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. extern void mInstallLibrary_C();
  27. extern void mInstallLibrary_Vec();
  28. extern void mInstall_Library_SSE();
  29. static MRandomLCG sgPlatRandom;
  30. U32 Platform::getMathControlState()
  31. {
  32. U16 cw;
  33. asm("fstcw %0" : "=m" (cw) :);
  34. return cw;
  35. }
  36. void Platform::setMathControlState(U32 state)
  37. {
  38. U16 cw = state;
  39. asm("fldcw %0" : : "m" (cw));
  40. }
  41. void Platform::setMathControlStateKnown()
  42. {
  43. U16 cw = 0x27F;
  44. asm("fldcw %0" : : "m" (cw));
  45. }
  46. //--------------------------------------
  47. ConsoleFunction( MathInit, void, 1, 10, "(DETECT|C|SSE)")
  48. {
  49. U32 properties = CPU_PROP_C; // C entensions are always used
  50. if (argc == 1)
  51. {
  52. Math::init(0);
  53. return;
  54. }
  55. for (argc--, argv++; argc; argc--, argv++)
  56. {
  57. if (dStricmp(*argv, "DETECT") == 0) {
  58. Math::init(0);
  59. return;
  60. }
  61. if (dStricmp(*argv, "C") == 0) {
  62. properties |= CPU_PROP_C;
  63. continue;
  64. }
  65. if( dStricmp( *argv, "SSE" ) == 0 )
  66. {
  67. properties |= CPU_PROP_SSE;
  68. continue;
  69. }
  70. //Con::printf("Error: MathInit(): ignoring unknown math extension '%s'", *argv);
  71. }
  72. Math::init(properties);
  73. }
  74. //------------------------------------------------------------------------------
  75. void Math::init(U32 properties)
  76. {
  77. if (!properties)
  78. // detect what's available
  79. properties = Platform::SystemInfo.processor.properties;
  80. else
  81. // Make sure we're not asking for anything that's not supported
  82. properties &= Platform::SystemInfo.processor.properties;
  83. Con::printf("Math Init:");
  84. Con::printf(" Installing Standard C extensions");
  85. mInstallLibrary_C();
  86. #ifdef TORQUE_CPU_X86
  87. if( properties & CPU_PROP_SSE )
  88. {
  89. Con::printf( " Installing SSE extensions" );
  90. mInstall_Library_SSE();
  91. }
  92. #endif
  93. Con::printf(" ");
  94. }
  95. //------------------------------------------------------------------------------
  96. F32 Platform::getRandom()
  97. {
  98. return sgPlatRandom.randF();
  99. }