PFUNC  1.0
pfunc/utility.h
Go to the documentation of this file.
00001 #ifndef PFUNC_UTILITY_H
00002 #define PFUNC_UTILITY_H
00003 
00004 #include <pfunc/config.h>
00005 #include <pfunc/environ.hpp>
00006 #include <stdlib.h>
00007 
00008 #if PFUNC_WINDOWS == 1
00009 #include <Windows.h>
00010 #endif
00011 
00012 #if PFUNC_HAVE_SYS_TIME_H == 1
00013 #include <sys/time.h>
00014 #endif
00015 
00020 #if defined (c_plusplus) || defined (__cplusplus)
00021 extern "C" {
00022 #endif
00023 
00029 static const double NORMALIZER = static_cast<double>(RAND_MAX);
00030 static inline double get_next_rand () {
00031   return (static_cast<double>(rand())/NORMALIZER);
00032 }
00033 
00039 static inline int get_closest_power_of_2 (const int n) {
00040   int closest_power_of_2 = 0x1;
00041   while (n > closest_power_of_2) closest_power_of_2 = closest_power_of_2 << 1;
00042   return closest_power_of_2;
00043 }
00044 
00052 static PFUNC_INLINE double micro_time () {
00053 #if PFUNC_WINDOWS == 1
00054 
00056   SYSTEMTIME sys_time;
00057   FILETIME file_time;
00058   ULARGE_INTEGER uli;
00059 
00060   GetSystemTime (&sys_time);
00061   SystemTimeToFileTime (&sys_time, &file_time);
00062   uli.LowPart = file_time.dwLowDateTime;
00063   uli.HighPart = file_time.dwHighDateTime;
00064 
00069   return ((double) uli.QuadPart)*1e-7;
00070 
00071 #elif PFUNC_HAVE_SYS_TIME_H == 1
00072 
00074   struct timeval tp;
00075   gettimeofday(&tp, NULL);
00076   return (double)tp.tv_sec + tp.tv_usec*1.e-6;
00077 
00078 #else 
00079 #warn "No accurate implementation found --- returning 0.0 always"
00080   return 0.0;
00081 #endif
00082 }
00083 
00084 #if defined (c_plusplus) || defined (__cplusplus)
00085 }
00086 #endif
00087 
00088 #endif /* PFUNC_UTILITY_H */