misc: Appease clang static analyzer
[gem5.git] / src / base / intmath.hh
index c536fda519b843f13faf031d762dc76e52a1ccce..eded26d4a4c291255886fd70ff3bde2c7bad090b 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);
@@ -41,7 +42,7 @@ int prevPrime(int n);
 // Determine if a number is prime
 template <class T>
 inline bool
-isPrime(T n)
+isPrime(const T& n)
 {
     T i;
 
@@ -62,18 +63,39 @@ isPrime(T n)
 
 template <class T>
 inline T
-leastSigBit(T n)
+leastSigBit(const T& n)
 {
     return n & ~(n - 1);
 }
 
 template <class T>
 inline bool
-isPowerOf2(T n)
+isPowerOf2(const 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)
 {
@@ -149,7 +171,7 @@ floorLog2(long long x)
 
 template <class T>
 inline int
-ceilLog2(T n)
+ceilLog2(const T& n)
 {
     if (n == 1)
         return 0;
@@ -159,36 +181,36 @@ ceilLog2(T n)
 
 template <class T>
 inline T
-floorPow2(T n)
+floorPow2(const T& n)
 {
     return (T)1 << floorLog2(n);
 }
 
 template <class T>
 inline T
-ceilPow2(T n)
+ceilPow2(const 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;
 }
 
-template <class T>
+template <class T, class U>
 inline T
-roundUp(T val, int align)
+roundUp(const T& val, const U& align)
 {
     T mask = (T)align - 1;
     return (val + mask) & ~mask;
 }
 
-template <class T>
+template <class T, class U>
 inline T
-roundDown(T val, int align)
+roundDown(const T& val, const U& align)
 {
     T mask = (T)align - 1;
     return val & ~mask;
@@ -229,4 +251,4 @@ hex2Int(char c)
   return 0;
 }
 
-#endif // __INTMATH_HH__
+#endif // __BASE_INTMATH_HH__