1/// @ref core
2/// @file glm/detail/func_exponential.inl
3
4#include "func_vector_relational.hpp"
5#include "_vectorize.hpp"
6#include <limits>
7#include <cmath>
8#include <cassert>
9
10namespace glm{
11namespace detail
12{
13#	if GLM_HAS_CXX11_STL
14		using std::log2;
15#	else
16		template <typename genType>
17		genType log2(genType Value)
18		{
19			return std::log(Value) * static_cast<genType>(1.4426950408889634073599246810019);
20		}
21#	endif
22
23	template <typename T, precision P, template <class, precision> class vecType, bool isFloat, bool Aligned>
24	struct compute_log2
25	{
26		GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & vec)
27		{
28			return detail::functor1<T, T, P, vecType>::call(log2, vec);
29		}
30	};
31
32	template <template <class, precision> class vecType, typename T, precision P, bool Aligned>
33	struct compute_sqrt
34	{
35		GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & x)
36		{
37			return detail::functor1<T, T, P, vecType>::call(std::sqrt, x);
38		}
39	};
40
41	template <template <class, precision> class vecType, typename T, precision P, bool Aligned>
42	struct compute_inversesqrt
43	{
44		GLM_FUNC_QUALIFIER static vecType<T, P> call(vecType<T, P> const & x)
45		{
46			return static_cast<T>(1) / sqrt(x);
47		}
48	};
49
50	template <template <class, precision> class vecType, bool Aligned>
51	struct compute_inversesqrt<vecType, float, lowp, Aligned>
52	{
53		GLM_FUNC_QUALIFIER static vecType<float, lowp> call(vecType<float, lowp> const & x)
54		{
55			vecType<float, lowp> tmp(x);
56			vecType<float, lowp> xhalf(tmp * 0.5f);
57			vecType<uint, lowp>* p = reinterpret_cast<vecType<uint, lowp>*>(const_cast<vecType<float, lowp>*>(&x));
58			vecType<uint, lowp> i = vecType<uint, lowp>(0x5f375a86) - (*p >> vecType<uint, lowp>(1));
59			vecType<float, lowp>* ptmp = reinterpret_cast<vecType<float, lowp>*>(&i);
60			tmp = *ptmp;
61			tmp = tmp * (1.5f - xhalf * tmp * tmp);
62			return tmp;
63		}
64	};
65}//namespace detail
66
67	// pow
68	using std::pow;
69	template <typename T, precision P, template <typename, precision> class vecType>
70	GLM_FUNC_QUALIFIER vecType<T, P> pow(vecType<T, P> const & base, vecType<T, P> const & exponent)
71	{
72		return detail::functor2<T, P, vecType>::call(pow, base, exponent);
73	}
74
75	// exp
76	using std::exp;
77	template <typename T, precision P, template <typename, precision> class vecType>
78	GLM_FUNC_QUALIFIER vecType<T, P> exp(vecType<T, P> const & x)
79	{
80		return detail::functor1<T, T, P, vecType>::call(exp, x);
81	}
82
83	// log
84	using std::log;
85	template <typename T, precision P, template <typename, precision> class vecType>
86	GLM_FUNC_QUALIFIER vecType<T, P> log(vecType<T, P> const & x)
87	{
88		return detail::functor1<T, T, P, vecType>::call(log, x);
89	}
90
91	//exp2, ln2 = 0.69314718055994530941723212145818f
92	template <typename genType>
93	GLM_FUNC_QUALIFIER genType exp2(genType x)
94	{
95		GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'exp2' only accept floating-point inputs");
96
97		return std::exp(static_cast<genType>(0.69314718055994530941723212145818) * x);
98	}
99
100	template <typename T, precision P, template <typename, precision> class vecType>
101	GLM_FUNC_QUALIFIER vecType<T, P> exp2(vecType<T, P> const & x)
102	{
103		return detail::functor1<T, T, P, vecType>::call(exp2, x);
104	}
105
106	// log2, ln2 = 0.69314718055994530941723212145818f
107	template <typename genType>
108	GLM_FUNC_QUALIFIER genType log2(genType x)
109	{
110		return log2(tvec1<genType>(x)).x;
111	}
112
113	template <typename T, precision P, template <typename, precision> class vecType>
114	GLM_FUNC_QUALIFIER vecType<T, P> log2(vecType<T, P> const & x)
115	{
116		return detail::compute_log2<T, P, vecType, std::numeric_limits<T>::is_iec559, detail::is_aligned<P>::value>::call(x);
117	}
118
119	// sqrt
120	using std::sqrt;
121	template <typename T, precision P, template <typename, precision> class vecType>
122	GLM_FUNC_QUALIFIER vecType<T, P> sqrt(vecType<T, P> const & x)
123	{
124		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'sqrt' only accept floating-point inputs");
125		return detail::compute_sqrt<vecType, T, P, detail::is_aligned<P>::value>::call(x);
126	}
127
128	// inversesqrt
129	template <typename genType>
130	GLM_FUNC_QUALIFIER genType inversesqrt(genType x)
131	{
132		return static_cast<genType>(1) / sqrt(x);
133	}
134
135	template <typename T, precision P, template <typename, precision> class vecType>
136	GLM_FUNC_QUALIFIER vecType<T, P> inversesqrt(vecType<T, P> const & x)
137	{
138		GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'inversesqrt' only accept floating-point inputs");
139		return detail::compute_inversesqrt<vecType, T, P, detail::is_aligned<P>::value>::call(x);
140	}
141}//namespace glm
142
143#if GLM_ARCH != GLM_ARCH_PURE && GLM_HAS_UNRESTRICTED_UNIONS
144#	include "func_exponential_simd.inl"
145#endif
146
147