fast_square_root.inl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /// @ref gtx_fast_square_root
  2. namespace glm
  3. {
  4. // fastSqrt
  5. template<typename genType>
  6. GLM_FUNC_QUALIFIER genType fastSqrt(genType x)
  7. {
  8. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'fastSqrt' only accept floating-point input");
  9. return genType(1) / fastInverseSqrt(x);
  10. }
  11. template<length_t L, typename T, qualifier Q>
  12. GLM_FUNC_QUALIFIER vec<L, T, Q> fastSqrt(vec<L, T, Q> const& x)
  13. {
  14. return detail::functor1<vec, L, T, T, Q>::call(fastSqrt, x);
  15. }
  16. // fastInversesqrt
  17. template<typename genType>
  18. GLM_FUNC_QUALIFIER genType fastInverseSqrt(genType x)
  19. {
  20. # ifdef __CUDACC__ // Wordaround for a CUDA compiler bug up to CUDA6
  21. vec<1, T, Q> tmp(detail::compute_inversesqrt<tvec1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)));
  22. return tmp.x;
  23. # else
  24. return detail::compute_inversesqrt<1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)).x;
  25. # endif
  26. }
  27. template<length_t L, typename T, qualifier Q>
  28. GLM_FUNC_QUALIFIER vec<L, T, Q> fastInverseSqrt(vec<L, T, Q> const& x)
  29. {
  30. return detail::compute_inversesqrt<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  31. }
  32. // fastLength
  33. template<typename genType>
  34. GLM_FUNC_QUALIFIER genType fastLength(genType x)
  35. {
  36. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'fastLength' only accept floating-point inputs");
  37. return abs(x);
  38. }
  39. template<length_t L, typename T, qualifier Q>
  40. GLM_FUNC_QUALIFIER T fastLength(vec<L, T, Q> const& x)
  41. {
  42. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'fastLength' only accept floating-point inputs");
  43. return fastSqrt(dot(x, x));
  44. }
  45. // fastDistance
  46. template<typename genType>
  47. GLM_FUNC_QUALIFIER genType fastDistance(genType x, genType y)
  48. {
  49. return fastLength(y - x);
  50. }
  51. template<length_t L, typename T, qualifier Q>
  52. GLM_FUNC_QUALIFIER T fastDistance(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  53. {
  54. return fastLength(y - x);
  55. }
  56. // fastNormalize
  57. template<typename genType>
  58. GLM_FUNC_QUALIFIER genType fastNormalize(genType x)
  59. {
  60. return x > genType(0) ? genType(1) : -genType(1);
  61. }
  62. template<length_t L, typename T, qualifier Q>
  63. GLM_FUNC_QUALIFIER vec<L, T, Q> fastNormalize(vec<L, T, Q> const& x)
  64. {
  65. return x * fastInverseSqrt(dot(x, x));
  66. }
  67. }//namespace glm