perf_matrix_div.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #define GLM_FORCE_INLINE
  2. #include <glm/ext/matrix_float4x4.hpp>
  3. #include <glm/ext/matrix_double4x4.hpp>
  4. #include <glm/ext/matrix_transform.hpp>
  5. #include <glm/ext/matrix_relational.hpp>
  6. #include <glm/ext/vector_float4.hpp>
  7. #if GLM_CONFIG_SIMD == GLM_ENABLE
  8. #include <glm/gtc/type_aligned.hpp>
  9. #include <vector>
  10. #include <chrono>
  11. #include <cstdio>
  12. template <typename matType>
  13. static void test_mat_div_mat(matType const& M, std::vector<matType> const& I, std::vector<matType>& O)
  14. {
  15. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  16. O[i] = M / I[i];
  17. }
  18. template <typename matType>
  19. static int launch_mat_div_mat(std::vector<matType>& O, matType const& Transform, matType const& Scale, std::size_t Samples)
  20. {
  21. typedef typename matType::value_type T;
  22. std::vector<matType> I(Samples);
  23. O.resize(Samples);
  24. for(std::size_t i = 0; i < Samples; ++i)
  25. I[i] = Scale * static_cast<T>(i) + Scale;
  26. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  27. test_mat_div_mat<matType>(Transform, I, O);
  28. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  29. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  30. }
  31. template <typename packedMatType, typename alignedMatType>
  32. static int comp_mat2_div_mat2(std::size_t Samples)
  33. {
  34. typedef typename packedMatType::value_type T;
  35. int Error = 0;
  36. packedMatType const Transform(1, 2, 3, 4);
  37. packedMatType const Scale(0.01, 0.02, 0.03, 0.05);
  38. std::vector<packedMatType> SISD;
  39. std::printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  40. std::vector<alignedMatType> SIMD;
  41. std::printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  42. for(std::size_t i = 0; i < Samples; ++i)
  43. {
  44. packedMatType const A = SISD[i];
  45. packedMatType const B = SIMD[i];
  46. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  47. assert(!Error);
  48. }
  49. return Error;
  50. }
  51. template <typename packedMatType, typename alignedMatType>
  52. static int comp_mat3_div_mat3(std::size_t Samples)
  53. {
  54. typedef typename packedMatType::value_type T;
  55. int Error = 0;
  56. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  57. packedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01);
  58. std::vector<packedMatType> SISD;
  59. std::printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  60. std::vector<alignedMatType> SIMD;
  61. std::printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  62. for(std::size_t i = 0; i < Samples; ++i)
  63. {
  64. packedMatType const A = SISD[i];
  65. packedMatType const B = SIMD[i];
  66. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  67. assert(!Error);
  68. }
  69. return Error;
  70. }
  71. template <typename packedMatType, typename alignedMatType>
  72. static int comp_mat4_div_mat4(std::size_t Samples)
  73. {
  74. typedef typename packedMatType::value_type T;
  75. int Error = 0;
  76. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  77. packedMatType const Scale(0.01, 0.02, 0.05, 0.04, 0.02, 0.08, 0.05, 0.01, 0.08, 0.03, 0.05, 0.06, 0.02, 0.03, 0.07, 0.05);
  78. std::vector<packedMatType> SISD;
  79. std::printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  80. std::vector<alignedMatType> SIMD;
  81. std::printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  82. for(std::size_t i = 0; i < Samples; ++i)
  83. {
  84. packedMatType const A = SISD[i];
  85. packedMatType const B = SIMD[i];
  86. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  87. assert(!Error);
  88. }
  89. return Error;
  90. }
  91. int main()
  92. {
  93. std::size_t const Samples = 1000;
  94. int Error = 0;
  95. std::printf("mat2 / mat2:\n");
  96. Error += comp_mat2_div_mat2<glm::mat2, glm::aligned_mat2>(Samples);
  97. std::printf("dmat2 / dmat2:\n");
  98. Error += comp_mat2_div_mat2<glm::dmat2, glm::aligned_dmat2>(Samples);
  99. std::printf("mat3 / mat3:\n");
  100. Error += comp_mat3_div_mat3<glm::mat3, glm::aligned_mat3>(Samples);
  101. std::printf("dmat3 / dmat3:\n");
  102. Error += comp_mat3_div_mat3<glm::dmat3, glm::aligned_dmat3>(Samples);
  103. std::printf("mat4 / mat4:\n");
  104. Error += comp_mat4_div_mat4<glm::mat4, glm::aligned_mat4>(Samples);
  105. std::printf("dmat4 / dmat4:\n");
  106. Error += comp_mat4_div_mat4<glm::dmat4, glm::aligned_dmat4>(Samples);
  107. return Error;
  108. }
  109. #else
  110. int main()
  111. {
  112. return 0;
  113. }
  114. #endif