tests, base: Removed dead code from base/intmath
[gem5.git] / src / base / intmath.hh
1 /*
2 * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Nathan Binkert
29 */
30
31 #ifndef __BASE_INTMATH_HH__
32 #define __BASE_INTMATH_HH__
33
34 #include <cassert>
35
36 #include "base/logging.hh"
37 #include "base/types.hh"
38
39 inline uint64_t
40 power(uint32_t n, uint32_t e)
41 {
42 if (e > 20)
43 warn("Warning, power() function is quite slow for large exponents\n");
44
45 if (e == 0)
46 return 1;
47
48 uint64_t result = n;
49 uint64_t old_result = 0;
50 for (int x = 1; x < e; x++) {
51 old_result = result;
52 result *= n;
53 if (old_result > result)
54 warn("power() overflowed!\n");
55 }
56 return result;
57 }
58
59
60 inline int
61 floorLog2(unsigned x)
62 {
63 assert(x > 0);
64
65 int y = 0;
66
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; }
72
73 return y;
74 }
75
76 inline int
77 floorLog2(unsigned long x)
78 {
79 assert(x > 0);
80
81 int y = 0;
82
83 #if defined(__LP64__)
84 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
85 #endif
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; }
91
92 return y;
93 }
94
95 inline int
96 floorLog2(unsigned long long x)
97 {
98 assert(x > 0);
99
100 int y = 0;
101
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; }
108
109 return y;
110 }
111
112 inline int
113 floorLog2(int x)
114 {
115 assert(x > 0);
116 return floorLog2((unsigned)x);
117 }
118
119 inline int
120 floorLog2(long x)
121 {
122 assert(x > 0);
123 return floorLog2((unsigned long)x);
124 }
125
126 inline int
127 floorLog2(long long x)
128 {
129 assert(x > 0);
130 return floorLog2((unsigned long long)x);
131 }
132
133 template <class T>
134 inline int
135 ceilLog2(const T& n)
136 {
137 if (n == 1)
138 return 0;
139
140 return floorLog2(n - (T)1) + 1;
141 }
142
143 template <class T>
144 inline bool
145 isPowerOf2(const T& n)
146 {
147 return n != 0 && floorLog2(n) == ceilLog2(n);
148 }
149
150 template <class T, class U>
151 inline T
152 divCeil(const T& a, const U& b)
153 {
154 return (a + b - 1) / b;
155 }
156
157 /**
158 * This function is used to align addresses in memory.
159 *
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.
164 */
165 template <class T, class U>
166 inline T
167 roundUp(const T& val, const U& align)
168 {
169 assert(isPowerOf2(align));
170 T mask = (T)align - 1;
171 return (val + mask) & ~mask;
172 }
173
174 /**
175 * This function is used to align addresses in memory.
176 *
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.
181 */
182 template <class T, class U>
183 inline T
184 roundDown(const T& val, const U& align)
185 {
186 assert(isPowerOf2(align));
187 T mask = (T)align - 1;
188 return val & ~mask;
189 }
190
191 #endif // __BASE_INTMATH_HH__