Home | History | Annotate | Download | only in gtx
      1 ///////////////////////////////////////////////////////////////////////////////////////////////////
      2 // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
      3 ///////////////////////////////////////////////////////////////////////////////////////////////////
      4 // Created : 2006-01-09
      5 // Updated : 2006-01-09
      6 // Licence : This source is under MIT License
      7 // File    : glm/gtx/fast_exponential.inl
      8 ///////////////////////////////////////////////////////////////////////////////////////////////////
      9 
     10 namespace glm
     11 {
     12 	// fastPow:
     13 	template <typename genType>
     14 	GLM_FUNC_QUALIFIER genType fastPow(genType const & x, genType const & y)
     15 	{
     16 		return exp(y * log(x));
     17 	}
     18 
     19 	VECTORIZE_VEC_VEC(fastPow)
     20 
     21 	template <typename T>
     22 	GLM_FUNC_QUALIFIER T fastPow(const T x, int y)
     23 	{
     24 		T f = static_cast<T>(1);
     25 		for(int i = 0; i < y; ++i)
     26 			f *= x;
     27 		return f;
     28 	}
     29 
     30 	template <typename T, precision P>
     31 	GLM_FUNC_QUALIFIER detail::tvec2<T, P> fastPow(
     32 		const detail::tvec2<T, P>& x, 
     33 		const detail::tvec2<int, P>& y)
     34 	{
     35 		return detail::tvec2<T, P>(
     36 			fastPow(x.x, y.x),
     37 			fastPow(x.y, y.y));
     38 	}
     39 
     40 	template <typename T, precision P>
     41 	GLM_FUNC_QUALIFIER detail::tvec3<T, P> fastPow(
     42 		const detail::tvec3<T, P>& x, 
     43 		const detail::tvec3<int, P>& y)
     44 	{
     45 		return detail::tvec3<T, P>(
     46 			fastPow(x.x, y.x),
     47 			fastPow(x.y, y.y),
     48 			fastPow(x.z, y.z));
     49 	}
     50 
     51 	template <typename T, precision P>
     52 	GLM_FUNC_QUALIFIER detail::tvec4<T, P> fastPow(
     53 		const detail::tvec4<T, P>& x, 
     54 		const detail::tvec4<int, P>& y)
     55 	{
     56 		return detail::tvec4<T, P>(
     57 			fastPow(x.x, y.x),
     58 			fastPow(x.y, y.y),
     59 			fastPow(x.z, y.z),
     60 			fastPow(x.w, y.w));
     61 	}
     62 
     63 	// fastExp
     64 	// Note: This function provides accurate results only for value between -1 and 1, else avoid it.
     65 	template <typename T>
     66 	GLM_FUNC_QUALIFIER T fastExp(const T x)
     67 	{
     68 		// This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
     69 		// return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
     70 		T x2 = x * x;
     71 		T x3 = x2 * x;
     72 		T x4 = x3 * x;
     73 		T x5 = x4 * x;
     74 		return T(1) + x + (x2 * T(0.5)) + (x3 * T(0.1666666667)) + (x4 * T(0.041666667)) + (x5 * T(0.008333333333));
     75 	}
     76 	/*  // Try to handle all values of float... but often shower than std::exp, glm::floor and the loop kill the performance
     77 	GLM_FUNC_QUALIFIER float fastExp(float x)
     78 	{
     79 		const float e = 2.718281828f;
     80 		const float IntegerPart = floor(x);
     81 		const float FloatPart = x - IntegerPart;
     82 		float z = 1.f;
     83 
     84 		for(int i = 0; i < int(IntegerPart); ++i)
     85 			z *= e;
     86 
     87 		const float x2 = FloatPart * FloatPart;
     88 		const float x3 = x2 * FloatPart;
     89 		const float x4 = x3 * FloatPart;
     90 		const float x5 = x4 * FloatPart;
     91 		return z * (1.0f + FloatPart + (x2 * 0.5f) + (x3 * 0.1666666667f) + (x4 * 0.041666667f) + (x5 * 0.008333333333f));
     92 	}
     93 
     94 	// Increase accuracy on number bigger that 1 and smaller than -1 but it's not enough for high and negative numbers
     95 	GLM_FUNC_QUALIFIER float fastExp(float x)
     96 	{
     97 		// This has a better looking and same performance in release mode than the following code. However, in debug mode it's slower.
     98 		// return 1.0f + x * (1.0f + x * 0.5f * (1.0f + x * 0.3333333333f * (1.0f + x * 0.25 * (1.0f + x * 0.2f))));
     99 		float x2 = x * x;
    100 		float x3 = x2 * x;
    101 		float x4 = x3 * x;
    102 		float x5 = x4 * x;
    103 		float x6 = x5 * x;
    104 		float x7 = x6 * x;
    105 		float x8 = x7 * x;
    106 		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);;
    107 	}
    108 	*/
    109 
    110 	VECTORIZE_VEC(fastExp)
    111 
    112 	// fastLog
    113 	template <typename genType>
    114 	GLM_FUNC_QUALIFIER genType fastLog(genType const & x)
    115 	{
    116 		return std::log(x);
    117 	}
    118 
    119 	/* Slower than the VC7.1 function...
    120 	GLM_FUNC_QUALIFIER float fastLog(float x)
    121 	{
    122 		float y1 = (x - 1.0f) / (x + 1.0f);
    123 		float y2 = y1 * y1;
    124 		return 2.0f * y1 * (1.0f + y2 * (0.3333333333f + y2 * (0.2f + y2 * 0.1428571429f)));
    125 	}
    126 	*/
    127 
    128 	VECTORIZE_VEC(fastLog)
    129 
    130 	//fastExp2, ln2 = 0.69314718055994530941723212145818f
    131 	template <typename genType>
    132 	GLM_FUNC_QUALIFIER genType fastExp2(genType const & x)
    133 	{
    134 		return fastExp(0.69314718055994530941723212145818f * x);
    135 	}
    136 
    137 	VECTORIZE_VEC(fastExp2)
    138 
    139 	// fastLog2, ln2 = 0.69314718055994530941723212145818f
    140 	template <typename genType>
    141 	GLM_FUNC_QUALIFIER genType fastLog2(genType const & x)
    142 	{
    143 		return fastLog(x) / 0.69314718055994530941723212145818f;
    144 	}
    145 
    146 	VECTORIZE_VEC(fastLog2)
    147 
    148 }//namespace glm
    149