2 * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
31 #ifndef __BASE_INTMATH_HH__
32 #define __BASE_INTMATH_HH__
36 #include "base/misc.hh"
37 #include "base/types.hh"
39 // Returns the prime number one less than n.
42 // Determine if a number is prime
52 // Don't try every odd number to prove if it is a prime.
53 // Toggle between every 2nd and 4th number.
54 // (This is because every 6th odd number is divisible by 3.)
55 for (i = 5; i*i <= n; i += 6) {
56 if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) {
75 return n != 0 && leastSigBit(n) == n;
79 power(uint32_t n, uint32_t e)
82 warn("Warning, power() function is quite slow for large exponents\n");
88 uint64_t old_result = 0;
89 for (int x = 1; x < e; x++) {
92 if (old_result > result)
93 warn("power() overflowed!\n");
100 floorLog2(unsigned x)
106 if (x & 0xffff0000) { y += 16; x >>= 16; }
107 if (x & 0x0000ff00) { y += 8; x >>= 8; }
108 if (x & 0x000000f0) { y += 4; x >>= 4; }
109 if (x & 0x0000000c) { y += 2; x >>= 2; }
110 if (x & 0x00000002) { y += 1; }
116 floorLog2(unsigned long x)
122 #if defined(__LP64__)
123 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
125 if (x & 0xffff0000) { y += 16; x >>= 16; }
126 if (x & 0x0000ff00) { y += 8; x >>= 8; }
127 if (x & 0x000000f0) { y += 4; x >>= 4; }
128 if (x & 0x0000000c) { y += 2; x >>= 2; }
129 if (x & 0x00000002) { y += 1; }
135 floorLog2(unsigned long long x)
141 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
142 if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; }
143 if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; }
144 if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; }
145 if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; }
146 if (x & ULL(0x0000000000000002)) { y += 1; }
155 return floorLog2((unsigned)x);
162 return floorLog2((unsigned long)x);
166 floorLog2(long long x)
169 return floorLog2((unsigned long long)x);
179 return floorLog2(n - (T)1) + 1;
186 return (T)1 << floorLog2(n);
193 return (T)1 << ceilLog2(n);
196 template <class T, class U>
198 divCeil(const T& a, const U& b)
200 return (a + b - 1) / b;
205 roundUp(T val, int align)
207 T mask = (T)align - 1;
208 return (val + mask) & ~mask;
213 roundDown(T val, int align)
215 T mask = (T)align - 1;
222 return (c >= '0' && c <= '9') ||
223 (c >= 'A' && c <= 'F') ||
224 (c >= 'a' && c <= 'f');
230 return c >= '0' && c <= '7';
236 return c >= '0' && c <= '9';
242 if (c >= '0' && c <= '9')
245 if (c >= 'A' && c <= 'F')
246 return (c - 'A') + 10;
248 if (c >= 'a' && c <= 'f')
249 return (c - 'a') + 10;
254 #endif // __BASE_INTMATH_HH__