intmath.hh revision 100
1/* 2 * Copyright (c) 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. 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(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 91inline int 92FloorLog2(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 108inline int 109FloorLog2(int32_t x) 110{ 111 assert(x > 0); 112 return FloorLog2(x); 113} 114 115inline int 116FloorLog2(int64_t x) 117{ 118 assert(x > 0); 119 return FloorLog2(x); 120} 121 122template <class T> 123inline int 124CeilLog2(T n) 125{ 126 if (n == 1) 127 return 0; 128 129 return FloorLog2(n - (T)1) + 1; 130} 131 132template <class T> 133inline T 134FloorPow2(T n) 135{ 136 return (T)1 << FloorLog2(n); 137} 138 139template <class T> 140inline T 141CeilPow2(T n) 142{ 143 return (T)1 << CeilLog2(n); 144} 145 146template <class T> 147inline T 148DivCeil(T a, T b) 149{ 150 return (a + b - 1) / b; 151} 152 153template <class T> 154inline T 155RoundUp(T val, T align) 156{ 157 T mask = align - 1; 158 return (val + mask) & ~mask; 159} 160 161template <class T> 162inline T 163RoundDown(T val, T align) 164{ 165 T mask = align - 1; 166 return val & ~mask; 167} 168 169inline bool 170IsHex(char c) 171{ 172 return c >= '0' && c <= '9' || 173 c >= 'A' && c <= 'F' || 174 c >= 'a' && c <= 'f'; 175} 176 177inline bool 178IsOct(char c) 179{ 180 return c >= '0' && c <= '7'; 181} 182 183inline bool 184IsDec(char c) 185{ 186 return c >= '0' && c <= '9'; 187} 188 189inline int 190Hex2Int(char c) 191{ 192 if (c >= '0' && c <= '9') 193 return (c - '0'); 194 195 if(c >= 'A' && c <= 'F') 196 return (c - 'A') + 10; 197 198 if (c >= 'a' && c <= 'f') 199 return (c - 'a') + 10; 200 201 return 0; 202} 203 204#endif // __INTMATH_HH__ 205