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.
| 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 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. 37int prevPrime(int n); 38 39// Determine if a number is prime 40template <class T> 41inline bool 42isPrime(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 61template <class T> 62inline T 63leastSigBit(T n) 64{ 65 return n & ~(n - 1); 66} 67 68template <class T> 69inline bool 70isPowerOf2(T n) 71{ 72 return n != 0 && leastSigBit(n) == n; 73} 74 75inline int 76floorLog2(unsigned 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 91inline int 92floorLog2(unsigned long x) 93{ 94 assert(x > 0); 95 96 int y = 0; 97 98#if defined(__LP64__) 99 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; } 100#endif 101 if (x & 0xffff0000) { y += 16; x >>= 16; } 102 if (x & 0x0000ff00) { y += 8; x >>= 8; } 103 if (x & 0x000000f0) { y += 4; x >>= 4; } 104 if (x & 0x0000000c) { y += 2; x >>= 2; } 105 if (x & 0x00000002) { y += 1; } 106 107 return y; 108} 109 110inline int 111floorLog2(unsigned long long x) 112{ 113 assert(x > 0); 114 115 int y = 0; 116 117 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; } 118 if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; } 119 if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; } 120 if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; } 121 if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; } 122 if (x & ULL(0x0000000000000002)) { y += 1; } 123 124 return y; 125} 126 127inline int 128floorLog2(int x) 129{ 130 assert(x > 0); 131 return floorLog2((unsigned)x); 132} 133 134inline int 135floorLog2(long x) 136{ 137 assert(x > 0); 138 return floorLog2((unsigned long)x); 139} 140 141inline int 142floorLog2(long long x) 143{ 144 assert(x > 0); 145 return floorLog2((unsigned long long)x); 146} 147 148template <class T> 149inline int 150ceilLog2(T n) 151{ 152 if (n == 1) 153 return 0; 154 155 return floorLog2(n - (T)1) + 1; 156} 157 158template <class T> 159inline T 160floorPow2(T n) 161{ 162 return (T)1 << floorLog2(n); 163} 164 165template <class T> 166inline T 167ceilPow2(T n) 168{ 169 return (T)1 << ceilLog2(n); 170} 171 172template <class T> 173inline T 174divCeil(T a, T b) 175{ 176 return (a + b - 1) / b; 177} 178 179template <class T> 180inline T 181roundUp(T val, int align) 182{ 183 T mask = (T)align - 1; 184 return (val + mask) & ~mask; 185} 186 187template <class T> 188inline T 189roundDown(T val, int align) 190{ 191 T mask = (T)align - 1; 192 return val & ~mask; 193} 194 195inline bool 196isHex(char c) 197{ 198 return c >= '0' && c <= '9' || 199 c >= 'A' && c <= 'F' || 200 c >= 'a' && c <= 'f'; 201} 202 203inline bool 204isOct(char c) 205{ 206 return c >= '0' && c <= '7'; 207} 208 209inline bool 210isDec(char c) 211{ 212 return c >= '0' && c <= '9'; 213} 214 215inline int 216hex2Int(char c) 217{ 218 if (c >= '0' && c <= '9') 219 return (c - '0'); 220 221 if (c >= 'A' && c <= 'F') 222 return (c - 'A') + 10; 223 224 if (c >= 'a' && c <= 'f') 225 return (c - 'a') + 10; 226 227 return 0; 228} 229 230#endif // __INTMATH_HH__
| 29 */ 30 31#ifndef __INTMATH_HH__ 32#define __INTMATH_HH__ 33 34#include <assert.h> 35 36#include "sim/host.hh" 37 38// Returns the prime number one less than n. 39int prevPrime(int n); 40 41// Determine if a number is prime 42template <class T> 43inline bool 44isPrime(T n) 45{ 46 T i; 47 48 if (n == 2 || n == 3) 49 return true; 50 51 // Don't try every odd number to prove if it is a prime. 52 // Toggle between every 2nd and 4th number. 53 // (This is because every 6th odd number is divisible by 3.) 54 for (i = 5; i*i <= n; i += 6) { 55 if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) { 56 return false; 57 } 58 } 59 60 return true; 61} 62 63template <class T> 64inline T 65leastSigBit(T n) 66{ 67 return n & ~(n - 1); 68} 69 70template <class T> 71inline bool 72isPowerOf2(T n) 73{ 74 return n != 0 && leastSigBit(n) == n; 75} 76 77inline int 78floorLog2(unsigned x) 79{ 80 assert(x > 0); 81 82 int y = 0; 83 84 if (x & 0xffff0000) { y += 16; x >>= 16; } 85 if (x & 0x0000ff00) { y += 8; x >>= 8; } 86 if (x & 0x000000f0) { y += 4; x >>= 4; } 87 if (x & 0x0000000c) { y += 2; x >>= 2; } 88 if (x & 0x00000002) { y += 1; } 89 90 return y; 91} 92 93inline int 94floorLog2(unsigned long x) 95{ 96 assert(x > 0); 97 98 int y = 0; 99 100#if defined(__LP64__) 101 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; } 102#endif 103 if (x & 0xffff0000) { y += 16; x >>= 16; } 104 if (x & 0x0000ff00) { y += 8; x >>= 8; } 105 if (x & 0x000000f0) { y += 4; x >>= 4; } 106 if (x & 0x0000000c) { y += 2; x >>= 2; } 107 if (x & 0x00000002) { y += 1; } 108 109 return y; 110} 111 112inline int 113floorLog2(unsigned long long x) 114{ 115 assert(x > 0); 116 117 int y = 0; 118 119 if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; } 120 if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; } 121 if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; } 122 if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; } 123 if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; } 124 if (x & ULL(0x0000000000000002)) { y += 1; } 125 126 return y; 127} 128 129inline int 130floorLog2(int x) 131{ 132 assert(x > 0); 133 return floorLog2((unsigned)x); 134} 135 136inline int 137floorLog2(long x) 138{ 139 assert(x > 0); 140 return floorLog2((unsigned long)x); 141} 142 143inline int 144floorLog2(long long x) 145{ 146 assert(x > 0); 147 return floorLog2((unsigned long long)x); 148} 149 150template <class T> 151inline int 152ceilLog2(T n) 153{ 154 if (n == 1) 155 return 0; 156 157 return floorLog2(n - (T)1) + 1; 158} 159 160template <class T> 161inline T 162floorPow2(T n) 163{ 164 return (T)1 << floorLog2(n); 165} 166 167template <class T> 168inline T 169ceilPow2(T n) 170{ 171 return (T)1 << ceilLog2(n); 172} 173 174template <class T> 175inline T 176divCeil(T a, T b) 177{ 178 return (a + b - 1) / b; 179} 180 181template <class T> 182inline T 183roundUp(T val, int align) 184{ 185 T mask = (T)align - 1; 186 return (val + mask) & ~mask; 187} 188 189template <class T> 190inline T 191roundDown(T val, int align) 192{ 193 T mask = (T)align - 1; 194 return val & ~mask; 195} 196 197inline bool 198isHex(char c) 199{ 200 return c >= '0' && c <= '9' || 201 c >= 'A' && c <= 'F' || 202 c >= 'a' && c <= 'f'; 203} 204 205inline bool 206isOct(char c) 207{ 208 return c >= '0' && c <= '7'; 209} 210 211inline bool 212isDec(char c) 213{ 214 return c >= '0' && c <= '9'; 215} 216 217inline int 218hex2Int(char c) 219{ 220 if (c >= '0' && c <= '9') 221 return (c - '0'); 222 223 if (c >= 'A' && c <= 'F') 224 return (c - 'A') + 10; 225 226 if (c >= 'a' && c <= 'f') 227 return (c - 'a') + 10; 228 229 return 0; 230} 231 232#endif // __INTMATH_HH__
|