Change licence to GPLv2 for files moved from the old ltt directory to lttv
[lttv.git] / lttv / lttv / time.h
CommitLineData
7a4bdb54
YB
1/* This file is part of the Linux Trace Toolkit trace reading library
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
4 *
adc007f3
YB
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
7a4bdb54 8 *
adc007f3 9 * This program is distributed in the hope that it will be useful,
7a4bdb54 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
adc007f3
YB
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
7a4bdb54 13 *
adc007f3
YB
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA
7a4bdb54
YB
18 */
19
20#ifndef LTT_TIME_H
21#define LTT_TIME_H
22
23#include <glib.h>
24#include <math.h>
25#include <lttv/compiler.h>
26
27typedef struct _LttTime {
28 unsigned long tv_sec;
29 unsigned long tv_nsec;
30} LttTime;
31
190724cd
YB
32typedef struct _TimeInterval {
33 LttTime start_time;
34 LttTime end_time;
35} TimeInterval;
7a4bdb54
YB
36
37#define NANOSECONDS_PER_SECOND 1000000000
38
39/* We give the DIV and MUL constants so we can always multiply, for a
40 * division as well as a multiplication of NANOSECONDS_PER_SECOND */
41/* 2^30/1.07374182400631629848 = 1000000000.0 */
42#define DOUBLE_SHIFT_CONST_DIV 1.07374182400631629848
43#define DOUBLE_SHIFT 30
44
45/* 2^30*0.93132257461547851562 = 1000000000.0000000000 */
46#define DOUBLE_SHIFT_CONST_MUL 0.93132257461547851562
47
48
49/* 1953125 * 2^9 = NANOSECONDS_PER_SECOND */
50#define LTT_TIME_UINT_SHIFT_CONST 1953125
51#define LTT_TIME_UINT_SHIFT 9
52
53
54static const LttTime ltt_time_zero = { 0, 0 };
55
56static const LttTime ltt_time_one = { 0, 1 };
57
58static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND };
59
60static inline LttTime ltt_time_sub(LttTime t1, LttTime t2)
61{
62 LttTime res;
63 res.tv_sec = t1.tv_sec - t2.tv_sec;
64 res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
65 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
66 * higher probability of low value for t2.tv_sec, we will habitually
67 * not wrap.
68 */
69 if(unlikely(t1.tv_nsec < t2.tv_nsec)) {
70 res.tv_sec--;
71 res.tv_nsec += NANOSECONDS_PER_SECOND;
72 }
73 return res;
74}
75
76
77static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
78{
79 LttTime res;
80 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
81 res.tv_sec = t1.tv_sec + t2.tv_sec;
82 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
83 * higher probability of low value for t2.tv_sec, we will habitually
84 * not wrap.
85 */
86 if(unlikely(res.tv_nsec >= NANOSECONDS_PER_SECOND)) {
87 res.tv_sec++;
88 res.tv_nsec -= NANOSECONDS_PER_SECOND;
89 }
90 return res;
91}
92
93/* Fastest comparison : t1 > t2 */
94static inline int ltt_time_compare(LttTime t1, LttTime t2)
95{
96 int ret=0;
97 if(likely(t1.tv_sec > t2.tv_sec)) ret = 1;
98 else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1;
99 else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1;
100 else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1;
101
102 return ret;
103}
104
105#define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b))
106#define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b))
107
108#define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
109static inline double ltt_time_to_double(LttTime t1)
110{
111 /* We lose precision if tv_sec is > than (2^23)-1
112 *
113 * Max values that fits in a double (53 bits precision on normalised
114 * mantissa):
115 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
116 *
117 * So we have 53-30 = 23 bits left for tv_sec.
118 * */
119#ifdef EXTRA_CHECK
120 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
121 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
122 g_warning("Precision loss in conversion LttTime to double");
123#endif //EXTRA_CHECK
124 return ((double)((guint64)t1.tv_sec<<DOUBLE_SHIFT)
125 * (double)DOUBLE_SHIFT_CONST_MUL)
126 + (double)t1.tv_nsec;
127}
128
129
130static inline LttTime ltt_time_from_double(double t1)
131{
132 /* We lose precision if tv_sec is > than (2^23)-1
133 *
134 * Max values that fits in a double (53 bits precision on normalised
135 * mantissa):
136 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
137 *
138 * So we have 53-30 = 23 bits left for tv_sec.
139 * */
140#ifdef EXTRA_CHECK
141 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
142 if(t1 > MAX_TV_SEC_TO_DOUBLE)
143 g_warning("Conversion from non precise double to LttTime");
144#endif //EXTRA_CHECK
145 LttTime res;
146 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
147 res.tv_sec = (guint64)(t1 * DOUBLE_SHIFT_CONST_DIV) >> DOUBLE_SHIFT;
148 res.tv_nsec = (t1 - (((guint64)res.tv_sec<<LTT_TIME_UINT_SHIFT))
149 * LTT_TIME_UINT_SHIFT_CONST);
150 return res;
151}
152
153/* Use ltt_time_to_double and ltt_time_from_double to check for lack
154 * of precision.
155 */
156static inline LttTime ltt_time_mul(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#if 0
169 /* What is that ? (Mathieu) */
170 if(f == 0.0){
171 res.tv_sec = 0;
172 res.tv_nsec = 0;
173 }else{
174 double d;
175 d = 1.0/f;
176 sec = t1.tv_sec / (double)d;
177 res.tv_sec = sec;
178 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
179 NANOSECONDS_PER_SECOND;
180 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
181 res.tv_nsec %= NANOSECONDS_PER_SECOND;
182 }
183 return res;
184#endif //0
185}
186
187
188/* Use ltt_time_to_double and ltt_time_from_double to check for lack
189 * of precision.
190 */
191static inline LttTime ltt_time_div(LttTime t1, double d)
192{
193 LttTime res;
194
195 double time_double = ltt_time_to_double(t1);
196
197 time_double = time_double / d;
198
199 res = ltt_time_from_double(time_double);
200
201 return res;
202
203
204#if 0
205 double sec;
206 LttTime res;
207
208 sec = t1.tv_sec / (double)f;
209 res.tv_sec = sec;
210 res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) *
211 NANOSECONDS_PER_SECOND;
212 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
213 res.tv_nsec %= NANOSECONDS_PER_SECOND;
214 return res;
215#endif //0
216}
217
218
219static inline guint64 ltt_time_to_uint64(LttTime t1)
220{
221 return (((guint64)t1.tv_sec*LTT_TIME_UINT_SHIFT_CONST) << LTT_TIME_UINT_SHIFT)
222 + (guint64)t1.tv_nsec;
223}
224
225
226#define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
227
228/* The likely branch is with sec != 0, because most events in a bloc
229 * will be over 1s from the block start. (see tracefile.c)
230 */
231static inline LttTime ltt_time_from_uint64(guint64 t1)
232{
233 /* We lose precision if tv_sec is > than (2^62)-1
234 * */
235#ifdef EXTRA_CHECK
236 g_assert(t1 <= MAX_TV_SEC_TO_UINT64);
237 if(t1 > MAX_TV_SEC_TO_UINT64)
238 g_warning("Conversion from uint64 to non precise LttTime");
239#endif //EXTRA_CHECK
240 LttTime res;
241 //if(unlikely(t1 >= NANOSECONDS_PER_SECOND)) {
242 if(likely(t1>>LTT_TIME_UINT_SHIFT >= LTT_TIME_UINT_SHIFT_CONST)) {
243 //res.tv_sec = t1/NANOSECONDS_PER_SECOND;
244 res.tv_sec = (t1>>LTT_TIME_UINT_SHIFT)
245 /LTT_TIME_UINT_SHIFT_CONST; // acceleration
246 res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND);
247 } else {
248 res.tv_sec = 0;
249 res.tv_nsec = (guint32)t1;
250 }
251 return res;
252}
253
254#endif // LTT_TIME_H
This page took 0.031458 seconds and 4 git commands to generate.