scons: Enable build on OSX
[gem5.git] / src / base / intmath.hh
index 227012e30a2435c5c131e751834e4d52b0063e47..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 "sim/host.hh"
+#include "base/misc.hh"
+#include "base/types.hh"
 
 // Returns the prime number one less than n.
 int prevPrime(int 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;
 }
@@ -197,9 +219,9 @@ roundDown(T val, int align)
 inline bool
 isHex(char c)
 {
-    return c >= '0' && c <= '9' ||
-        c >= 'A' && c <= 'F' ||
-        c >= 'a' && c <= 'f';
+    return (c >= '0' && c <= '9') ||
+        (c >= 'A' && c <= 'F') ||
+        (c >= 'a' && c <= 'f');
 }
 
 inline bool
@@ -229,4 +251,4 @@ hex2Int(char c)
   return 0;
 }
 
-#endif // __INTMATH_HH__
+#endif // __BASE_INTMATH_HH__