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