#ifndef __MISC_TOOLS_H__
#define __MISC_TOOLS_H__
#include "Constants.h"
#if (defined(WIN32) || defined(__MINGW32__))
#include <sys/time.h>
#endif
namespace ale {
inline int pow(int x, int p) {
if (p == 0)
return 1;
if (x == 0 && p > 0)
return 0;
if (p < 0) {
assert(x == 1 || x == -1);
return (-p % 2) ? x : 1;
}
int r = 1;
for (;;) {
if (p & 1)
r *= x;
if ((p >>= 1) == 0)
return r;
x *= x;
}
}
inline void bound(int& x, int lower_bound, int upper_bound) {
if (x > upper_bound) {
x = upper_bound;
}
if (x < lower_bound) {
x = lower_bound;
}
}
#if (defined(WIN32) || defined(__MINGW32__))
#include <windows.h>
inline long timeMillis() { return GetTickCount(); }
#else
inline long timeMillis() {
struct timeval ts;
gettimeofday(&ts, NULL);
return ts.tv_sec * 1000 + ts.tv_usec / 1000;
}
#endif
}
#endif