mMathAltivec.cc 4.4 KB

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