1///////////////////////////////////////////////////////////////////////////////////////////////////
2// OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
3///////////////////////////////////////////////////////////////////////////////////////////////////
4// Created : 2006-01-04
5// Updated : 2011-10-14
6// Licence : This source is under MIT License
7// File    : glm/gtx/fast_square_root.inl
8///////////////////////////////////////////////////////////////////////////////////////////////////
9
10namespace glm
11{
12	// fastSqrt
13	template <typename genType>
14	GLM_FUNC_QUALIFIER genType fastSqrt
15	(
16		genType const & x
17	)
18	{
19		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'fastSqrt' only accept floating-point input");
20
21		return genType(1) / fastInverseSqrt(x);
22	}
23
24	VECTORIZE_VEC(fastSqrt)
25
26	// fastInversesqrt
27	template <>
28	GLM_FUNC_QUALIFIER float fastInverseSqrt<float>(float const & x)
29	{
30#		ifdef __CUDACC__ // Wordaround for a CUDA compiler bug up to CUDA6
31			detail::tvec1<T, P> tmp(detail::compute_inversesqrt<detail::tvec1, float, lowp>::call(detail::tvec1<float, lowp>(x)));
32			return tmp.x;
33#		else
34			return detail::compute_inversesqrt<detail::tvec1, float, lowp>::call(detail::tvec1<float, lowp>(x)).x;
35#		endif
36	}
37
38	template <>
39	GLM_FUNC_QUALIFIER double fastInverseSqrt<double>(double const & x)
40	{
41#		ifdef __CUDACC__ // Wordaround for a CUDA compiler bug up to CUDA6
42			detail::tvec1<T, P> tmp(detail::compute_inversesqrt<detail::tvec1, double, lowp>::call(detail::tvec1<double, lowp>(x)));
43			return tmp.x;
44#		else
45			return detail::compute_inversesqrt<detail::tvec1, double, lowp>::call(detail::tvec1<double, lowp>(x)).x;
46#		endif
47	}
48
49	template <template <class, precision> class vecType, typename T, precision P>
50	GLM_FUNC_QUALIFIER vecType<T, P> fastInverseSqrt
51	(
52		vecType<T, P> const & x
53	)
54	{
55		return detail::compute_inversesqrt<vecType, T, P>::call(x);
56	}
57
58	VECTORIZE_VEC(fastInverseSqrt)
59
60	// fastLength
61	template <typename genType>
62	GLM_FUNC_QUALIFIER genType fastLength
63	(
64		genType const & x
65	)
66	{
67		return abs(x);
68	}
69
70	template <typename valType, precision P>
71	GLM_FUNC_QUALIFIER valType fastLength
72	(
73		detail::tvec2<valType, P> const & x
74	)
75	{
76		valType sqr = x.x * x.x + x.y * x.y;
77		return fastSqrt(sqr);
78	}
79
80	template <typename valType, precision P>
81	GLM_FUNC_QUALIFIER valType fastLength
82	(
83		detail::tvec3<valType, P> const & x
84	)
85	{
86		valType sqr = x.x * x.x + x.y * x.y + x.z * x.z;
87		return fastSqrt(sqr);
88	}
89
90	template <typename valType, precision P>
91	GLM_FUNC_QUALIFIER valType fastLength
92	(
93		detail::tvec4<valType, P> const & x
94	)
95	{
96		valType sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
97		return fastSqrt(sqr);
98	}
99
100	// fastDistance
101	template <typename genType>
102	GLM_FUNC_QUALIFIER genType fastDistance
103	(
104		genType const & x,
105		genType const & y
106	)
107	{
108		return fastLength(y - x);
109	}
110
111	// fastNormalize
112	template <typename genType>
113	GLM_FUNC_QUALIFIER genType fastNormalize
114	(
115		genType const & x
116	)
117	{
118		return x > genType(0) ? genType(1) : -genType(1);
119	}
120
121	template <typename valType, precision P>
122	GLM_FUNC_QUALIFIER detail::tvec2<valType, P> fastNormalize
123	(
124		detail::tvec2<valType, P> const & x
125	)
126	{
127		valType sqr = x.x * x.x + x.y * x.y;
128		return x * fastInverseSqrt(sqr);
129	}
130
131	template <typename valType, precision P>
132	GLM_FUNC_QUALIFIER detail::tvec3<valType, P> fastNormalize
133	(
134		detail::tvec3<valType, P> const & x
135	)
136	{
137		valType sqr = x.x * x.x + x.y * x.y + x.z * x.z;
138		return x * fastInverseSqrt(sqr);
139	}
140
141	template <typename valType, precision P>
142	GLM_FUNC_QUALIFIER detail::tvec4<valType, P> fastNormalize
143	(
144		detail::tvec4<valType, P> const & x
145	)
146	{
147		valType sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w;
148		return x * fastInverseSqrt(sqr);
149	}
150}//namespace glm
151