Merge saidi@zizzer.eecs.umich.edu:/bk/linux
[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 inline int
123 FloorLog2(size_t x)
124 {
125 assert(x > 0);
126 assert(sizeof(size_t) == 4 || sizeof(size_t) == 8);
127
128 // It's my hope that this is optimized away?
129 if (sizeof(size_t) == 4)
130 return FloorLog2((uint32_t)x);
131 else if (sizeof(size_t) == 8)
132 return FloorLog2((uint64_t)x);
133
134 }
135
136 template <class T>
137 inline int
138 CeilLog2(T n)
139 {
140 if (n == 1)
141 return 0;
142
143 return FloorLog2(n - (T)1) + 1;
144 }
145
146 template <class T>
147 inline T
148 FloorPow2(T n)
149 {
150 return (T)1 << FloorLog2(n);
151 }
152
153 template <class T>
154 inline T
155 CeilPow2(T n)
156 {
157 return (T)1 << CeilLog2(n);
158 }
159
160 template <class T>
161 inline T
162 DivCeil(T a, T b)
163 {
164 return (a + b - 1) / b;
165 }
166
167 template <class T>
168 inline T
169 RoundUp(T val, T align)
170 {
171 T mask = align - 1;
172 return (val + mask) & ~mask;
173 }
174
175 template <class T>
176 inline T
177 RoundDown(T val, T align)
178 {
179 T mask = align - 1;
180 return val & ~mask;
181 }
182
183 inline bool
184 IsHex(char c)
185 {
186 return c >= '0' && c <= '9' ||
187 c >= 'A' && c <= 'F' ||
188 c >= 'a' && c <= 'f';
189 }
190
191 inline bool
192 IsOct(char c)
193 {
194 return c >= '0' && c <= '7';
195 }
196
197 inline bool
198 IsDec(char c)
199 {
200 return c >= '0' && c <= '9';
201 }
202
203 inline int
204 Hex2Int(char c)
205 {
206 if (c >= '0' && c <= '9')
207 return (c - '0');
208
209 if (c >= 'A' && c <= 'F')
210 return (c - 'A') + 10;
211
212 if (c >= 'a' && c <= 'f')
213 return (c - 'a') + 10;
214
215 return 0;
216 }
217
218 #endif // __INTMATH_HH__