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