diff options
-rw-r--r-- | base/circlebuf.cc | 4 | ||||
-rw-r--r-- | base/intmath.cc | 2 | ||||
-rw-r--r-- | base/intmath.hh (renamed from base/intmath.h) | 112 | ||||
-rw-r--r-- | base/range.hh | 2 | ||||
-rw-r--r-- | base/remote_gdb.cc | 2 | ||||
-rw-r--r-- | base/statistics.cc | 2 | ||||
-rw-r--r-- | base/str.cc | 2 |
7 files changed, 74 insertions, 52 deletions
diff --git a/base/circlebuf.cc b/base/circlebuf.cc index 482c97f84..311de60b7 100644 --- a/base/circlebuf.cc +++ b/base/circlebuf.cc @@ -33,9 +33,9 @@ #include <string.h> #include <unistd.h> -#include "cprintf.hh" #include "circlebuf.hh" -#include "intmath.h" +#include "cprintf.hh" +#include "intmath.hh" using namespace std; diff --git a/base/intmath.cc b/base/intmath.cc index 8d08e59a8..7a6858d16 100644 --- a/base/intmath.cc +++ b/base/intmath.cc @@ -26,7 +26,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "intmath.h" +#include "intmath.hh" int PrevPrime(int n) diff --git a/base/intmath.h b/base/intmath.hh index 814dacd5f..16467426d 100644 --- a/base/intmath.h +++ b/base/intmath.hh @@ -26,84 +26,106 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef __INTMATH_H__ -#define __INTMATH_H__ +#ifndef __INTMATH_HH__ +#define __INTMATH_HH__ // Returns the prime number one less than n. int PrevPrime(int n); // Determine if a number is prime +template <class T> inline bool -IsPrime(int n) +IsPrime(T n) { - int i; - - if (n == 2 || n == 3) - return true; - - // Don't try every odd number to prove if it is a prime. - // Toggle between every 2nd and 4th number. - // (This is because every 6th odd number is divisible by 3.) - for (i = 5; i*i <= n; i += 6) { - if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) { - return false; + T i; + + if (n == 2 || n == 3) + return true; + + // Don't try every odd number to prove if it is a prime. + // Toggle between every 2nd and 4th number. + // (This is because every 6th odd number is divisible by 3.) + for (i = 5; i*i <= n; i += 6) { + if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) { + return false; + } } - } - return true; + return true; } -inline unsigned -LeastSigBit(unsigned n) -{ return n & ~(n - 1); } +template <class T> +inline T +LeastSigBit(T n) +{ + return n & ~(n - 1); +} +template <class T> inline bool -IsPowerOf2(unsigned n) -{ return n != 0 && LeastSigBit(n) == n; } +IsPowerOf2(T n) +{ + return n != 0 && LeastSigBit(n) == n; +} +template <class T> inline int -FloorLog2(unsigned x) +FloorLog2(T x) { - if (x == 0) - return -1; + if (x == 0) + return -1; - int y = 0; + int y = 0; - if (x & 0xffff0000) { y += 16; x >>= 16; } - if (x & 0x0000ff00) { y += 8; x >>= 8; } - if (x & 0x000000f0) { y += 4; x >>= 4; } - if (x & 0x0000000c) { y += 2; x >>= 2; } - if (x & 0x00000002) { y += 1; } + if (x & 0xffff0000) { y += 16; x >>= 16; } + if (x & 0x0000ff00) { y += 8; x >>= 8; } + if (x & 0x000000f0) { y += 4; x >>= 4; } + if (x & 0x0000000c) { y += 2; x >>= 2; } + if (x & 0x00000002) { y += 1; } - return y; + return y; } +template <class T> inline int -CeilLog2(unsigned n) -{ return FloorLog2(n-1)+1; } +CeilLog2(T n) +{ + return FloorLog2(n - 1) + 1; +} -inline unsigned -FloorPow2(unsigned n) -{ return 1 << FloorLog2(n); } +template <class T> +inline T +FloorPow2(T n) +{ + return (T)1 << FloorLog2(n); +} -inline unsigned -CeilPow2(unsigned n) -{ return 1 << CeilLog2(n); } +template <class T> +inline T +CeilPow2(T n) +{ + return (T)1 << CeilLog2(n); +} 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 IsOct(char c) -{ return (c >= '0' && c <= '7'); } +{ + return c >= '0' && c <= '7'; +} inline bool IsDec(char c) -{ return (c >= '0' && c <= '9'); } +{ + return c >= '0' && c <= '9'; +} inline int Hex2Int(char c) @@ -120,4 +142,4 @@ Hex2Int(char c) return 0; } -#endif // __INTMATH_H__ +#endif // __INTMATH_HH__ diff --git a/base/range.hh b/base/range.hh index 254e71460..dcc863e06 100644 --- a/base/range.hh +++ b/base/range.hh @@ -31,8 +31,8 @@ #include <assert.h> +#include "intmath.hh" #include "str.hh" -#include "intmath.h" template<class T> class Range diff --git a/base/remote_gdb.cc b/base/remote_gdb.cc index 5a6987877..280b1cc23 100644 --- a/base/remote_gdb.cc +++ b/base/remote_gdb.cc @@ -94,7 +94,7 @@ #include <string> #include "exec_context.hh" -#include "intmath.h" +#include "intmath.hh" #include "kgdb.h" #include "physical_memory.hh" diff --git a/base/statistics.cc b/base/statistics.cc index 1e8cd2565..f956de828 100644 --- a/base/statistics.cc +++ b/base/statistics.cc @@ -36,7 +36,7 @@ #include <math.h> #include "cprintf.hh" -#include "intmath.h" +#include "intmath.hh" #include "misc.hh" #include "statistics.hh" #include "str.hh" diff --git a/base/str.cc b/base/str.cc index f54729813..19cbea27d 100644 --- a/base/str.cc +++ b/base/str.cc @@ -34,7 +34,7 @@ #include <string> #include <vector> -#include "intmath.h" +#include "intmath.hh" #include "str.hh" using namespace std; |