| 1 | /// @ref gtx_fast_square_root |
| 2 | |
| 3 | namespace glm |
| 4 | { |
| 5 | // fastSqrt |
| 6 | template<typename genType> |
| 7 | GLM_FUNC_QUALIFIER genType fastSqrt(genType x) |
| 8 | { |
| 9 | static_assert(std::numeric_limits<genType>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'fastSqrt' only accept floating-point input"); |
| 10 | |
| 11 | return genType(1) / fastInverseSqrt(x); |
| 12 | } |
| 13 | |
| 14 | template<length_t L, typename T, qualifier Q> |
| 15 | GLM_FUNC_QUALIFIER vec<L, T, Q> fastSqrt(vec<L, T, Q> const& x) |
| 16 | { |
| 17 | return detail::functor1<vec, L, T, T, Q>::call(fastSqrt, x); |
| 18 | } |
| 19 | |
| 20 | // fastInversesqrt |
| 21 | template<typename genType> |
| 22 | GLM_FUNC_QUALIFIER genType fastInverseSqrt(genType x) |
| 23 | { |
| 24 | return detail::compute_inversesqrt<1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)).x; |
| 25 | } |
| 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 | |
| 33 | // fastLength |
| 34 | template<typename genType> |
| 35 | GLM_FUNC_QUALIFIER genType fastLength(genType x) |
| 36 | { |
| 37 | static_assert(std::numeric_limits<genType>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'fastLength' only accept floating-point inputs"); |
| 38 | |
| 39 | return abs(x); |
| 40 | } |
| 41 | |
| 42 | template<length_t L, typename T, qualifier Q> |
| 43 | GLM_FUNC_QUALIFIER T fastLength(vec<L, T, Q> const& x) |
| 44 | { |
| 45 | static_assert(std::numeric_limits<T>::is_iec559 || GLM_CONFIG_UNRESTRICTED_FLOAT, "'fastLength' only accept floating-point inputs"); |
| 46 | |
| 47 | return fastSqrt(dot(x, x)); |
| 48 | } |
| 49 | |
| 50 | // fastDistance |
| 51 | template<typename genType> |
| 52 | GLM_FUNC_QUALIFIER genType fastDistance(genType x, genType y) |
| 53 | { |
| 54 | return fastLength(y - x); |
| 55 | } |
| 56 | |
| 57 | template<length_t L, typename T, qualifier Q> |
| 58 | GLM_FUNC_QUALIFIER T fastDistance(vec<L, T, Q> const& x, vec<L, T, Q> const& y) |
| 59 | { |
| 60 | return fastLength(y - x); |
| 61 | } |
| 62 | |
| 63 | // fastNormalize |
| 64 | template<typename genType> |
| 65 | GLM_FUNC_QUALIFIER genType fastNormalize(genType x) |
| 66 | { |
| 67 | return x > genType(0) ? genType(1) : -genType(1); |
| 68 | } |
| 69 | |
| 70 | template<length_t L, typename T, qualifier Q> |
| 71 | GLM_FUNC_QUALIFIER vec<L, T, Q> fastNormalize(vec<L, T, Q> const& x) |
| 72 | { |
| 73 | return x * fastInverseSqrt(dot(x, x)); |
| 74 | } |
| 75 | }//namespace glm |