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
| 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
|
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 // __BASE_INTMATH_HH__
| 99inline int 100floorLog2(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 115inline int 116floorLog2(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 134inline int 135floorLog2(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 151inline int 152floorLog2(int x) 153{ 154 assert(x > 0); 155 return floorLog2((unsigned)x); 156} 157 158inline int 159floorLog2(long x) 160{ 161 assert(x > 0); 162 return floorLog2((unsigned long)x); 163} 164 165inline int 166floorLog2(long long x) 167{ 168 assert(x > 0); 169 return floorLog2((unsigned long long)x); 170} 171 172template <class T> 173inline int 174ceilLog2(T n) 175{ 176 if (n == 1) 177 return 0; 178 179 return floorLog2(n - (T)1) + 1; 180} 181 182template <class T> 183inline T 184floorPow2(T n) 185{ 186 return (T)1 << floorLog2(n); 187} 188 189template <class T> 190inline T 191ceilPow2(T n) 192{ 193 return (T)1 << ceilLog2(n); 194} 195 196template <class T> 197inline T 198divCeil(T a, T b) 199{ 200 return (a + b - 1) / b; 201} 202 203template <class T> 204inline T 205roundUp(T val, int align) 206{ 207 T mask = (T)align - 1; 208 return (val + mask) & ~mask; 209} 210 211template <class T> 212inline T 213roundDown(T val, int align) 214{ 215 T mask = (T)align - 1; 216 return val & ~mask; 217} 218 219inline bool 220isHex(char c) 221{ 222 return (c >= '0' && c <= '9') || 223 (c >= 'A' && c <= 'F') || 224 (c >= 'a' && c <= 'f'); 225} 226 227inline bool 228isOct(char c) 229{ 230 return c >= '0' && c <= '7'; 231} 232 233inline bool 234isDec(char c) 235{ 236 return c >= '0' && c <= '9'; 237} 238 239inline int 240hex2Int(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__
|