| 1 | /* This file is part of the Linux Trace Toolkit trace reading library |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License Version 2.1 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #ifndef LTT_TIME_H |
| 20 | #define LTT_TIME_H |
| 21 | |
| 22 | #include <glib.h> |
| 23 | |
| 24 | |
| 25 | typedef struct _LttTime { |
| 26 | unsigned long tv_sec; |
| 27 | unsigned long tv_nsec; |
| 28 | } LttTime; |
| 29 | |
| 30 | |
| 31 | #define NANOSECONDS_PER_SECOND 1000000000 |
| 32 | #define SHIFT_CONST 1.07374182400631629848 |
| 33 | |
| 34 | static const LttTime ltt_time_zero = { 0, 0 }; |
| 35 | |
| 36 | static const LttTime ltt_time_one = { 0, 1 }; |
| 37 | |
| 38 | static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND }; |
| 39 | |
| 40 | static inline LttTime ltt_time_sub(LttTime t1, LttTime t2) |
| 41 | { |
| 42 | LttTime res; |
| 43 | res.tv_sec = t1.tv_sec - t2.tv_sec; |
| 44 | res.tv_nsec = t1.tv_nsec - t2.tv_nsec; |
| 45 | if(t1.tv_nsec < t2.tv_nsec) { |
| 46 | res.tv_sec--; |
| 47 | res.tv_nsec += NANOSECONDS_PER_SECOND; |
| 48 | } |
| 49 | return res; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static inline LttTime ltt_time_add(LttTime t1, LttTime t2) |
| 54 | { |
| 55 | LttTime res; |
| 56 | res.tv_nsec = t1.tv_nsec + t2.tv_nsec; |
| 57 | res.tv_sec = t1.tv_sec + t2.tv_sec; |
| 58 | if(res.tv_nsec >= NANOSECONDS_PER_SECOND) { |
| 59 | res.tv_sec++; |
| 60 | res.tv_nsec -= NANOSECONDS_PER_SECOND; |
| 61 | } |
| 62 | return res; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static inline int ltt_time_compare(LttTime t1, LttTime t2) |
| 67 | { |
| 68 | if(t1.tv_sec > t2.tv_sec) return 1; |
| 69 | if(t1.tv_sec < t2.tv_sec) return -1; |
| 70 | if(t1.tv_nsec > t2.tv_nsec) return 1; |
| 71 | if(t1.tv_nsec < t2.tv_nsec) return -1; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b)) |
| 76 | #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b)) |
| 77 | |
| 78 | #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF |
| 79 | static inline double ltt_time_to_double(LttTime t1) |
| 80 | { |
| 81 | /* We lose precision if tv_sec is > than (2^23)-1 |
| 82 | * |
| 83 | * Max values that fits in a double (53 bits precision on normalised |
| 84 | * mantissa): |
| 85 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
| 86 | * |
| 87 | * So we have 53-30 = 23 bits left for tv_sec. |
| 88 | * */ |
| 89 | #ifdef EXTRA_CHECK |
| 90 | g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE); |
| 91 | if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE) |
| 92 | g_warning("Precision loss in conversion LttTime to double"); |
| 93 | #endif //EXTRA_CHECK |
| 94 | return ((double)t1.tv_sec * (double)NANOSECONDS_PER_SECOND) + (double)t1.tv_nsec; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | static inline LttTime ltt_time_from_double(double t1) |
| 99 | { |
| 100 | /* We lose precision if tv_sec is > than (2^23)-1 |
| 101 | * |
| 102 | * Max values that fits in a double (53 bits precision on normalised |
| 103 | * mantissa): |
| 104 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
| 105 | * |
| 106 | * So we have 53-30 = 23 bits left for tv_sec. |
| 107 | * */ |
| 108 | #ifdef EXTRA_CHECK |
| 109 | g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE); |
| 110 | if(t1 > MAX_TV_SEC_TO_DOUBLE) |
| 111 | g_warning("Conversion from non precise double to LttTime"); |
| 112 | #endif //EXTRA_CHECK |
| 113 | LttTime res; |
| 114 | //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND; |
| 115 | res.tv_sec = (guint64)(t1 * SHIFT_CONST) >> 30; |
| 116 | res.tv_nsec = (t1 - (res.tv_sec*NANOSECONDS_PER_SECOND)); |
| 117 | return res; |
| 118 | } |
| 119 | |
| 120 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
| 121 | * of precision. |
| 122 | */ |
| 123 | static inline LttTime ltt_time_mul(LttTime t1, double d) |
| 124 | { |
| 125 | LttTime res; |
| 126 | |
| 127 | double time_double = ltt_time_to_double(t1); |
| 128 | |
| 129 | time_double = time_double * d; |
| 130 | |
| 131 | res = ltt_time_from_double(time_double); |
| 132 | |
| 133 | return res; |
| 134 | |
| 135 | #if 0 |
| 136 | /* What is that ? (Mathieu) */ |
| 137 | if(f == 0.0){ |
| 138 | res.tv_sec = 0; |
| 139 | res.tv_nsec = 0; |
| 140 | }else{ |
| 141 | double d; |
| 142 | d = 1.0/f; |
| 143 | sec = t1.tv_sec / (double)d; |
| 144 | res.tv_sec = sec; |
| 145 | res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) * |
| 146 | NANOSECONDS_PER_SECOND; |
| 147 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 148 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 149 | } |
| 150 | return res; |
| 151 | #endif //0 |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
| 156 | * of precision. |
| 157 | */ |
| 158 | static inline LttTime ltt_time_div(LttTime t1, double d) |
| 159 | { |
| 160 | LttTime res; |
| 161 | |
| 162 | double time_double = ltt_time_to_double(t1); |
| 163 | |
| 164 | time_double = time_double / d; |
| 165 | |
| 166 | res = ltt_time_from_double(time_double); |
| 167 | |
| 168 | return res; |
| 169 | |
| 170 | |
| 171 | #if 0 |
| 172 | double sec; |
| 173 | LttTime res; |
| 174 | |
| 175 | sec = t1.tv_sec / (double)f; |
| 176 | res.tv_sec = sec; |
| 177 | res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) * |
| 178 | NANOSECONDS_PER_SECOND; |
| 179 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
| 180 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 181 | return res; |
| 182 | #endif //0 |
| 183 | } |
| 184 | |
| 185 | static inline guint64 ltt_time_to_uint64(LttTime t1) |
| 186 | { |
| 187 | return (guint64)t1.tv_sec*NANOSECONDS_PER_SECOND |
| 188 | + (guint64)t1.tv_nsec; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL |
| 193 | static inline LttTime ltt_time_from_uint64(guint64 t1) |
| 194 | { |
| 195 | /* We lose precision if tv_sec is > than (2^62)-1 |
| 196 | * */ |
| 197 | #ifdef EXTRA_CHECK |
| 198 | g_assert(t1 <= MAX_TV_SEC_TO_UINT64); |
| 199 | if(t1 > MAX_TV_SEC_TO_UINT64) |
| 200 | g_warning("Conversion from non precise uint64 to LttTime"); |
| 201 | #endif //EXTRA_CHECK |
| 202 | LttTime res; |
| 203 | res.tv_sec = t1/NANOSECONDS_PER_SECOND; |
| 204 | res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND); |
| 205 | return res; |
| 206 | } |
| 207 | |
| 208 | #endif // LTT_TIME_H |