X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fbase%2Fintmath.hh;h=f46133764927d9e6cfe169a3012a2302ae933349;hb=fdf6f6c4b61cadd79d910ccb225ebf9fba6191c2;hp=139f6bf158fc0c6da214898c75da32094ac2bfde;hpb=eef3a2e142443d94b75de333ff3ccb69644a9831;p=gem5.git diff --git a/src/base/intmath.hh b/src/base/intmath.hh index 139f6bf15..f46133764 100644 --- a/src/base/intmath.hh +++ b/src/base/intmath.hh @@ -28,11 +28,12 @@ * Authors: Nathan Binkert */ -#ifndef __INTMATH_HH__ -#define __INTMATH_HH__ +#ifndef __BASE_INTMATH_HH__ +#define __BASE_INTMATH_HH__ -#include +#include +#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 +template 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__