• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/// @ref gtx_fast_exponential
2/// @file glm/gtx/fast_exponential.inl
3
4namespace glm
5{
6	// fastPow:
7	template <typename genType>
8	GLM_FUNC_QUALIFIER genType fastPow(genType x, genType y)
9	{
10		return exp(y * log(x));
11	}
12
13	template <typename T, precision P, template <typename, precision> class vecType>
14	GLM_FUNC_QUALIFIER vecType<T, P> fastPow(vecType<T, P> const & x, vecType<T, P> const & y)
15	{
16		return exp(y * log(x));
17	}
18
19	template <typename T>
20	GLM_FUNC_QUALIFIER T fastPow(T x, int y)
21	{
22		T f = static_cast<T>(1);
23		for(int i = 0; i < y; ++i)
24			f *= x;
25		return f;
26	}
27
28	template <typename T, precision P, template <typename, precision> class vecType>
29	GLM_FUNC_QUALIFIER vecType<T, P> fastPow(vecType<T, P> const & x, vecType<int, P> const & y)
30	{
31		vecType<T, P> Result(uninitialize);
32		for(length_t i = 0, n = x.length(); i < n; ++i)
33			Result[i] = fastPow(x[i], y[i]);
34		return Result;
35	}
36
37	// fastExp
38	// Note: This function provides accurate results only for value between -1 and 1, else avoid it.
39	template <typename T>
40	GLM_FUNC_QUALIFIER T fastExp(T x)
41	{
42		// This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
43		// return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
44		T x2 = x * x;
45		T x3 = x2 * x;
46		T x4 = x3 * x;
47		T x5 = x4 * x;
48		return T(1) + x + (x2 * T(0.5)) + (x3 * T(0.1666666667)) + (x4 * T(0.041666667)) + (x5 * T(0.008333333333));
49	}
50	/*  // Try to handle all values of float... but often shower than std::exp, glm::floor and the loop kill the performance
51	GLM_FUNC_QUALIFIER float fastExp(float x)
52	{
53		const float e = 2.718281828f;
54		const float IntegerPart = floor(x);
55		const float FloatPart = x - IntegerPart;
56		float z = 1.f;
57
58		for(int i = 0; i < int(IntegerPart); ++i)
59			z *= e;
60
61		const float x2 = FloatPart * FloatPart;
62		const float x3 = x2 * FloatPart;
63		const float x4 = x3 * FloatPart;
64		const float x5 = x4 * FloatPart;
65		return z * (1.0f + FloatPart + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f));
66	}
67
68	// Increase accuracy on number bigger that 1 and smaller than -1 but it's not enough for high and negative numbers
69	GLM_FUNC_QUALIFIER float fastExp(float x)
70	{
71		// This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
72		// return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
73		float x2 = x * x;
74		float x3 = x2 * x;
75		float x4 = x3 * x;
76		float x5 = x4 * x;
77		float x6 = x5 * x;
78		float x7 = x6 * x;
79		float x8 = x7 * x;
80		return 1.0f + x + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f)+ (x6 * 0.00138888888888f) + (x7 * 0.000198412698f) + (x8 * 0.0000248015873f);;
81	}
82	*/
83
84	template <typename T, precision P, template <typename, precision> class vecType>
85	GLM_FUNC_QUALIFIER vecType<T, P> fastExp(vecType<T, P> const & x)
86	{
87		return detail::functor1<T, T, P, vecType>::call(fastExp, x);
88	}
89
90	// fastLog
91	template <typename genType>
92	GLM_FUNC_QUALIFIER genType fastLog(genType x)
93	{
94		return std::log(x);
95	}
96
97	/* Slower than the VC7.1 function...
98	GLM_FUNC_QUALIFIER float fastLog(float x)
99	{
100		float y1 = (x - 1.0f) / (x + 1.0f);
101		float y2 = y1 * y1;
102		return 2.0f * y1 * (1.0f + y2 * (0.3333333333f + y2 * (0.2f + y2 * 0.1428571429f)));
103	}
104	*/
105
106	template <typename T, precision P, template <typename, precision> class vecType>
107	GLM_FUNC_QUALIFIER vecType<T, P> fastLog(vecType<T, P> const & x)
108	{
109		return detail::functor1<T, T, P, vecType>::call(fastLog, x);
110	}
111
112	//fastExp2, ln2 = 0.69314718055994530941723212145818f
113	template <typename genType>
114	GLM_FUNC_QUALIFIER genType fastExp2(genType x)
115	{
116		return fastExp(0.69314718055994530941723212145818f * x);
117	}
118
119	template <typename T, precision P, template <typename, precision> class vecType>
120	GLM_FUNC_QUALIFIER vecType<T, P> fastExp2(vecType<T, P> const & x)
121	{
122		return detail::functor1<T, T, P, vecType>::call(fastExp2, x);
123	}
124
125	// fastLog2, ln2 = 0.69314718055994530941723212145818f
126	template <typename genType>
127	GLM_FUNC_QUALIFIER genType fastLog2(genType x)
128	{
129		return fastLog(x) / 0.69314718055994530941723212145818f;
130	}
131
132	template <typename T, precision P, template <typename, precision> class vecType>
133	GLM_FUNC_QUALIFIER vecType<T, P> fastLog2(vecType<T, P> const & x)
134	{
135		return detail::functor1<T, T, P, vecType>::call(fastLog2, x);
136	}
137}//namespace glm
138