* 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.
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)
{
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;
}
return 0;
}
-#endif // __INTMATH_HH__
+#endif // __BASE_INTMATH_HH__