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/logging.hh"
37 #include "base/types.hh"
40 power(uint32_t n, uint32_t e)
43 warn("Warning, power() function is quite slow for large exponents\n");
49 uint64_t old_result = 0;
50 for (int x = 1; x < e; x++) {
53 if (old_result > result)
54 warn("power() overflowed!\n");
67 if (x & 0xffff0000) { y += 16; x >>= 16; }
68 if (x & 0x0000ff00) { y += 8; x >>= 8; }
69 if (x & 0x000000f0) { y += 4; x >>= 4; }
70 if (x & 0x0000000c) { y += 2; x >>= 2; }
71 if (x & 0x00000002) { y += 1; }
77 floorLog2(unsigned long x)
84 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
86 if (x & 0xffff0000) { y += 16; x >>= 16; }
87 if (x & 0x0000ff00) { y += 8; x >>= 8; }
88 if (x & 0x000000f0) { y += 4; x >>= 4; }
89 if (x & 0x0000000c) { y += 2; x >>= 2; }
90 if (x & 0x00000002) { y += 1; }
96 floorLog2(unsigned long long x)
102 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
103 if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; }
104 if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; }
105 if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; }
106 if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; }
107 if (x & ULL(0x0000000000000002)) { y += 1; }
116 return floorLog2((unsigned)x);
123 return floorLog2((unsigned long)x);
127 floorLog2(long long x)
130 return floorLog2((unsigned long long)x);
140 return floorLog2(n - (T)1) + 1;
145 isPowerOf2(const T& n)
147 return n != 0 && floorLog2(n) == ceilLog2(n);
150 template <class T, class U>
152 divCeil(const T& a, const U& b)
154 return (a + b - 1) / b;
158 * This function is used to align addresses in memory.
160 * @param val is the address to be aligned.
161 * @param align is the alignment. Can only be a power of 2.
162 * @return The aligned address. The smallest number divisible
163 * by @param align which is greater than or equal to @param val.
165 template <class T, class U>
167 roundUp(const T& val, const U& align)
169 assert(isPowerOf2(align));
170 T mask = (T)align - 1;
171 return (val + mask) & ~mask;
175 * This function is used to align addresses in memory.
177 * @param val is the address to be aligned.
178 * @param align is the alignment. Can only be a power of 2.
179 * @return The aligned address. The biggest number divisible
180 * by @param align which is less than or equal to @param val.
182 template <class T, class U>
184 roundDown(const T& val, const U& align)
186 assert(isPowerOf2(align));
187 T mask = (T)align - 1;
191 #endif // __BASE_INTMATH_HH__