fast_square_root.inl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. return detail::compute_inversesqrt<1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)).x;
  21. }
  22. template<length_t L, typename T, qualifier Q>
  23. GLM_FUNC_QUALIFIER vec<L, T, Q> fastInverseSqrt(vec<L, T, Q> const& x)
  24. {
  25. return detail::compute_inversesqrt<L, T, Q, detail::is_aligned<Q>::value>::call(x);
  26. }
  27. // fastLength
  28. template<typename genType>
  29. GLM_FUNC_QUALIFIER genType fastLength(genType x)
  30. {
  31. GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'fastLength' only accept floating-point inputs");
  32. return abs(x);
  33. }
  34. template<length_t L, typename T, qualifier Q>
  35. GLM_FUNC_QUALIFIER T fastLength(vec<L, T, Q> const& x)
  36. {
  37. GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'fastLength' only accept floating-point inputs");
  38. return fastSqrt(dot(x, x));
  39. }
  40. // fastDistance
  41. template<typename genType>
  42. GLM_FUNC_QUALIFIER genType fastDistance(genType x, genType y)
  43. {
  44. return fastLength(y - x);
  45. }
  46. template<length_t L, typename T, qualifier Q>
  47. GLM_FUNC_QUALIFIER T fastDistance(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
  48. {
  49. return fastLength(y - x);
  50. }
  51. // fastNormalize
  52. template<typename genType>
  53. GLM_FUNC_QUALIFIER genType fastNormalize(genType x)
  54. {
  55. return x > genType(0) ? genType(1) : -genType(1);
  56. }
  57. template<length_t L, typename T, qualifier Q>
  58. GLM_FUNC_QUALIFIER vec<L, T, Q> fastNormalize(vec<L, T, Q> const& x)
  59. {
  60. return x * fastInverseSqrt(dot(x, x));
  61. }
  62. }//namespace glm