Rename intmath.h to intmath.hh
clean up the formatting make things use templates since this is C++ now. base/circlebuf.cc: base/intmath.cc: base/intmath.hh: base/range.hh: base/remote_gdb.cc: base/statistics.cc: base/str.cc: intmath.h -> intmath.hh base/intmath.hh: Make this more like C++ by templatizing these functions --HG-- rename : base/intmath.h => base/intmath.hh extra : convert_revision : 8f9b14de9db751b1bd78588c51613d77afd96989
This commit is contained in:
parent
62c4d82600
commit
1092515e65
7 changed files with 79 additions and 57 deletions
|
@ -33,9 +33,9 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "cprintf.hh"
|
|
||||||
#include "circlebuf.hh"
|
#include "circlebuf.hh"
|
||||||
#include "intmath.h"
|
#include "cprintf.hh"
|
||||||
|
#include "intmath.hh"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "intmath.h"
|
#include "intmath.hh"
|
||||||
|
|
||||||
int
|
int
|
||||||
PrevPrime(int n)
|
PrevPrime(int n)
|
||||||
|
|
|
@ -26,84 +26,106 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __INTMATH_H__
|
#ifndef __INTMATH_HH__
|
||||||
#define __INTMATH_H__
|
#define __INTMATH_HH__
|
||||||
|
|
||||||
// Returns the prime number one less than n.
|
// Returns the prime number one less than n.
|
||||||
int PrevPrime(int n);
|
int PrevPrime(int n);
|
||||||
|
|
||||||
// Determine if a number is prime
|
// Determine if a number is prime
|
||||||
|
template <class T>
|
||||||
inline bool
|
inline bool
|
||||||
IsPrime(int n)
|
IsPrime(T n)
|
||||||
{
|
{
|
||||||
int i;
|
T i;
|
||||||
|
|
||||||
if (n == 2 || n == 3)
|
if (n == 2 || n == 3)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Don't try every odd number to prove if it is a prime.
|
// Don't try every odd number to prove if it is a prime.
|
||||||
// Toggle between every 2nd and 4th number.
|
// Toggle between every 2nd and 4th number.
|
||||||
// (This is because every 6th odd number is divisible by 3.)
|
// (This is because every 6th odd number is divisible by 3.)
|
||||||
for (i = 5; i*i <= n; i += 6) {
|
for (i = 5; i*i <= n; i += 6) {
|
||||||
if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) {
|
if (((n % i) == 0 ) || ((n % (i + 2)) == 0) ) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline unsigned
|
template <class T>
|
||||||
LeastSigBit(unsigned n)
|
inline T
|
||||||
{ return n & ~(n - 1); }
|
LeastSigBit(T n)
|
||||||
|
|
||||||
inline bool
|
|
||||||
IsPowerOf2(unsigned n)
|
|
||||||
{ return n != 0 && LeastSigBit(n) == n; }
|
|
||||||
|
|
||||||
inline int
|
|
||||||
FloorLog2(unsigned x)
|
|
||||||
{
|
{
|
||||||
if (x == 0)
|
return n & ~(n - 1);
|
||||||
return -1;
|
|
||||||
|
|
||||||
int y = 0;
|
|
||||||
|
|
||||||
if (x & 0xffff0000) { y += 16; x >>= 16; }
|
|
||||||
if (x & 0x0000ff00) { y += 8; x >>= 8; }
|
|
||||||
if (x & 0x000000f0) { y += 4; x >>= 4; }
|
|
||||||
if (x & 0x0000000c) { y += 2; x >>= 2; }
|
|
||||||
if (x & 0x00000002) { y += 1; }
|
|
||||||
|
|
||||||
return y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline bool
|
||||||
|
IsPowerOf2(T n)
|
||||||
|
{
|
||||||
|
return n != 0 && LeastSigBit(n) == n;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
inline int
|
inline int
|
||||||
CeilLog2(unsigned n)
|
FloorLog2(T x)
|
||||||
{ return FloorLog2(n-1)+1; }
|
{
|
||||||
|
if (x == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
inline unsigned
|
int y = 0;
|
||||||
FloorPow2(unsigned n)
|
|
||||||
{ return 1 << FloorLog2(n); }
|
|
||||||
|
|
||||||
inline unsigned
|
if (x & 0xffff0000) { y += 16; x >>= 16; }
|
||||||
CeilPow2(unsigned n)
|
if (x & 0x0000ff00) { y += 8; x >>= 8; }
|
||||||
{ return 1 << CeilLog2(n); }
|
if (x & 0x000000f0) { y += 4; x >>= 4; }
|
||||||
|
if (x & 0x0000000c) { y += 2; x >>= 2; }
|
||||||
|
if (x & 0x00000002) { y += 1; }
|
||||||
|
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline int
|
||||||
|
CeilLog2(T n)
|
||||||
|
{
|
||||||
|
return FloorLog2(n - 1) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline T
|
||||||
|
FloorPow2(T n)
|
||||||
|
{
|
||||||
|
return (T)1 << FloorLog2(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
inline T
|
||||||
|
CeilPow2(T n)
|
||||||
|
{
|
||||||
|
return (T)1 << CeilLog2(n);
|
||||||
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
IsHex(char c)
|
IsHex(char c)
|
||||||
{ return (c >= '0' && c <= '9' ||
|
{
|
||||||
c >= 'A' && c <= 'F' ||
|
return c >= '0' && c <= '9' ||
|
||||||
c >= 'a' && c <= 'f');
|
c >= 'A' && c <= 'F' ||
|
||||||
|
c >= 'a' && c <= 'f';
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
IsOct(char c)
|
IsOct(char c)
|
||||||
{ return (c >= '0' && c <= '7'); }
|
{
|
||||||
|
return c >= '0' && c <= '7';
|
||||||
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
IsDec(char c)
|
IsDec(char c)
|
||||||
{ return (c >= '0' && c <= '9'); }
|
{
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
inline int
|
inline int
|
||||||
Hex2Int(char c)
|
Hex2Int(char c)
|
||||||
|
@ -120,4 +142,4 @@ Hex2Int(char c)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // __INTMATH_H__
|
#endif // __INTMATH_HH__
|
|
@ -31,8 +31,8 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include "intmath.hh"
|
||||||
#include "str.hh"
|
#include "str.hh"
|
||||||
#include "intmath.h"
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class Range
|
class Range
|
||||||
|
|
|
@ -94,7 +94,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "exec_context.hh"
|
#include "exec_context.hh"
|
||||||
#include "intmath.h"
|
#include "intmath.hh"
|
||||||
#include "kgdb.h"
|
#include "kgdb.h"
|
||||||
|
|
||||||
#include "physical_memory.hh"
|
#include "physical_memory.hh"
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "cprintf.hh"
|
#include "cprintf.hh"
|
||||||
#include "intmath.h"
|
#include "intmath.hh"
|
||||||
#include "misc.hh"
|
#include "misc.hh"
|
||||||
#include "statistics.hh"
|
#include "statistics.hh"
|
||||||
#include "str.hh"
|
#include "str.hh"
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "intmath.h"
|
#include "intmath.hh"
|
||||||
#include "str.hh"
|
#include "str.hh"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
Loading…
Reference in a new issue