00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef SGE_MATH_POWER_HPP_INCLUDED
00022 #define SGE_MATH_POWER_HPP_INCLUDED
00023
00024 #include <cmath>
00025 #include <boost/utility/enable_if.hpp>
00026 #include <boost/type_traits/is_integral.hpp>
00027 #include <boost/type_traits/is_unsigned.hpp>
00028 #include "dim.hpp"
00029
00030 namespace sge
00031 {
00032 namespace math
00033 {
00034
00039 template<typename Base, typename Exp> Base pow_int(const Base base, const Exp e)
00040 {
00041 if(e == 0)
00042 return 1;
00043 Base ret = base;
00044 for(Exp i = 1; i < e; ++i)
00045 ret*=base;
00046 return ret;
00047 }
00048
00049 template<bool is_integral> struct next_pow_2_implementation {
00050 template<typename T> static T next_pow_2(const T t)
00051 {
00052 return static_cast<T>(std::pow(static_cast<T>(2),std::ceil(std::log(static_cast<double>(t))/std::log(static_cast<double>(2)))));
00053 }
00054 };
00055
00056 template<> struct next_pow_2_implementation<true> {
00057 template<typename T> static T next_pow_2(const T t)
00058 {
00059 if (t <= 0) std::log(static_cast<double>(-1));
00060 if (t & (t-1)) {
00061 register T c = t & 0x3f,
00062 t_copy = t, ret=2;
00063 while (t_copy >>= 6) { ret <<= 6; c = t_copy & 0x3f; }
00064 return ret <<= (c & 0x20) ? 5 : (c & 0x10) ? 4 : (c & 0x08) ? 3 : (c & 0x04) ? 2 : (c & 0x02) ? 1 : 0;
00065 } else return t;
00066 }
00067 };
00068
00072 template<typename T> inline T next_pow_2(const T t) {
00073 typedef typename boost::is_integral<T> is_int;
00074 return next_pow_2_implementation<is_int::value>::next_pow_2(t);
00075 }
00076
00080 template<typename T> inline basic_dim<T, 2> next_pow_2(const basic_dim<T, 2>& r)
00081 {
00082 return basic_dim<T, 2>(next_pow_2(r.w()), next_pow_2(r.h()));
00083 }
00084
00088 template<typename T>
00089 inline typename boost::enable_if<boost::is_unsigned<T>, bool>::type
00090 is_power_of_2(const T t)
00091 {
00092 return t && !(t & (t - 1));
00093 }
00094
00099 template<typename T>
00100 inline typename boost::enable_if<boost::is_unsigned<T>, bool>::type
00101 is_power_of_2(const basic_dim<T, 2>& d)
00102 {
00103 return is_power_of_2(d.w()) && is_power_of_2(d.h());
00104 }
00105
00109 template<typename T>
00110 inline T quad(T const& t)
00111 {
00112 return t*t;
00113 }
00114
00115 }
00116 }
00117
00118 #endif