Merge ktlim@zizzer.eecs.umich.edu:/bk/m5
[gem5.git] / base / intmath.hh
1 /*
2 * Copyright (c) 2001, 2003 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
29 #ifndef __INTMATH_HH__
30 #define __INTMATH_HH__
31
32 #include <assert.h>
33
34 #include "sim/host.hh"
35
36 // Returns the prime number one less than n.
37 int PrevPrime(int n);
38
39 // Determine if a number is prime
40 template <class T>
41 inline bool
42 IsPrime(T n)
43 {
44 T i;
45
46 if (n == 2 || n == 3)
47 return true;
48
49 // Don't try every odd number to prove if it is a prime.
50 // Toggle between every 2nd and 4th number.
51 // (This is because every 6th odd number is divisible by 3.)
52 for (i = 5; i*i <= n; i += 6) {
53 if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) {
54 return false;
55 }
56 }
57
58 return true;
59 }
60
61 template <class T>
62 inline T
63 LeastSigBit(T n)
64 {
65 return n & ~(n - 1);
66 }
67
68 template <class T>
69 inline bool
70 IsPowerOf2(T n)
71 {
72 return n != 0 && LeastSigBit(n) == n;
73 }
74
75 inline int
76 FloorLog2(uint32_t x)
77 {
78 assert(x > 0);
79
80 int y = 0;
81
82 if (x & 0xffff0000) { y += 16; x >>= 16; }
83 if (x & 0x0000ff00) { y += 8; x >>= 8; }
84 if (x & 0x000000f0) { y += 4; x >>= 4; }
85 if (x & 0x0000000c) { y += 2; x >>= 2; }
86 if (x & 0x00000002) { y += 1; }
87
88 return y;
89 }
90
91 inline int
92 FloorLog2(uint64_t x)
93 {
94 assert(x > 0);
95
96 int y = 0;
97
98 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
99 if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; }
100 if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; }
101 if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; }
102 if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; }
103 if (x & ULL(0x0000000000000002)) { y += 1; }
104
105 return y;
106 }
107
108 inline int
109 FloorLog2(int32_t x)
110 {
111 assert(x > 0);
112 return FloorLog2((uint32_t)x);
113 }
114
115 inline int
116 FloorLog2(int64_t x)
117 {
118 assert(x > 0);
119 return FloorLog2((uint64_t)x);
120 }
121
122 #if defined(__APPLE__)
123 inline int
124 FloorLog2(size_t x)
125 {
126 assert(x > 0);
127 assert(sizeof(size_t) == 4 || sizeof(size_t) == 8);
128
129 // It's my hope that this is optimized away?
130 if (sizeof(size_t) == 4)
131 return FloorLog2((uint32_t)x);
132 else if (sizeof(size_t) == 8)
133 return FloorLog2((uint64_t)x);
134
135 }
136 #endif
137
138 template <class T>
139 inline int
140 CeilLog2(T n)
141 {
142 if (n == 1)
143 return 0;
144
145 return FloorLog2(n - (T)1) + 1;
146 }
147
148 template <class T>
149 inline T
150 FloorPow2(T n)
151 {
152 return (T)1 << FloorLog2(n);
153 }
154
155 template <class T>
156 inline T
157 CeilPow2(T n)
158 {
159 return (T)1 << CeilLog2(n);
160 }
161
162 template <class T>
163 inline T
164 DivCeil(T a, T b)
165 {
166 return (a + b - 1) / b;
167 }
168
169 template <class T>
170 inline T
171 RoundUp(T val, T align)
172 {
173 T mask = align - 1;
174 return (val + mask) & ~mask;
175 }
176
177 template <class T>
178 inline T
179 RoundDown(T val, T align)
180 {
181 T mask = align - 1;
182 return val & ~mask;
183 }
184
185 inline bool
186 IsHex(char c)
187 {
188 return c >= '0' && c <= '9' ||
189 c >= 'A' && c <= 'F' ||
190 c >= 'a' && c <= 'f';
191 }
192
193 inline bool
194 IsOct(char c)
195 {
196 return c >= '0' && c <= '7';
197 }
198
199 inline bool
200 IsDec(char c)
201 {
202 return c >= '0' && c <= '9';
203 }
204
205 inline int
206 Hex2Int(char c)
207 {
208 if (c >= '0' && c <= '9')
209 return (c - '0');
210
211 if (c >= 'A' && c <= 'F')
212 return (c - 'A') + 10;
213
214 if (c >= 'a' && c <= 'f')
215 return (c - 'a') + 10;
216
217 return 0;
218 }
219
220 #endif // __INTMATH_HH__