You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
2.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2006-01-04
  5. // Updated : 2011-10-14
  6. // Licence : This source is under MIT License
  7. // File : glm/gtx/fast_square_root.inl
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. namespace glm
  10. {
  11. // fastSqrt
  12. template <typename genType>
  13. GLM_FUNC_QUALIFIER genType fastSqrt
  14. (
  15. genType const & x
  16. )
  17. {
  18. GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'fastSqrt' only accept floating-point input");
  19. return genType(1) / fastInverseSqrt(x);
  20. }
  21. VECTORIZE_VEC(fastSqrt)
  22. // fastInversesqrt
  23. template <typename genType>
  24. GLM_FUNC_QUALIFIER genType fastInverseSqrt
  25. (
  26. genType const & x
  27. )
  28. {
  29. genType tmp = x;
  30. float xhalf = 0.5f * float(tmp);
  31. uint i = *(uint*)&x;
  32. i = 0x5f375a86 - (i >> 1);
  33. //x = *(float*)&i;
  34. //x = *((float*)(char*)&i);
  35. tmp = detail::uif(i).f;
  36. tmp = tmp * (1.5f - xhalf * tmp * tmp);
  37. return genType(tmp);
  38. }
  39. VECTORIZE_VEC(fastInverseSqrt)
  40. // fastLength
  41. template <typename genType>
  42. GLM_FUNC_QUALIFIER genType fastLength
  43. (
  44. genType const & x
  45. )
  46. {
  47. return abs(x);
  48. }
  49. template <typename valType>
  50. GLM_FUNC_QUALIFIER valType fastLength
  51. (
  52. detail::tvec2<valType> const & x
  53. )
  54. {
  55. valType sqr = x.x * x.x + x.y * x.y;
  56. return fastSqrt(sqr);
  57. }
  58. template <typename valType>
  59. GLM_FUNC_QUALIFIER valType fastLength
  60. (
  61. detail::tvec3<valType> const & x
  62. )
  63. {
  64. valType sqr = x.x * x.x + x.y * x.y + x.z * x.z;
  65. return fastSqrt(sqr);
  66. }
  67. template <typename valType>
  68. GLM_FUNC_QUALIFIER valType fastLength
  69. (
  70. detail::tvec4<valType> const & x
  71. )
  72. {
  73. valType sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
  74. return fastSqrt(sqr);
  75. }
  76. // fastDistance
  77. template <typename genType>
  78. GLM_FUNC_QUALIFIER genType fastDistance
  79. (
  80. genType const & x,
  81. genType const & y
  82. )
  83. {
  84. return fastLength(y - x);
  85. }
  86. // fastNormalize
  87. template <typename genType>
  88. GLM_FUNC_QUALIFIER genType fastNormalize
  89. (
  90. genType const & x
  91. )
  92. {
  93. return x > genType(0) ? genType(1) : -genType(1);
  94. }
  95. template <typename valType>
  96. GLM_FUNC_QUALIFIER detail::tvec2<valType> fastNormalize
  97. (
  98. detail::tvec2<valType> const & x
  99. )
  100. {
  101. valType sqr = x.x * x.x + x.y * x.y;
  102. return x * fastInverseSqrt(sqr);
  103. }
  104. template <typename valType>
  105. GLM_FUNC_QUALIFIER detail::tvec3<valType> fastNormalize
  106. (
  107. detail::tvec3<valType> const & x
  108. )
  109. {
  110. valType sqr = x.x * x.x + x.y * x.y + x.z * x.z;
  111. return x * fastInverseSqrt(sqr);
  112. }
  113. template <typename valType>
  114. GLM_FUNC_QUALIFIER detail::tvec4<valType> fastNormalize
  115. (
  116. detail::tvec4<valType> const & x
  117. )
  118. {
  119. valType sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
  120. return x * fastInverseSqrt(sqr);
  121. }
  122. }//namespace glm