further tweaks on ltt_trace_facility_by_id
[lttv.git] / ltt / branches / poly / ltt / time.h
CommitLineData
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>
1d1df11d 23#include <ltt/compiler.h>
308711e5 24
25typedef struct _LttTime {
26 unsigned long tv_sec;
27 unsigned long tv_nsec;
28} LttTime;
29
30
0aa6c3a1 31#define NANOSECONDS_PER_SECOND 1000000000
62b45a6e 32/* 2^30/1.07374182400631629848 = 1000000000.0 */
33#define DOUBLE_SHIFT_CONST 1.07374182400631629848
34#define DOUBLE_SHIFT 30
35
36/* 1953125 * 2^9 = NANOSECONDS_PER_SECOND */
37#define LTT_TIME_UINT_SHIFT_CONST 1953125
38#define LTT_TIME_UINT_SHIFT 9
39
308711e5 40
0aa6c3a1 41static const LttTime ltt_time_zero = { 0, 0 };
308711e5 42
18206708 43static const LttTime ltt_time_one = { 0, 1 };
44
0aa6c3a1 45static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND };
308711e5 46
47static inline LttTime ltt_time_sub(LttTime t1, LttTime t2)
48{
49 LttTime res;
50 res.tv_sec = t1.tv_sec - t2.tv_sec;
f3167549 51 res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
1d1df11d 52 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
53 * higher probability of low value for t2.tv_sec, we will habitually
54 * not wrap.
55 */
56 if(unlikely(t1.tv_nsec < t2.tv_nsec)) {
308711e5 57 res.tv_sec--;
f3167549 58 res.tv_nsec += NANOSECONDS_PER_SECOND;
308711e5 59 }
60 return res;
61}
62
63
64static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
65{
66 LttTime res;
308711e5 67 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
f3167549 68 res.tv_sec = t1.tv_sec + t2.tv_sec;
1d1df11d 69 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
70 * higher probability of low value for t2.tv_sec, we will habitually
71 * not wrap.
72 */
73 if(unlikely(res.tv_nsec >= NANOSECONDS_PER_SECOND)) {
308711e5 74 res.tv_sec++;
75 res.tv_nsec -= NANOSECONDS_PER_SECOND;
76 }
77 return res;
78}
79
280f9968 80/* Fastest comparison : t1 > t2 */
308711e5 81static inline int ltt_time_compare(LttTime t1, LttTime t2)
82{
280f9968 83 int ret=0;
887208b7 84 if(likely(t1.tv_sec > t2.tv_sec)) ret = 1;
85 else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1;
86 else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1;
87 else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1;
280f9968 88
89 return ret;
308711e5 90}
91
0aa6c3a1 92#define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b))
93#define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b))
94
8aee234c 95#define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
308711e5 96static inline double ltt_time_to_double(LttTime t1)
97{
8aee234c 98 /* We lose precision if tv_sec is > than (2^23)-1
99 *
100 * Max values that fits in a double (53 bits precision on normalised
101 * mantissa):
102 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
103 *
104 * So we have 53-30 = 23 bits left for tv_sec.
105 * */
c74e0cf9 106#ifdef EXTRA_CHECK
0c5dbe3b 107 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
8aee234c 108 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
109 g_warning("Precision loss in conversion LttTime to double");
c74e0cf9 110#endif //EXTRA_CHECK
62b45a6e 111 return ((double)((guint64)t1.tv_sec<<DOUBLE_SHIFT)
112 / (double)DOUBLE_SHIFT_CONST)
113 + (double)t1.tv_nsec;
308711e5 114}
115
116
117static inline LttTime ltt_time_from_double(double t1)
118{
8aee234c 119 /* We lose precision if tv_sec is > than (2^23)-1
120 *
121 * Max values that fits in a double (53 bits precision on normalised
122 * mantissa):
123 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
124 *
125 * So we have 53-30 = 23 bits left for tv_sec.
126 * */
c74e0cf9 127#ifdef EXTRA_CHECK
0c5dbe3b 128 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
8aee234c 129 if(t1 > MAX_TV_SEC_TO_DOUBLE)
130 g_warning("Conversion from non precise double to LttTime");
c74e0cf9 131#endif //EXTRA_CHECK
308711e5 132 LttTime res;
0ce58d10 133 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
62b45a6e 134 res.tv_sec = (guint64)(t1 * DOUBLE_SHIFT_CONST) >> DOUBLE_SHIFT;
135 res.tv_nsec = (t1 - (((guint64)res.tv_sec<<LTT_TIME_UINT_SHIFT))
136 * LTT_TIME_UINT_SHIFT_CONST);
308711e5 137 return res;
138}
139
8d1e6362 140/* Use ltt_time_to_double and ltt_time_from_double to check for lack
141 * of precision.
142 */
143static inline LttTime ltt_time_mul(LttTime t1, double d)
144{
145 LttTime res;
146
147 double time_double = ltt_time_to_double(t1);
148
149 time_double = time_double * d;
150
151 res = ltt_time_from_double(time_double);
152
153 return res;
154
155#if 0
156 /* What is that ? (Mathieu) */
157 if(f == 0.0){
158 res.tv_sec = 0;
159 res.tv_nsec = 0;
160 }else{
161 double d;
162 d = 1.0/f;
163 sec = t1.tv_sec / (double)d;
164 res.tv_sec = sec;
165 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
166 NANOSECONDS_PER_SECOND;
167 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
168 res.tv_nsec %= NANOSECONDS_PER_SECOND;
169 }
170 return res;
171#endif //0
172}
173
174
175/* Use ltt_time_to_double and ltt_time_from_double to check for lack
176 * of precision.
177 */
178static inline LttTime ltt_time_div(LttTime t1, double d)
179{
180 LttTime res;
181
182 double time_double = ltt_time_to_double(t1);
183
184 time_double = time_double / d;
185
186 res = ltt_time_from_double(time_double);
187
188 return res;
189
190
191#if 0
192 double sec;
193 LttTime res;
194
195 sec = t1.tv_sec / (double)f;
196 res.tv_sec = sec;
197 res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) *
198 NANOSECONDS_PER_SECOND;
199 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
200 res.tv_nsec %= NANOSECONDS_PER_SECOND;
201 return res;
202#endif //0
203}
204
62b45a6e 205
90ef7e4a 206static inline guint64 ltt_time_to_uint64(LttTime t1)
207{
57df94dd 208 return (guint64)((t1.tv_sec*LTT_TIME_UINT_SHIFT_CONST) >> LTT_TIME_UINT_SHIFT)
62b45a6e 209 + (guint64)t1.tv_nsec;
90ef7e4a 210}
211
212
213#define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
62b45a6e 214
215/* The likely branch is with sec != 0, because most events in a bloc
216 * will be over 1s from the block start. (see tracefile.c)
217 */
90ef7e4a 218static inline LttTime ltt_time_from_uint64(guint64 t1)
219{
220 /* We lose precision if tv_sec is > than (2^62)-1
221 * */
c74e0cf9 222#ifdef EXTRA_CHECK
90ef7e4a 223 g_assert(t1 <= MAX_TV_SEC_TO_UINT64);
224 if(t1 > MAX_TV_SEC_TO_UINT64)
49f3c39e 225 g_warning("Conversion from uint64 to non precise LttTime");
c74e0cf9 226#endif //EXTRA_CHECK
90ef7e4a 227 LttTime res;
62b45a6e 228 //if(unlikely(t1 >= NANOSECONDS_PER_SECOND)) {
229 if(likely(t1>>LTT_TIME_UINT_SHIFT >= LTT_TIME_UINT_SHIFT_CONST)) {
230 //res.tv_sec = t1/NANOSECONDS_PER_SECOND;
231 res.tv_sec = (t1>>LTT_TIME_UINT_SHIFT)
232 /LTT_TIME_UINT_SHIFT_CONST; // acceleration
49f3c39e 233 res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND);
234 } else {
235 res.tv_sec = 0;
236 res.tv_nsec = (guint32)t1;
237 }
90ef7e4a 238 return res;
239}
8d1e6362 240
308711e5 241#endif // LTT_TIME_H
This page took 0.036312 seconds and 4 git commands to generate.