gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
intmath.hh
Go to the documentation of this file.
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/misc.hh"
37 #include "base/types.hh"
38 
39 // Returns the prime number one less than n.
40 int prevPrime(int n);
41 
42 // Determine if a number is prime
43 template <class T>
44 inline bool
45 isPrime(const T& n)
46 {
47  T i;
48 
49  if (n == 2 || n == 3)
50  return true;
51 
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) ) {
57  return false;
58  }
59  }
60 
61  return true;
62 }
63 
64 template <class T>
65 inline T
66 leastSigBit(const T& n)
67 {
68  return n & ~(n - 1);
69 }
70 
71 template <class T>
72 inline bool
73 isPowerOf2(const T& n)
74 {
75  return n != 0 && leastSigBit(n) == n;
76 }
77 
78 inline uint64_t
79 power(uint32_t n, uint32_t e)
80 {
81  if (e > 20)
82  warn("Warning, power() function is quite slow for large exponents\n");
83 
84  if (e == 0)
85  return 1;
86 
87  uint64_t result = n;
88  uint64_t old_result = 0;
89  for (int x = 1; x < e; x++) {
90  old_result = result;
91  result *= n;
92  if (old_result > result)
93  warn("power() overflowed!\n");
94  }
95  return result;
96 }
97 
98 
99 inline int
100 floorLog2(unsigned x)
101 {
102  assert(x > 0);
103 
104  int y = 0;
105 
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; }
111 
112  return y;
113 }
114 
115 inline int
116 floorLog2(unsigned long x)
117 {
118  assert(x > 0);
119 
120  int y = 0;
121 
122 #if defined(__LP64__)
123  if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
124 #endif
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; }
130 
131  return y;
132 }
133 
134 inline int
135 floorLog2(unsigned long long x)
136 {
137  assert(x > 0);
138 
139  int y = 0;
140 
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; }
147 
148  return y;
149 }
150 
151 inline int
153 {
154  assert(x > 0);
155  return floorLog2((unsigned)x);
156 }
157 
158 inline int
160 {
161  assert(x > 0);
162  return floorLog2((unsigned long)x);
163 }
164 
165 inline int
166 floorLog2(long long x)
167 {
168  assert(x > 0);
169  return floorLog2((unsigned long long)x);
170 }
171 
172 template <class T>
173 inline int
174 ceilLog2(const T& n)
175 {
176  if (n == 1)
177  return 0;
178 
179  return floorLog2(n - (T)1) + 1;
180 }
181 
182 template <class T>
183 inline T
184 floorPow2(const T& n)
185 {
186  return (T)1 << floorLog2(n);
187 }
188 
189 template <class T>
190 inline T
191 ceilPow2(const T& n)
192 {
193  return (T)1 << ceilLog2(n);
194 }
195 
196 template <class T, class U>
197 inline T
198 divCeil(const T& a, const U& b)
199 {
200  return (a + b - 1) / b;
201 }
202 
203 template <class T, class U>
204 inline T
205 roundUp(const T& val, const U& align)
206 {
207  T mask = (T)align - 1;
208  return (val + mask) & ~mask;
209 }
210 
211 template <class T, class U>
212 inline T
213 roundDown(const T& val, const U& align)
214 {
215  T mask = (T)align - 1;
216  return val & ~mask;
217 }
218 
219 inline bool
220 isHex(char c)
221 {
222  return (c >= '0' && c <= '9') ||
223  (c >= 'A' && c <= 'F') ||
224  (c >= 'a' && c <= 'f');
225 }
226 
227 inline bool
228 isOct(char c)
229 {
230  return c >= '0' && c <= '7';
231 }
232 
233 inline bool
234 isDec(char c)
235 {
236  return c >= '0' && c <= '9';
237 }
238 
239 inline int
240 hex2Int(char c)
241 {
242  if (c >= '0' && c <= '9')
243  return (c - '0');
244 
245  if (c >= 'A' && c <= 'F')
246  return (c - 'A') + 10;
247 
248  if (c >= 'a' && c <= 'f')
249  return (c - 'a') + 10;
250 
251  return 0;
252 }
253 
254 #endif // __BASE_INTMATH_HH__
Bitfield< 7 > i
Definition: miscregs.hh:1378
bool isDec(char c)
Definition: intmath.hh:234
Bitfield< 8 > a
Definition: miscregs.hh:1377
bool isPrime(const T &n)
Definition: intmath.hh:45
int ceilLog2(const T &n)
Definition: intmath.hh:174
T roundUp(const T &val, const U &align)
Definition: intmath.hh:205
Bitfield< 63 > val
Definition: misc.hh:770
Bitfield< 31 > n
Definition: miscregs.hh:1636
#define warn(...)
Definition: misc.hh:219
Bitfield< 7 > b
Definition: miscregs.hh:1564
int hex2Int(char c)
Definition: intmath.hh:240
uint64_t power(uint32_t n, uint32_t e)
Definition: intmath.hh:79
bool isPowerOf2(const T &n)
Definition: intmath.hh:73
T roundDown(const T &val, const U &align)
Definition: intmath.hh:213
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
#define ULL(N)
uint64_t constant
Definition: types.hh:50
Bitfield< 9 > e
Definition: miscregs.hh:1376
int floorLog2(unsigned x)
Definition: intmath.hh:100
Bitfield< 29 > c
Definition: miscregs.hh:1365
int prevPrime(int n)
Definition: intmath.cc:35
T divCeil(const T &a, const U &b)
Definition: intmath.hh:198
T leastSigBit(const T &n)
Definition: intmath.hh:66
bool isOct(char c)
Definition: intmath.hh:228
Bitfield< 3, 0 > mask
Definition: types.hh:64
T ceilPow2(const T &n)
Definition: intmath.hh:191
bool isHex(char c)
Definition: intmath.hh:220
Bitfield< 1 > x
Definition: types.hh:105
T floorPow2(const T &n)
Definition: intmath.hh:184

Generated on Fri Jun 9 2017 13:03:40 for gem5 by doxygen 1.8.6