master
ブランチ master
ShaderPlayground / deps / glm / glm / gtx / fast_square_root.inl
2.2 KB、80行 生 Blame 履歴
1/// @ref gtx_fast_square_root
2
3namespace glm
4{
5 // fastSqrt
6 template<typename genType>
7 GLM_FUNC_QUALIFIER genType fastSqrt(genType x)
8 {
9 GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'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# ifdef __CUDACC__ // Wordaround for a CUDA compiler bug up to CUDA6
25 vec<1, T, Q> tmp(detail::compute_inversesqrt<tvec1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)));
26 return tmp.x;
27# else
28 return detail::compute_inversesqrt<1, genType, lowp, detail::is_aligned<lowp>::value>::call(vec<1, genType, lowp>(x)).x;
29# endif
30 }
31
32 template<length_t L, typename T, qualifier Q>
33 GLM_FUNC_QUALIFIER vec<L, T, Q> fastInverseSqrt(vec<L, T, Q> const& x)
34 {
35 return detail::compute_inversesqrt<L, T, Q, detail::is_aligned<Q>::value>::call(x);
36 }
37
38 // fastLength
39 template<typename genType>
40 GLM_FUNC_QUALIFIER genType fastLength(genType x)
41 {
42 GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'fastLength' only accept floating-point inputs");
43
44 return abs(x);
45 }
46
47 template<length_t L, typename T, qualifier Q>
48 GLM_FUNC_QUALIFIER T fastLength(vec<L, T, Q> const& x)
49 {
50 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'fastLength' only accept floating-point inputs");
51
52 return fastSqrt(dot(x, x));
53 }
54
55 // fastDistance
56 template<typename genType>
57 GLM_FUNC_QUALIFIER genType fastDistance(genType x, genType y)
58 {
59 return fastLength(y - x);
60 }
61
62 template<length_t L, typename T, qualifier Q>
63 GLM_FUNC_QUALIFIER T fastDistance(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
64 {
65 return fastLength(y - x);
66 }
67
68 // fastNormalize
69 template<typename genType>
70 GLM_FUNC_QUALIFIER genType fastNormalize(genType x)
71 {
72 return x > genType(0) ? genType(1) : -genType(1);
73 }
74
75 template<length_t L, typename T, qualifier Q>
76 GLM_FUNC_QUALIFIER vec<L, T, Q> fastNormalize(vec<L, T, Q> const& x)
77 {
78 return x * fastInverseSqrt(dot(x, x));
79 }
80}//namespace glm