00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _LUXRAYS_UTILS_H
00023 #define _LUXRAYS_UTILS_H
00024
00025 #include <cmath>
00026
00027 #if defined (__linux__)
00028 #include <pthread.h>
00029 #endif
00030
00031 #include <boost/thread.hpp>
00032
00033 #if defined(__APPLE__) // OSX adaptions Jens Verwiebe
00034 # define memalign(a,b) valloc(b)
00035 #include <string>
00036 typedef unsigned int u_int;
00037 #endif
00038
00039 #if defined(__APPLE__)
00040 #if (__GNUC__ == 3) || (__GNUC__ == 4)
00041 extern "C" {
00042 int isinf(float);
00043 int isnan(double);
00044 int isnanf(float);
00045 }
00046 #endif
00047 #endif
00048
00049 #if defined(WIN32)
00050 #include <float.h>
00051 #define isnanf(a) _isnan(a)
00052 #endif
00053
00054 #if defined(__APPLE__)
00055 #include <malloc/malloc.h>
00056 #else
00057 #include <malloc.h>
00058 #endif
00059
00060 #include <sstream>
00061
00062 #if defined(__linux__) || defined(__APPLE__)
00063 #include <stddef.h>
00064 #include <sys/time.h>
00065 #elif defined (WIN32)
00066 #include <windows.h>
00067 #else
00068 Unsupported Platform !!!
00069 #endif
00070
00071 #ifndef M_PI
00072 #define M_PI 3.14159265358979323846f
00073 #endif
00074
00075 #ifndef INFINITY
00076 #define INFINITY (std::numeric_limits<float>::infinity())
00077 #endif
00078
00079 #ifndef INV_PI
00080 #define INV_PI 0.31830988618379067154f
00081 #endif
00082
00083 #ifndef INV_TWOPI
00084 #define INV_TWOPI 0.15915494309189533577f
00085 #endif
00086
00087 namespace luxrays {
00088
00089 inline double WallClockTime() {
00090 #if defined(__linux__) || defined(__APPLE__)
00091 struct timeval t;
00092 gettimeofday(&t, NULL);
00093
00094 return t.tv_sec + t.tv_usec / 1000000.0;
00095 #elif defined (WIN32)
00096 return GetTickCount() / 1000.0;
00097 #else
00098 Unsupported Platform !!!
00099 #endif
00100 }
00101
00102 template<class T> inline T Clamp(T val, T low, T high) {
00103 return val > low ? (val < high ? val : high) : low;
00104 }
00105
00106 template<class T> inline T Max(T a, T b) {
00107 return a > b ? a : b;
00108 }
00109
00110 template<class T> inline T Min(T a, T b) {
00111 return a < b ? a : b;
00112 }
00113
00114 template<class T> inline void Swap(T &a, T &b) {
00115 const T tmp = a;
00116 a = b;
00117 b = tmp;
00118 }
00119
00120 template<class T> inline T Mod(T a, T b) {
00121 if (b == 0)
00122 b = 1;
00123
00124 a %= b;
00125 if (a < 0)
00126 a += b;
00127
00128 return a;
00129 }
00130
00131 inline float Radians(float deg) {
00132 return (M_PI / 180.f) * deg;
00133 }
00134
00135 inline float Degrees(float rad) {
00136 return (180.f / M_PI) * rad;
00137 }
00138
00139 inline float Sgn(float a) {
00140 return a < 0.f ? -1.f : 1.f;
00141 }
00142
00143 inline int Sgn(int a) {
00144 return a < 0 ? -1 : 1.f;
00145 }
00146
00147 template<class T> inline int Float2Int(T val) {
00148 return static_cast<int> (val);
00149 }
00150
00151 template<class T> inline unsigned int Float2UInt(T val) {
00152 return val >= 0 ? static_cast<unsigned int> (val) : 0;
00153 }
00154
00155 inline int Floor2Int(double val) {
00156 return static_cast<int> (floor(val));
00157 }
00158
00159 inline int Floor2Int(float val) {
00160 return static_cast<int> (floorf(val));
00161 }
00162
00163 inline unsigned int Floor2UInt(double val) {
00164 return val > 0. ? static_cast<unsigned int> (floor(val)) : 0;
00165 }
00166
00167 inline unsigned int Floor2UInt(float val) {
00168 return val > 0.f ? static_cast<unsigned int> (floorf(val)) : 0;
00169 }
00170
00171 inline int Ceil2Int(double val) {
00172 return static_cast<int> (ceil(val));
00173 }
00174
00175 inline int Ceil2Int(float val) {
00176 return static_cast<int> (ceilf(val));
00177 }
00178
00179 inline unsigned int Ceil2UInt(double val) {
00180 return val > 0. ? static_cast<unsigned int> (ceil(val)) : 0;
00181 }
00182
00183 inline unsigned int Ceil2UInt(float val) {
00184 return val > 0.f ? static_cast<unsigned int> (ceilf(val)) : 0;
00185 }
00186
00187 template <class T> inline std::string ToString(const T& t) {
00188 std::stringstream ss;
00189 ss << t;
00190 return ss.str();
00191 }
00192
00193 template <class T> inline T RoundUp(const T a, const T b) {
00194 const unsigned int r = a % b;
00195 if (r == 0)
00196 return a;
00197 else
00198 return a + b - r;
00199 }
00200
00201 template <class T> inline T RoundUpPow2(T v) {
00202 v--;
00203
00204 v |= v >> 1;
00205 v |= v >> 2;
00206 v |= v >> 4;
00207 v |= v >> 8;
00208 v |= v >> 16;
00209
00210 return v+1;
00211 }
00212
00213 inline unsigned int UIntLog2(unsigned int value) {
00214 unsigned int l = 0;
00215 while (value >>= 1) l++;
00216 return l;
00217 }
00218
00219 inline void StringTrim(std::string &str) {
00220 std::string::size_type pos = str.find_last_not_of(' ');
00221 if (pos != std::string::npos) {
00222 str.erase(pos + 1);
00223 pos = str.find_first_not_of(' ');
00224 if (pos != std::string::npos) str.erase(0, pos);
00225 } else str.erase(str.begin(), str.end());
00226 }
00227
00228 inline bool SetThreadRRPriority(boost::thread *thread, int pri = 0) {
00229 #if defined (__linux__) || defined (__APPLE__)
00230 {
00231 const pthread_t tid = (pthread_t)thread->native_handle();
00232
00233 int policy = SCHED_FIFO;
00234 int sysMinPriority = sched_get_priority_min(policy);
00235 struct sched_param param;
00236 param.sched_priority = sysMinPriority + pri;
00237
00238 return pthread_setschedparam(tid, policy, ¶m);
00239 }
00240 #elif defined (WIN32)
00241 {
00242 const HANDLE tid = (HANDLE)thread->native_handle();
00243 if (!SetPriorityClass(tid, HIGH_PRIORITY_CLASS))
00244 return false;
00245 else
00246 return true;
00247
00248
00249
00250
00251
00252 }
00253 #endif
00254 }
00255
00256
00257
00258
00259
00260 #ifndef L1_CACHE_LINE_SIZE
00261 #define L1_CACHE_LINE_SIZE 64
00262 #endif
00263
00264 template<class T> inline T *AllocAligned(size_t size, std::size_t N = L1_CACHE_LINE_SIZE) {
00265 #if defined(WIN32) && !defined(__CYGWIN__) // NOBOOK
00266 return static_cast<T *> (_aligned_malloc(size * sizeof (T), N));
00267 #else // NOBOOK
00268 return static_cast<T *> (memalign(N, size * sizeof (T)));
00269 #endif // NOBOOK
00270 }
00271
00272 template<class T> inline void FreeAligned(T *ptr) {
00273 #if defined(WIN32) && !defined(__CYGWIN__) // NOBOOK
00274 _aligned_free(ptr);
00275 #else // NOBOOK
00276 free(ptr);
00277 #endif // NOBOOK
00278 }
00279
00280 template <typename T, std::size_t N = 16 > class AlignedAllocator {
00281 public:
00282 typedef T value_type;
00283 typedef std::size_t size_type;
00284 typedef std::ptrdiff_t difference_type;
00285
00286 typedef T *pointer;
00287 typedef const T *const_pointer;
00288
00289 typedef T &reference;
00290 typedef const T &const_reference;
00291
00292 public:
00293
00294 inline AlignedAllocator() throw () {
00295 }
00296
00297 template <typename T2> inline AlignedAllocator(const AlignedAllocator<T2, N> &) throw () {
00298 }
00299
00300 inline ~AlignedAllocator() throw () {
00301 }
00302
00303 inline pointer adress(reference r) {
00304 return &r;
00305 }
00306
00307 inline const_pointer adress(const_reference r) const {
00308 return &r;
00309 }
00310
00311 inline pointer allocate(size_type n) {
00312 return AllocAligned<value_type > (n, N);
00313 }
00314
00315 inline void deallocate(pointer p, size_type) {
00316 FreeAligned(p);
00317 }
00318
00319 inline void construct(pointer p, const value_type &wert) {
00320 new (p) value_type(wert);
00321 }
00322
00323 inline void destroy(pointer p) {
00324 p->~value_type();
00325 }
00326
00327 inline size_type max_size() const throw () {
00328 return size_type(-1) / sizeof (value_type);
00329 }
00330
00331 template <typename T2> struct rebind {
00332 typedef AlignedAllocator<T2, N> other;
00333 };
00334 };
00335
00336 #define P_CLASS_ATTR __attribute__
00337 #define P_CLASS_ATTR __attribute__
00338
00339 #if defined(WIN32) && !defined(__CYGWIN__) // NOBOOK
00340
00341 class __declspec(align(16)) Aligned16 {
00342 #else // NOBOOK
00343
00344 class Aligned16 {
00345 #endif // NOBOOK
00346 public:
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 void *operator new(size_t s) {
00358 return AllocAligned<char>(s, 16);
00359 }
00360
00361 void *operator new (size_t s, void *q) {
00362 return q;
00363 }
00364
00365 void operator delete(void *p) {
00366 FreeAligned(p);
00367 }
00368 #if defined(WIN32) && !defined(__CYGWIN__) // NOBOOK
00369 };
00370 #else // NOBOOK
00371 } __attribute__((aligned(16)));
00372 #endif // NOBOOK
00373
00374 }
00375
00376 #endif