mMathAltivec.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // This file is Mac specific.
  23. #if defined( __APPLE__ )
  24. #include <vecLib/vecLib.h>
  25. #include "math/mMathFn.h"
  26. #include "console/console.h"
  27. #include "platform/profiler.h"
  28. #if defined( __VEC__ )
  29. // tests show BLAS to be about 4x slower than aligned altivec, 3x slower than unaligned altivec code below.
  30. /// Altivec 4x4 Matrix multiplication.
  31. /// Most of our time is spent moving data in & out of the vector registers.
  32. /// Alignment of the matrix data to 16-byte boundaries is very important,
  33. /// because we get a much better speed gain if we can assume the data is aligned.
  34. void vec_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result)
  35. {
  36. vector F32 A[4][1];
  37. vector F32 B[4][1];
  38. vector F32 C[4][1];
  39. /// If the incoming pointers are not 16-byte aligned, we have to load & store the slow way.
  40. if((int)matA & 0xF || (int)matB & 0xF || (int)result & 0xF)
  41. {
  42. F32 *loader;
  43. loader = (F32*) &A;
  44. loader[0] = matA[0];
  45. loader[1] = matA[1];
  46. loader[2] = matA[2];
  47. loader[3] = matA[3];
  48. loader[4] = matA[4];
  49. loader[5] = matA[5];
  50. loader[6] = matA[6];
  51. loader[7] = matA[7];
  52. loader[8] = matA[8];
  53. loader[9] = matA[9];
  54. loader[10] = matA[10];
  55. loader[11] = matA[11];
  56. loader[12] = matA[12];
  57. loader[13] = matA[13];
  58. loader[14] = matA[14];
  59. loader[15] = matA[15];
  60. loader = (F32*) &B;
  61. loader[0] = matB[0];
  62. loader[1] = matB[1];
  63. loader[2] = matB[2];
  64. loader[3] = matB[3];
  65. loader[4] = matB[4];
  66. loader[5] = matB[5];
  67. loader[6] = matB[6];
  68. loader[7] = matB[7];
  69. loader[8] = matB[8];
  70. loader[9] = matB[9];
  71. loader[10] = matB[10];
  72. loader[11] = matB[11];
  73. loader[12] = matB[12];
  74. loader[13] = matB[13];
  75. loader[14] = matB[14];
  76. loader[15] = matB[15];
  77. vMultMatMat_4x4( A, B, C);
  78. loader = (F32*) &C;
  79. result[0] = loader[0];
  80. result[1] = loader[1];
  81. result[2] = loader[2];
  82. result[3] = loader[3];
  83. result[4] = loader[4];
  84. result[5] = loader[5];
  85. result[6] = loader[6];
  86. result[7] = loader[7];
  87. result[8] = loader[8];
  88. result[9] = loader[9];
  89. result[10] = loader[10];
  90. result[11] = loader[11];
  91. result[12] = loader[12];
  92. result[13] = loader[13];
  93. result[14] = loader[14];
  94. result[15] = loader[15];
  95. }
  96. else
  97. {
  98. A[0][0] = vec_ld(0, matA);
  99. A[1][0] = vec_ld(16, matA);
  100. A[2][0] = vec_ld(32, matA);
  101. A[3][0] = vec_ld(48, matA);
  102. B[0][0] = vec_ld(0, matB);
  103. B[1][0] = vec_ld(16, matB);
  104. B[2][0] = vec_ld(32, matB);
  105. B[3][0] = vec_ld(48, matB);
  106. vMultMatMat_4x4( A, B, C);
  107. vec_st(C[0][0], 0, result);
  108. vec_st(C[1][0], 16, result);
  109. vec_st(C[2][0], 32, result);
  110. vec_st(C[3][0], 48, result);
  111. }
  112. }
  113. void mInstallLibrary_Vec()
  114. {
  115. m_matF_x_matF = vec_MatrixF_x_MatrixF;
  116. }
  117. #else // defined(__VEC__)
  118. void mInstallLibrary_Vec()
  119. {
  120. Con::warnf("Cannot use altivec math, this build does not support altivec.");
  121. }
  122. #endif// defined(__VEC__)
  123. #endif// defined(__APPLE__)