$search
00001 00005 #include <blort/Recognizer3D/Math.hh> 00006 00007 00008 namespace P 00009 { 00010 00011 00016 int timeval_subtract(struct timeval *result, struct timeval *x, 00017 struct timeval *y) 00018 { 00019 if(x->tv_usec < y->tv_usec) 00020 { 00021 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; 00022 y->tv_usec -= 1000000 * nsec; 00023 y->tv_sec += nsec; 00024 } 00025 if(x->tv_usec - y->tv_usec > 1000000) 00026 { 00027 int nsec = (y->tv_usec - x->tv_usec) / 1000000; 00028 y->tv_usec += 1000000 * nsec; 00029 y->tv_sec -= nsec; 00030 } 00031 result->tv_sec = x->tv_sec - y->tv_sec; 00032 result->tv_usec = x->tv_usec - y->tv_usec; 00033 return x->tv_sec < y->tv_sec; 00034 } 00035 00039 double timespec_diff(struct timespec *x, struct timespec *y) 00040 { 00041 /*timespec res = {x->tv_sec - y->tv_sec, x->tv_nsec - y->tv_nsec}; 00042 if(res.tv_nsec >= 1000000000) 00043 { 00044 res.tv_nsec -= 1000000000; 00045 res.tv_sec++; 00046 } 00047 if(res.tv_nsec < 0) 00048 { 00049 res.tv_nsec += 1000000000; 00050 res.tv_sec--; 00051 } 00052 return (double)res.tv_sec + 1e-9*(double)res.tv_nsec;*/ 00053 /* TODO: make the above clearer version handle nsec overflows of more than one 00054 * sec, see below*/ 00055 if(x->tv_nsec < y->tv_nsec) 00056 { 00057 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1; 00058 y->tv_nsec -= 1000000000 * nsec; 00059 y->tv_sec += nsec; 00060 } 00061 if(x->tv_nsec - y->tv_nsec > 1000000000) 00062 { 00063 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000; 00064 y->tv_nsec += 1000000000 * nsec; 00065 y->tv_sec -= nsec; 00066 } 00067 return (double)(x->tv_sec - y->tv_sec) + 00068 (double)(x->tv_nsec - y->tv_nsec)/1000000000.; 00069 } 00070 00071 00072 00073 } 00074