11 #ifndef MI_MATH_FUNCTION_H
12 #define MI_MATH_FUNCTION_H
68 struct Operator_equal_equal {
71 template <
typename T1,
typename T2>
72 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 == t2; }
78 template <
typename T1,
typename T2>
79 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 != t2; }
85 template <
typename T1,
typename T2>
86 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 < t2; }
92 template <
typename T1,
typename T2>
93 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 <= t2; }
99 template <
typename T1,
typename T2>
100 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 > t2; }
106 template <
typename T1,
typename T2>
107 inline bool operator()(
const T1& t1,
const T2& t2)
const {
return t1 >= t2; }
113 template <
typename T>
114 inline T
operator()(
const T& t1,
const T& t2)
const {
return t1 + t2; }
120 template <
typename T>
123 template <
typename T>
124 inline T
operator()(
const T& t1,
const T& t2)
const {
return t1 - t2; }
130 template <
typename T>
131 inline T
operator()(
const T& t1,
const T& t2)
const {
return t1 * t2; }
137 template <
typename T>
138 inline T
operator()(
const T& t1,
const T& t2)
const {
return t1 / t2; }
144 template <
typename T>
145 inline bool operator()(
const T& t1,
const T& t2)
const {
return t1 && t2; }
151 template <
typename T>
152 inline bool operator()(
const T& t1,
const T& t2)
const {
return t1 || t2; }
158 template <
typename T>
159 inline bool operator()(
const T& t1,
const T& t2)
const {
return t1 ^ t2; }
165 template <
typename T>
172 template <
typename T>
179 template <
typename T>
186 template <
typename T>
193 template <
typename T>
219 template <
class Vector,
class ResultVector,
class UnaryFunctor>
231 result.set( i, f( vec.
get(i)));
242 template <
class Vector1,
class Vector2,
class ResultVector,
class BinaryFunctor>
244 const Vector1& vec1,
const Vector2& vec2, ResultVector& result, BinaryFunctor f)
248 for(
Size i = 0; i != Vector1::SIZE; ++i)
249 result.set( i, f( vec1.get(i), vec2.get(i)));
261 template <
class Scalar,
class Vector,
class ResultVector,
class BinaryFunctor>
263 const Scalar& s,
const Vector& vec, ResultVector& result, BinaryFunctor f)
267 result.set( i, f( s, vec.
get(i)));
279 template <
class Scalar,
class Vector,
class ResultVector,
class BinaryFunctor>
281 const Vector& vec,
const Scalar& s, ResultVector& result, BinaryFunctor f)
285 result.set( i, f( vec.
get(i), s));
294 template <
class Vector,
class UnaryFunctor>
308 template <
class Vector1,
class Vector2,
class BinaryFunctor>
309 inline void for_each( Vector1& vec1,
const Vector2& vec2, BinaryFunctor f)
312 for(
Size i = 0; i != Vector1::SIZE; ++i)
313 f( vec1.begin()[i], vec2.begin()[i]);
362 const Float32 EXP_C = 8388608.0f;
363 const Float32 LOG_2_E = 1.4426950408889634073599246810019f;
367 y = (y - y*y) * 0.33971f;
375 const Float32 EXP_C = 8388608.0f;
378 y = (y - y*y) * 0.33971f;
386 const Float32 LOG_C = 0.00000011920928955078125f;
388 const Float32 x =
Float32(base::binary_cast<int>(i)) * LOG_C - 127.f;
390 return x + (y - y*y) * 0.346607f;
401 const Float32 LOG_C = 0.00000011920928955078125f;
402 const Float32 EXP_C = 8388608.0f;
404 const Float32 x = (
Float32)(base::binary_cast<int>(b)) * LOG_C - 127.f;
406 const Float32 fl = e * (x + (y - y*y) * 0.346607f);
409 fl*EXP_C + (
Float32)(127.0*EXP_C) - (y2 - y2*y2) * (
Float32)(0.33971*EXP_C), 0.f);
492 return ( s < low) ? low : ( s > high) ? high : s;
498 return ( s < low) ? low : ( s > high) ? high : s;
504 return ( s < low) ? low : ( s > high) ? high : s;
510 return ( s < low) ? low : ( s > high) ? high : s;
516 return ( s < low) ? low : ( s > high) ? high : s;
522 return ( s < low) ? low : ( s > high) ? high : s;
528 return ( s < low) ? low : ( s > high) ? high : s;
534 return ( s < low) ? low : ( s > high) ? high : s;
540 return ( s < low) ? low : ( s > high) ? high : s;
546 return ( s < low) ? low : ( s > high) ? high : s;
564 return MISTD::exp(s * 0.69314718055994530941723212145818 );
606 return abs( left - right ) <= e;
615 return abs( left - right ) <= e;
621 {
return MISTD::log(s) * 1.4426950408889634073599246810019f ; }
624 {
return MISTD::log(s) * 1.4426950408889634073599246810019 ; }
638 return s1 * (
Float32(1)-t) + s2 * t;
648 return s1 * (
Float64(1)-t) + s2 * t;
721 return (base::binary_cast<Uint32>(s) & (1U << 31)) != 0U;
730 return (base::binary_cast<Uint64>(s) & (1ULL << 63)) != 0ULL;
738 const Uint32 exponent_mask = 0x7F800000;
739 const Uint32 fraction_mask = 0x7FFFFF;
745 return ((f & exponent_mask) == exponent_mask) &&
746 ((f & fraction_mask) != 0);
754 const Uint64 exponent_mask = 0x7FF0000000000000ULL;
755 const Uint64 fraction_mask = 0xFFFFFFFFFFFFFULL;
761 return ((f & exponent_mask) == exponent_mask) &&
762 ((f & fraction_mask) != 0);
770 const Uint32 exponent_mask = 0x7F800000;
771 const Uint32 fraction_mask = 0x7FFFFF;
777 return ((f & exponent_mask) == exponent_mask) &&
778 ((f & fraction_mask) == 0);
786 const Uint64 exponent_mask = 0x7FF0000000000000ULL;
787 const Uint64 fraction_mask = 0xFFFFFFFFFFFFFULL;
793 return ((f & exponent_mask) == exponent_mask) &&
794 ((f & fraction_mask) == 0);
804 const Uint32 exponent_mask = 0x7F800000;
810 return ((f & exponent_mask) != exponent_mask);
820 const Uint64 exponent_mask = 0x7FF0000000000000ULL;
826 return ((f & exponent_mask) != exponent_mask);
862 return t * t * (3.0f - 2.0f * t);
875 return t * t * (3.0 - 2.0 * t);
898 c[0] = mi::base::max( color[0], 0.0f);
899 c[1] = mi::base::max( color[1], 0.0f);
900 c[2] = mi::base::max( color[2], 0.0f);
902 const Float32 max = mi::base::max( mi::base::max( c[0], c[1]), c[2]);
905 if( max <= 7.5231631727e-37f)
907 else if( max >= 1.7014118346046923173168730371588e+38f)
914 | (
Uint32( c[1] * v) << 8)
915 | (
Uint32( c[2] * v) << 16)
916 | (e * 2 + (2 << 24));
924 c[0] = mi::base::max( color[0], 0.0f);
925 c[1] = mi::base::max( color[1], 0.0f);
926 c[2] = mi::base::max( color[2], 0.0f);
928 const Float32 max = mi::base::max( mi::base::max( c[0], c[1]), c[2]);
931 if( max <= 7.5231631727e-37f)
932 rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
933 else if( max >= 1.7014118346046923173168730371588e+38f)
934 rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 255;
939 rgbe[0] =
Uint8( c[0] * v);
940 rgbe[1] =
Uint8( c[1] * v);
941 rgbe[2] =
Uint8( c[2] * v);
942 rgbe[3] =
Uint8( (e >> 23) + 2);
950 color[0] = color[1] = color[2] = 0.0f;
966 const Uint32 rgbe3 = rgbe & 0xFF000000u;
968 color[0] = color[1] = color[2] = 0.0f;
972 const Uint32 e = (rgbe3 >> 1) - 0x800000u;
995 inline typename V::value_type
dot(
const V& lhs,
const V& rhs)
997 typename V::value_type v(0);
998 for(
Size i(0u); i < V::SIZE; ++i)
999 v += lhs.get(i) * rhs.get(i);
1022 inline typename V::value_type
length(
const V& v)
1039 const V& lhs,
const V& rhs)
1041 return length( lhs - rhs);
1048 for(
Size i(0u); i < V::SIZE; ++i)
1058 for(
Size i(0u); i < V::SIZE; ++i)
1059 if( ! (lhs.get(i) == rhs.get(i)))
1069 for(
Size i(0u); i < V::SIZE; ++i)
1070 if( lhs.get(i) != rhs.get(i))
1082 for(
Size i(0u); i < V::SIZE-1; ++i) {
1083 if( lhs.get(i) < rhs.get(i))
1085 if( lhs.get(i) > rhs.get(i))
1088 return lhs.get(V::SIZE-1) < rhs.get(V::SIZE-1);
1098 for(
Size i(0u); i < V::SIZE-1; ++i) {
1099 if( lhs.get(i) < rhs.get(i))
1101 if( lhs.get(i) > rhs.get(i))
1104 return lhs.get(V::SIZE-1) <= rhs.get(V::SIZE-1);
1114 for(
Size i(0u); i < V::SIZE-1; ++i) {
1115 if( lhs.get(i) > rhs.get(i))
1117 if( lhs.get(i) < rhs.get(i))
1120 return lhs.get(V::SIZE-1) > rhs.get(V::SIZE-1);
1130 for(
Size i(0u); i < V::SIZE-1; ++i) {
1131 if( lhs.get(i) > rhs.get(i))
1133 if( lhs.get(i) < rhs.get(i))
1136 return lhs.get(V::SIZE-1) >= rhs.get(V::SIZE-1);
1151 for(
Size i(0u); i < V::SIZE; ++i) {
1153 if( result !=
EQUAL)
1165 #endif // MI_MATH_FUNCTION_H