1 /* This file is part of the Linux Trace Toolkit trace reading library
2 * Copyright (C) 2003-2004 Michel Dagenais
3 * 2005 Mathieu Desnoyers
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.
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.
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.
24 #include <ltt/compiler.h>
27 typedef struct _LttTime
{
29 unsigned long tv_nsec
;
33 #define NANOSECONDS_PER_SECOND 1000000000
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 */
37 /* 2^30/1.07374182400631629848 = 1000000000.0 */
38 #define DOUBLE_SHIFT_CONST_DIV 1.07374182400631629848
39 #define DOUBLE_SHIFT 30
41 /* 2^30*0.93132257461547851562 = 1000000000.0000000000 */
42 #define DOUBLE_SHIFT_CONST_MUL 0.93132257461547851562
45 /* 1953125 * 2^9 = NANOSECONDS_PER_SECOND */
46 #define LTT_TIME_UINT_SHIFT_CONST 1953125
47 #define LTT_TIME_UINT_SHIFT 9
50 static const LttTime ltt_time_zero
= { 0, 0 };
52 static const LttTime ltt_time_one
= { 0, 1 };
54 static const LttTime ltt_time_infinite
= { G_MAXUINT
, NANOSECONDS_PER_SECOND
};
56 static inline LttTime
ltt_time_sub(LttTime t1
, LttTime t2
)
59 res
.tv_sec
= t1
.tv_sec
- t2
.tv_sec
;
60 res
.tv_nsec
= t1
.tv_nsec
- t2
.tv_nsec
;
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
65 if(unlikely(t1
.tv_nsec
< t2
.tv_nsec
)) {
67 res
.tv_nsec
+= NANOSECONDS_PER_SECOND
;
73 static inline LttTime
ltt_time_add(LttTime t1
, LttTime t2
)
76 res
.tv_nsec
= t1
.tv_nsec
+ t2
.tv_nsec
;
77 res
.tv_sec
= t1
.tv_sec
+ t2
.tv_sec
;
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
82 if(unlikely(res
.tv_nsec
>= NANOSECONDS_PER_SECOND
)) {
84 res
.tv_nsec
-= NANOSECONDS_PER_SECOND
;
93 /* Fastest comparison : t1 > t2 */
94 static inline int ltt_time_compare(LttTime t1
, LttTime t2
)
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;
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))
112 #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
113 static inline double ltt_time_to_double(LttTime t1
)
115 /* We lose precision if tv_sec is > than (2^23)-1
117 * Max values that fits in a double (53 bits precision on normalised
119 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
121 * So we have 53-30 = 23 bits left for tv_sec.
124 g_assert(t1
.tv_sec
<= MAX_TV_SEC_TO_DOUBLE
);
125 if(t1
.tv_sec
> MAX_TV_SEC_TO_DOUBLE
)
126 g_warning("Precision loss in conversion LttTime to double");
128 return ((double)((guint64
)t1
.tv_sec
<<DOUBLE_SHIFT
)
129 * (double)DOUBLE_SHIFT_CONST_MUL
)
130 + (double)t1
.tv_nsec
;
134 static inline LttTime
ltt_time_from_double(double t1
)
136 /* We lose precision if tv_sec is > than (2^23)-1
138 * Max values that fits in a double (53 bits precision on normalised
140 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
142 * So we have 53-30 = 23 bits left for tv_sec.
145 g_assert(t1
<= MAX_TV_SEC_TO_DOUBLE
);
146 if(t1
> MAX_TV_SEC_TO_DOUBLE
)
147 g_warning("Conversion from non precise double to LttTime");
150 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
151 res
.tv_sec
= (guint64
)(t1
* DOUBLE_SHIFT_CONST_DIV
) >> DOUBLE_SHIFT
;
152 res
.tv_nsec
= (t1
- (((guint64
)res
.tv_sec
<<LTT_TIME_UINT_SHIFT
))
153 * LTT_TIME_UINT_SHIFT_CONST
);
157 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
160 static inline LttTime
ltt_time_mul(LttTime t1
, double d
)
164 double time_double
= ltt_time_to_double(t1
);
166 time_double
= time_double
* d
;
168 res
= ltt_time_from_double(time_double
);
173 /* What is that ? (Mathieu) */
180 sec
= t1
.tv_sec
/ (double)d
;
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
;
192 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
195 static inline LttTime
ltt_time_div(LttTime t1
, double d
)
199 double time_double
= ltt_time_to_double(t1
);
201 time_double
= time_double
/ d
;
203 res
= ltt_time_from_double(time_double
);
212 sec
= t1
.tv_sec
/ (double)f
;
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
;
223 static inline guint64
ltt_time_to_uint64(LttTime t1
)
225 return (((guint64
)t1
.tv_sec
*LTT_TIME_UINT_SHIFT_CONST
) << LTT_TIME_UINT_SHIFT
)
226 + (guint64
)t1
.tv_nsec
;
230 #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
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)
235 static inline LttTime
ltt_time_from_uint64(guint64 t1
)
237 /* We lose precision if tv_sec is > than (2^62)-1
240 g_assert(t1
<= MAX_TV_SEC_TO_UINT64
);
241 if(t1
> MAX_TV_SEC_TO_UINT64
)
242 g_warning("Conversion from uint64 to non precise LttTime");
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
250 res
.tv_nsec
= (t1
- res
.tv_sec
*NANOSECONDS_PER_SECOND
);
253 res
.tv_nsec
= (guint32
)t1
;