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