diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/base/SConscript | 1 | ||||
-rw-r--r-- | src/base/intmath.cc | 61 | ||||
-rw-r--r-- | src/base/intmath.hh | 92 |
3 files changed, 18 insertions, 136 deletions
diff --git a/src/base/SConscript b/src/base/SConscript index 09bcf3f86..1c2f80baf 100644 --- a/src/base/SConscript +++ b/src/base/SConscript @@ -58,7 +58,6 @@ Source('hostinfo.cc') Source('inet.cc') Source('inifile.cc') GTest('inifile.test', 'inifile.test.cc', 'inifile.cc', 'str.cc') -Source('intmath.cc') Source('logging.cc') Source('match.cc') GTest('match.test', 'match.test.cc', 'match.cc', 'str.cc') diff --git a/src/base/intmath.cc b/src/base/intmath.cc deleted file mode 100644 index 22414ea4b..000000000 --- a/src/base/intmath.cc +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer; - * redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution; - * neither the name of the copyright holders nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: Nathan Binkert - * Steve Reinhardt - */ - -#include "base/intmath.hh" - -int -prevPrime(int n) -{ - int decr; - - // If the number is even, let's start with the previous odd number. - if (!(n & 1)) - --n; - - // Lets test for divisibility by 3. Then we will be able to easily - // avoid numbers that are divisible by 3 in the future. - decr = n % 3; - if (decr == 0) { - n -= 2; - decr = 2; - } - else if (decr == 1) - decr = 4; - - for (;;) { - if (isPrime(n)) - return n; - n -= decr; - // Toggle between 2 and 4 to prevent trying numbers that are known - // to be divisible by 3. - decr = 6 - decr; - } -} diff --git a/src/base/intmath.hh b/src/base/intmath.hh index ee5cf66c8..449a49ca8 100644 --- a/src/base/intmath.hh +++ b/src/base/intmath.hh @@ -36,31 +36,6 @@ #include "base/logging.hh" #include "base/types.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(const T& n) -{ - 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; -} - inline uint64_t power(uint32_t n, uint32_t e) { @@ -172,20 +147,6 @@ isPowerOf2(const T& n) return n != 0 && floorLog2(n) == ceilLog2(n); } -template <class T> -inline T -floorPow2(const T& n) -{ - return (T)1 << floorLog2(n); -} - -template <class T> -inline T -ceilPow2(const T& n) -{ - return (T)1 << ceilLog2(n); -} - template <class T, class U> inline T divCeil(const T& a, const U& b) @@ -193,55 +154,38 @@ divCeil(const T& a, const U& b) return (a + b - 1) / b; } +/** + * This function is used to align addresses in memory. + * + * @param val is the address to be aligned. + * @param align is the alignment. Can only be a power of 2. + * @return The aligned address. The smallest number divisible + * by @param align which is greater than or equal to @param val. +*/ template <class T, class U> inline T roundUp(const T& val, const U& align) { + assert(isPowerOf2(align)); T mask = (T)align - 1; return (val + mask) & ~mask; } +/** + * This function is used to align addresses in memory. + * + * @param val is the address to be aligned. + * @param align is the alignment. Can only be a power of 2. + * @return The aligned address. The biggest number divisible + * by @param align which is less than or equal to @param val. +*/ template <class T, class U> inline T roundDown(const T& val, const U& align) { + assert(isPowerOf2(align)); T mask = (T)align - 1; return val & ~mask; } -inline bool -isHex(char c) -{ - return (c >= '0' && c <= '9') || - (c >= 'A' && c <= 'F') || - (c >= 'a' && c <= 'f'); -} - -inline bool -isOct(char c) -{ - return c >= '0' && c <= '7'; -} - -inline bool -isDec(char c) -{ - return c >= '0' && c <= '9'; -} - -inline int -hex2Int(char c) -{ - if (c >= '0' && c <= '9') - return (c - '0'); - - if (c >= 'A' && c <= 'F') - return (c - 'A') + 10; - - if (c >= 'a' && c <= 'f') - return (c - 'a') + 10; - - return 0; -} - #endif // __BASE_INTMATH_HH__ |