scons: Enable build on OSX
[gem5.git] / src / base / intmath.hh
index 139f6bf158fc0c6da214898c75da32094ac2bfde..f46133764927d9e6cfe169a3012a2302ae933349 100644 (file)
  * Authors: Nathan Binkert
  */
 
-#ifndef __INTMATH_HH__
-#define __INTMATH_HH__
+#ifndef __BASE_INTMATH_HH__
+#define __BASE_INTMATH_HH__
 
-#include <assert.h>
+#include <cassert>
 
+#include "base/misc.hh"
 #include "base/types.hh"
 
 // Returns the prime number one less than n.
@@ -74,6 +75,27 @@ isPowerOf2(T n)
     return n != 0 && leastSigBit(n) == n;
 }
 
+inline uint64_t
+power(uint32_t n, uint32_t e)
+{
+    if (e > 20)
+        warn("Warning, power() function is quite slow for large exponents\n");
+
+    if (e == 0)
+        return 1;
+
+    uint64_t result = n;
+    uint64_t old_result = 0;
+    for (int x = 1; x < e; x++) {
+        old_result = result;
+        result *= n;
+        if (old_result > result)
+            warn("power() overflowed!\n");
+    }
+    return result;
+}
+
+
 inline int
 floorLog2(unsigned x)
 {
@@ -171,9 +193,9 @@ ceilPow2(T n)
     return (T)1 << ceilLog2(n);
 }
 
-template <class T>
+template <class T, class U>
 inline T
-divCeil(T a, T b)
+divCeil(const T& a, const U& b)
 {
     return (a + b - 1) / b;
 }
@@ -229,4 +251,4 @@ hex2Int(char c)
   return 0;
 }
 
-#endif // __INTMATH_HH__
+#endif // __BASE_INTMATH_HH__