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