9c312311 |
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 | |
308711e5 |
19 | #ifndef LTT_TIME_H |
20 | #define LTT_TIME_H |
21 | |
a00149f6 |
22 | #include <glib.h> |
23 | |
308711e5 |
24 | |
25 | typedef struct _LttTime { |
26 | unsigned long tv_sec; |
27 | unsigned long tv_nsec; |
28 | } LttTime; |
29 | |
30 | |
0aa6c3a1 |
31 | #define NANOSECONDS_PER_SECOND 1000000000 |
0ce58d10 |
32 | #define SHIFT_CONST 1.07374182400631629848 |
308711e5 |
33 | |
0aa6c3a1 |
34 | static const LttTime ltt_time_zero = { 0, 0 }; |
308711e5 |
35 | |
18206708 |
36 | static const LttTime ltt_time_one = { 0, 1 }; |
37 | |
0aa6c3a1 |
38 | static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND }; |
308711e5 |
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; |
f3167549 |
44 | res.tv_nsec = t1.tv_nsec - t2.tv_nsec; |
308711e5 |
45 | if(t1.tv_nsec < t2.tv_nsec) { |
46 | res.tv_sec--; |
f3167549 |
47 | res.tv_nsec += NANOSECONDS_PER_SECOND; |
308711e5 |
48 | } |
49 | return res; |
50 | } |
51 | |
52 | |
53 | static inline LttTime ltt_time_add(LttTime t1, LttTime t2) |
54 | { |
55 | LttTime res; |
308711e5 |
56 | res.tv_nsec = t1.tv_nsec + t2.tv_nsec; |
f3167549 |
57 | res.tv_sec = t1.tv_sec + t2.tv_sec; |
308711e5 |
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 | |
280f9968 |
66 | /* Fastest comparison : t1 > t2 */ |
308711e5 |
67 | static inline int ltt_time_compare(LttTime t1, LttTime t2) |
68 | { |
280f9968 |
69 | int ret=0; |
70 | if(t1.tv_sec > t2.tv_sec) ret = 1; |
71 | else if(t1.tv_sec < t2.tv_sec) ret = -1; |
72 | else if(t1.tv_nsec > t2.tv_nsec) ret = 1; |
73 | else if(t1.tv_nsec < t2.tv_nsec) ret = -1; |
74 | |
75 | return ret; |
308711e5 |
76 | } |
77 | |
0aa6c3a1 |
78 | #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b)) |
79 | #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b)) |
80 | |
8aee234c |
81 | #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF |
308711e5 |
82 | static inline double ltt_time_to_double(LttTime t1) |
83 | { |
8aee234c |
84 | /* We lose precision if tv_sec is > than (2^23)-1 |
85 | * |
86 | * Max values that fits in a double (53 bits precision on normalised |
87 | * mantissa): |
88 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
89 | * |
90 | * So we have 53-30 = 23 bits left for tv_sec. |
91 | * */ |
c74e0cf9 |
92 | #ifdef EXTRA_CHECK |
0c5dbe3b |
93 | g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE); |
8aee234c |
94 | if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE) |
95 | g_warning("Precision loss in conversion LttTime to double"); |
c74e0cf9 |
96 | #endif //EXTRA_CHECK |
a18124ff |
97 | return ((double)t1.tv_sec * (double)NANOSECONDS_PER_SECOND) + (double)t1.tv_nsec; |
308711e5 |
98 | } |
99 | |
100 | |
101 | static inline LttTime ltt_time_from_double(double t1) |
102 | { |
8aee234c |
103 | /* We lose precision if tv_sec is > than (2^23)-1 |
104 | * |
105 | * Max values that fits in a double (53 bits precision on normalised |
106 | * mantissa): |
107 | * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30 |
108 | * |
109 | * So we have 53-30 = 23 bits left for tv_sec. |
110 | * */ |
c74e0cf9 |
111 | #ifdef EXTRA_CHECK |
0c5dbe3b |
112 | g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE); |
8aee234c |
113 | if(t1 > MAX_TV_SEC_TO_DOUBLE) |
114 | g_warning("Conversion from non precise double to LttTime"); |
c74e0cf9 |
115 | #endif //EXTRA_CHECK |
308711e5 |
116 | LttTime res; |
0ce58d10 |
117 | //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND; |
118 | res.tv_sec = (guint64)(t1 * SHIFT_CONST) >> 30; |
c74e0cf9 |
119 | res.tv_nsec = (t1 - (res.tv_sec*NANOSECONDS_PER_SECOND)); |
308711e5 |
120 | return res; |
121 | } |
122 | |
8d1e6362 |
123 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
124 | * of precision. |
125 | */ |
126 | static inline LttTime ltt_time_mul(LttTime t1, double d) |
127 | { |
128 | LttTime res; |
129 | |
130 | double time_double = ltt_time_to_double(t1); |
131 | |
132 | time_double = time_double * d; |
133 | |
134 | res = ltt_time_from_double(time_double); |
135 | |
136 | return res; |
137 | |
138 | #if 0 |
139 | /* What is that ? (Mathieu) */ |
140 | if(f == 0.0){ |
141 | res.tv_sec = 0; |
142 | res.tv_nsec = 0; |
143 | }else{ |
144 | double d; |
145 | d = 1.0/f; |
146 | sec = t1.tv_sec / (double)d; |
147 | res.tv_sec = sec; |
148 | res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) * |
149 | NANOSECONDS_PER_SECOND; |
150 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
151 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
152 | } |
153 | return res; |
154 | #endif //0 |
155 | } |
156 | |
157 | |
158 | /* Use ltt_time_to_double and ltt_time_from_double to check for lack |
159 | * of precision. |
160 | */ |
161 | static inline LttTime ltt_time_div(LttTime t1, double d) |
162 | { |
163 | LttTime res; |
164 | |
165 | double time_double = ltt_time_to_double(t1); |
166 | |
167 | time_double = time_double / d; |
168 | |
169 | res = ltt_time_from_double(time_double); |
170 | |
171 | return res; |
172 | |
173 | |
174 | #if 0 |
175 | double sec; |
176 | LttTime res; |
177 | |
178 | sec = t1.tv_sec / (double)f; |
179 | res.tv_sec = sec; |
180 | res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) * |
181 | NANOSECONDS_PER_SECOND; |
182 | res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND; |
183 | res.tv_nsec %= NANOSECONDS_PER_SECOND; |
184 | return res; |
185 | #endif //0 |
186 | } |
187 | |
90ef7e4a |
188 | static inline guint64 ltt_time_to_uint64(LttTime t1) |
189 | { |
190 | return (guint64)t1.tv_sec*NANOSECONDS_PER_SECOND |
191 | + (guint64)t1.tv_nsec; |
192 | } |
193 | |
194 | |
195 | #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL |
196 | static inline LttTime ltt_time_from_uint64(guint64 t1) |
197 | { |
198 | /* We lose precision if tv_sec is > than (2^62)-1 |
199 | * */ |
c74e0cf9 |
200 | #ifdef EXTRA_CHECK |
90ef7e4a |
201 | g_assert(t1 <= MAX_TV_SEC_TO_UINT64); |
202 | if(t1 > MAX_TV_SEC_TO_UINT64) |
203 | g_warning("Conversion from non precise uint64 to LttTime"); |
c74e0cf9 |
204 | #endif //EXTRA_CHECK |
90ef7e4a |
205 | LttTime res; |
206 | res.tv_sec = t1/NANOSECONDS_PER_SECOND; |
207 | res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND); |
208 | return res; |
209 | } |
8d1e6362 |
210 | |
308711e5 |
211 | #endif // LTT_TIME_H |