geometric.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /// @ref simd
  2. /// @file glm/simd/geometric.h
  3. #pragma once
  4. #include "common.h"
  5. #if GLM_ARCH & GLM_ARCH_SSE2_BIT
  6. GLM_FUNC_DECL glm_vec4 glm_vec4_dot(glm_vec4 v1, glm_vec4 v2);
  7. GLM_FUNC_DECL glm_vec4 glm_vec1_dot(glm_vec4 v1, glm_vec4 v2);
  8. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_length(glm_vec4 x)
  9. {
  10. glm_vec4 const dot0 = glm_vec4_dot(x, x);
  11. glm_vec4 const sqt0 = _mm_sqrt_ps(dot0);
  12. return sqt0;
  13. }
  14. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_distance(glm_vec4 p0, glm_vec4 p1)
  15. {
  16. glm_vec4 const sub0 = _mm_sub_ps(p0, p1);
  17. glm_vec4 const len0 = glm_vec4_length(sub0);
  18. return len0;
  19. }
  20. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_dot(glm_vec4 v1, glm_vec4 v2)
  21. {
  22. # if GLM_ARCH & GLM_ARCH_AVX_BIT
  23. return _mm_dp_ps(v1, v2, 0xff);
  24. # elif GLM_ARCH & GLM_ARCH_SSE3_BIT
  25. glm_vec4 const mul0 = _mm_mul_ps(v1, v2);
  26. glm_vec4 const hadd0 = _mm_hadd_ps(mul0, mul0);
  27. glm_vec4 const hadd1 = _mm_hadd_ps(hadd0, hadd0);
  28. return hadd1;
  29. # else
  30. glm_vec4 const mul0 = _mm_mul_ps(v1, v2);
  31. glm_vec4 const swp0 = _mm_shuffle_ps(mul0, mul0, _MM_SHUFFLE(2, 3, 0, 1));
  32. glm_vec4 const add0 = _mm_add_ps(mul0, swp0);
  33. glm_vec4 const swp1 = _mm_shuffle_ps(add0, add0, _MM_SHUFFLE(0, 1, 2, 3));
  34. glm_vec4 const add1 = _mm_add_ps(add0, swp1);
  35. return add1;
  36. # endif
  37. }
  38. GLM_FUNC_QUALIFIER glm_vec4 glm_vec1_dot(glm_vec4 v1, glm_vec4 v2)
  39. {
  40. # if GLM_ARCH & GLM_ARCH_AVX_BIT
  41. return _mm_dp_ps(v1, v2, 0xff);
  42. # elif GLM_ARCH & GLM_ARCH_SSE3_BIT
  43. glm_vec4 const mul0 = _mm_mul_ps(v1, v2);
  44. glm_vec4 const had0 = _mm_hadd_ps(mul0, mul0);
  45. glm_vec4 const had1 = _mm_hadd_ps(had0, had0);
  46. return had1;
  47. # else
  48. glm_vec4 const mul0 = _mm_mul_ps(v1, v2);
  49. glm_vec4 const mov0 = _mm_movehl_ps(mul0, mul0);
  50. glm_vec4 const add0 = _mm_add_ps(mov0, mul0);
  51. glm_vec4 const swp1 = _mm_shuffle_ps(add0, add0, 1);
  52. glm_vec4 const add1 = _mm_add_ss(add0, swp1);
  53. return add1;
  54. # endif
  55. }
  56. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_cross(glm_vec4 v1, glm_vec4 v2)
  57. {
  58. glm_vec4 const swp0 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(3, 0, 2, 1));
  59. glm_vec4 const swp1 = _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(3, 1, 0, 2));
  60. glm_vec4 const swp2 = _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(3, 0, 2, 1));
  61. glm_vec4 const swp3 = _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(3, 1, 0, 2));
  62. glm_vec4 const mul0 = _mm_mul_ps(swp0, swp3);
  63. glm_vec4 const mul1 = _mm_mul_ps(swp1, swp2);
  64. glm_vec4 const sub0 = _mm_sub_ps(mul0, mul1);
  65. return sub0;
  66. }
  67. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_normalize(glm_vec4 v)
  68. {
  69. glm_vec4 const dot0 = glm_vec4_dot(v, v);
  70. glm_vec4 const isr0 = _mm_rsqrt_ps(dot0);
  71. glm_vec4 const mul0 = _mm_mul_ps(v, isr0);
  72. return mul0;
  73. }
  74. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_faceforward(glm_vec4 N, glm_vec4 I, glm_vec4 Nref)
  75. {
  76. glm_vec4 const dot0 = glm_vec4_dot(Nref, I);
  77. glm_vec4 const sgn0 = glm_vec4_sign(dot0);
  78. glm_vec4 const mul0 = _mm_mul_ps(sgn0, _mm_set1_ps(-1.0f));
  79. glm_vec4 const mul1 = _mm_mul_ps(N, mul0);
  80. return mul1;
  81. }
  82. GLM_FUNC_QUALIFIER glm_vec4 glm_vec4_reflect(glm_vec4 I, glm_vec4 N)
  83. {
  84. glm_vec4 const dot0 = glm_vec4_dot(N, I);
  85. glm_vec4 const mul0 = _mm_mul_ps(N, dot0);
  86. glm_vec4 const mul1 = _mm_mul_ps(mul0, _mm_set1_ps(2.0f));
  87. glm_vec4 const sub0 = _mm_sub_ps(I, mul1);
  88. return sub0;
  89. }
  90. GLM_FUNC_QUALIFIER __m128 glm_vec4_refract(glm_vec4 I, glm_vec4 N, glm_vec4 eta)
  91. {
  92. glm_vec4 const dot0 = glm_vec4_dot(N, I);
  93. glm_vec4 const mul0 = _mm_mul_ps(eta, eta);
  94. glm_vec4 const mul1 = _mm_mul_ps(dot0, dot0);
  95. glm_vec4 const sub0 = _mm_sub_ps(_mm_set1_ps(1.0f), mul0);
  96. glm_vec4 const sub1 = _mm_sub_ps(_mm_set1_ps(1.0f), mul1);
  97. glm_vec4 const mul2 = _mm_mul_ps(sub0, sub1);
  98. if(_mm_movemask_ps(_mm_cmplt_ss(mul2, _mm_set1_ps(0.0f))) == 0)
  99. return _mm_set1_ps(0.0f);
  100. glm_vec4 const sqt0 = _mm_sqrt_ps(mul2);
  101. glm_vec4 const mad0 = glm_vec4_fma(eta, dot0, sqt0);
  102. glm_vec4 const mul4 = _mm_mul_ps(mad0, N);
  103. glm_vec4 const mul5 = _mm_mul_ps(eta, I);
  104. glm_vec4 const sub2 = _mm_sub_ps(mul5, mul4);
  105. return sub2;
  106. }
  107. #endif//GLM_ARCH & GLM_ARCH_SSE2_BIT