Simple Application Framework
1
|
00001 /* 00002 This file is part of Simple Application Framework (Saf) library. 00003 Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com> 00004 00005 This library is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Saf is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Simple Application Framework library. If not, 00017 see <http://www.gnu.org/licenses/>. 00018 */ 00019 00028 #ifndef SAF_MATH_CALCULUS_POWERS_H 00029 #define SAF_MATH_CALCULUS_POWERS_H 00030 00031 #include <math.h> 00032 #include "../../Core.h" 00033 #include "../../Type.h" 00034 #include "../../InvalidOperationException.h" 00035 #include "../../ArgumentException.h" 00036 00037 namespace Saf 00038 { 00039 namespace Math 00040 { 00042 namespace Calculus 00043 { 00045 template <class T> 00046 inline T Sqr(T x) 00047 { 00048 return x * x; 00049 } 00050 00055 template <class T> 00056 inline T Sqrt(T x); 00057 00059 template <> 00060 inline Float32 Sqrt(Float32 x) 00061 { 00062 return sqrt(x); 00063 } 00064 00065 template <> 00066 inline Float64 Sqrt(Float64 x) 00067 { 00068 return sqrt(x); 00069 } 00073 template <class T> 00074 inline T Pow(T x, T y) 00075 { 00076 if (y < 0) 00077 { 00078 throw ArgumentException(SAF_SOURCE_LOCATION, "Function undefined for" 00079 " negative exponents."); 00080 } 00081 00082 T v = 1; 00083 while (y-- > 0) 00084 { 00085 v *= x; 00086 } 00087 return v; 00088 } 00089 00091 template <> 00092 inline Float32 Pow(Float32 x, Float32 y) 00093 { 00094 return pow(x, y); 00095 } 00096 00097 template <> 00098 inline Float64 Pow(Float64 x, Float64 y) 00099 { 00100 return pow(x, y); 00101 } 00105 template <class T> 00106 inline bool IsPowerOf2(T val) 00107 { 00108 return (val > 0) && ((val & (val - 1)) == 0); 00109 } 00110 00112 template <> 00113 inline bool IsPowerOf2(Float32 val) 00114 { 00115 throw InvalidOperationException(SAF_SOURCE_LOCATION, "Function defined for" 00116 " integral types only."); 00117 } 00118 00119 template <> 00120 inline bool IsPowerOf2(Float64 val) 00121 { 00122 throw InvalidOperationException(SAF_SOURCE_LOCATION, "Function defined for" 00123 " integral types only."); 00124 } 00128 template <class T> 00129 T SAF_DLL_EXPORT RoundUpToPowerOf2(T val); 00130 00132 template <class T> 00133 inline T RoundDownToPowerOf2(T val) 00134 { 00135 return T(1) << BinaryLogarithm(val); 00136 } 00137 00139 template <class T> 00140 T SAF_DLL_EXPORT NearestPowerOf2(T val); 00141 00143 template <class T> 00144 T SAF_DLL_EXPORT BinaryLogarithm(T val); 00145 } 00146 } 00147 } 00148 00149 #endif