intmath.hh (9091:9b29b9a4dda6) | intmath.hh (10295:9b9ef42122bc) |
---|---|
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; --- 28 unchanged lines hidden (view full) --- 37#include "base/types.hh" 38 39// Returns the prime number one less than n. 40int prevPrime(int n); 41 42// Determine if a number is prime 43template <class T> 44inline bool | 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; --- 28 unchanged lines hidden (view full) --- 37#include "base/types.hh" 38 39// Returns the prime number one less than n. 40int prevPrime(int n); 41 42// Determine if a number is prime 43template <class T> 44inline bool |
45isPrime(T n) | 45isPrime(const T& n) |
46{ 47 T i; 48 49 if (n == 2 || n == 3) 50 return true; 51 52 // Don't try every odd number to prove if it is a prime. 53 // Toggle between every 2nd and 4th number. --- 4 unchanged lines hidden (view full) --- 58 } 59 } 60 61 return true; 62} 63 64template <class T> 65inline T | 46{ 47 T i; 48 49 if (n == 2 || n == 3) 50 return true; 51 52 // Don't try every odd number to prove if it is a prime. 53 // Toggle between every 2nd and 4th number. --- 4 unchanged lines hidden (view full) --- 58 } 59 } 60 61 return true; 62} 63 64template <class T> 65inline T |
66leastSigBit(T n) | 66leastSigBit(const T& n) |
67{ 68 return n & ~(n - 1); 69} 70 71template <class T> 72inline bool | 67{ 68 return n & ~(n - 1); 69} 70 71template <class T> 72inline bool |
73isPowerOf2(T n) | 73isPowerOf2(const T& n) |
74{ 75 return n != 0 && leastSigBit(n) == n; 76} 77 78inline uint64_t 79power(uint32_t n, uint32_t e) 80{ 81 if (e > 20) --- 84 unchanged lines hidden (view full) --- 166floorLog2(long long x) 167{ 168 assert(x > 0); 169 return floorLog2((unsigned long long)x); 170} 171 172template <class T> 173inline int | 74{ 75 return n != 0 && leastSigBit(n) == n; 76} 77 78inline uint64_t 79power(uint32_t n, uint32_t e) 80{ 81 if (e > 20) --- 84 unchanged lines hidden (view full) --- 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) | 174ceilLog2(const 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 | 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) | 184floorPow2(const T& n) |
185{ 186 return (T)1 << floorLog2(n); 187} 188 189template <class T> 190inline T | 185{ 186 return (T)1 << floorLog2(n); 187} 188 189template <class T> 190inline T |
191ceilPow2(T n) | 191ceilPow2(const T& n) |
192{ 193 return (T)1 << ceilLog2(n); 194} 195 196template <class T, class U> 197inline T 198divCeil(const T& a, const U& b) 199{ 200 return (a + b - 1) / b; 201} 202 | 192{ 193 return (T)1 << ceilLog2(n); 194} 195 196template <class T, class U> 197inline T 198divCeil(const T& a, const U& b) 199{ 200 return (a + b - 1) / b; 201} 202 |
203template | 203template <class T, class U> |
204inline T | 204inline T |
205roundUp(T val, int align) | 205roundUp(const T& val, const U& align) |
206{ 207 T mask = (T)align - 1; 208 return (val + mask) & ~mask; 209} 210 | 206{ 207 T mask = (T)align - 1; 208 return (val + mask) & ~mask; 209} 210 |
211template | 211template <class T, class U> |
212inline T | 212inline T |
213roundDown(T val, int align) | 213roundDown(const T& val, const U& align) |
214{ 215 T mask = (T)align - 1; 216 return val & ~mask; 217} 218 219inline bool 220isHex(char c) 221{ --- 33 unchanged lines hidden --- | 214{ 215 T mask = (T)align - 1; 216 return val & ~mask; 217} 218 219inline bool 220isHex(char c) 221{ --- 33 unchanged lines hidden --- |