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