testMatrixMul.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "platform/platform.h"
  23. #include "unit/test.h"
  24. #include "math/mMath.h"
  25. #include "math/mRandom.h"
  26. extern void default_matF_x_matF_C(const F32 *a, const F32 *b, F32 *mresult);
  27. extern void mInstallLibrary_ASM();
  28. // If we're x86 and not Mac, then include these. There's probably a better way to do this.
  29. #if defined(TORQUE_CPU_X86) && !defined(TORQUE_OS_MAC)
  30. extern "C" void Athlon_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
  31. extern "C" void SSE_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
  32. #endif
  33. #if defined( __VEC__ )
  34. extern void vec_MatrixF_x_MatrixF(const F32 *matA, const F32 *matB, F32 *result);
  35. #endif
  36. using namespace UnitTesting;
  37. CreateUnitTest( TestMatrixMul, "Math/Matrix/Multiply" )
  38. {
  39. // The purpose of this test is to verify that the matrix multiplication operation
  40. // always agrees with the different implementations of itself within a reasonable
  41. // epsilon.
  42. void run()
  43. {
  44. F32 m1[16], m2[16], mrC[16];
  45. // I am not positive that the best way to do this is to use random numbers
  46. // but I think that using some kind of standard matrix may not always catch
  47. // all problems.
  48. for( int i = 0; i < 16; i++ )
  49. {
  50. m1[i] = gRandGen.randF();
  51. m2[i] = gRandGen.randF();
  52. }
  53. // C will be the baseline
  54. default_matF_x_matF_C( m1, m2, mrC );
  55. #if defined(TORQUE_CPU_X86) && !defined(TORQUE_OS_MAC)
  56. // Check the CPU info
  57. U32 cpuProperties = Platform::SystemInfo.processor.properties;
  58. bool same = true;
  59. // Test 3D NOW! if it is available
  60. F32 mrAMD[16];
  61. if( cpuProperties & CPU_PROP_3DNOW )
  62. {
  63. Athlon_MatrixF_x_MatrixF( m1, m2, mrAMD );
  64. for( int i = 0; i < 16; i++ )
  65. same &= mIsEqual( mrC[i], mrAMD[i] );
  66. test( same, "Matrix multiplication verification failed. (C vs. 3D NOW!)" );
  67. }
  68. else
  69. warn( "Could not test 3D NOW! matrix multiplication because CPU does not support 3D NOW!." );
  70. same = true;
  71. // Test SSE if it is available
  72. F32 mrSSE[16];
  73. if( cpuProperties & CPU_PROP_SSE )
  74. {
  75. SSE_MatrixF_x_MatrixF( m1, m2, mrSSE );
  76. for( int i = 0; i < 16; i++ )
  77. same &= mIsEqual( mrC[i], mrSSE[i] );
  78. test( same, "Matrix multiplication verification failed. (C vs. SSE)" );
  79. }
  80. else
  81. warn( "Could not test SSE matrix multiplication because CPU does not support SSE." );
  82. same = true;
  83. #endif
  84. // If Altivec exists, test it!
  85. #if defined( __VEC__ )
  86. bool same = false;
  87. F32 mrVEC[16];
  88. vec_MatrixF_x_MatrixF( m1, m2, mrVEC );
  89. for( int i = 0; i < 16; i++ )
  90. same &= isEqual( mrC[i], mrVEC[i] );
  91. test( same, "Matrix multiplication verification failed. (C vs. Altivec)" );
  92. #else
  93. warn( "Could not test Altivec matrix multiplication because CPU does not support Altivec." );
  94. #endif
  95. }
  96. };