add linkly and unlikely optimisation to ltt_time_compare
[lttv.git] / ltt / branches / poly / ltt / time.h
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
19 #ifndef LTT_TIME_H
20 #define LTT_TIME_H
21
22 #include <glib.h>
23
24
25 typedef struct _LttTime {
26 unsigned long tv_sec;
27 unsigned long tv_nsec;
28 } LttTime;
29
30
31 #define NANOSECONDS_PER_SECOND 1000000000
32 #define SHIFT_CONST 1.07374182400631629848
33
34 static const LttTime ltt_time_zero = { 0, 0 };
35
36 static const LttTime ltt_time_one = { 0, 1 };
37
38 static const LttTime ltt_time_infinite = { G_MAXUINT, NANOSECONDS_PER_SECOND };
39
40 static inline LttTime ltt_time_sub(LttTime t1, LttTime t2)
41 {
42 LttTime res;
43 res.tv_sec = t1.tv_sec - t2.tv_sec;
44 res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
45 if(t1.tv_nsec < t2.tv_nsec) {
46 res.tv_sec--;
47 res.tv_nsec += NANOSECONDS_PER_SECOND;
48 }
49 return res;
50 }
51
52
53 static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
54 {
55 LttTime res;
56 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
57 res.tv_sec = t1.tv_sec + t2.tv_sec;
58 if(res.tv_nsec >= NANOSECONDS_PER_SECOND) {
59 res.tv_sec++;
60 res.tv_nsec -= NANOSECONDS_PER_SECOND;
61 }
62 return res;
63 }
64
65 #define likely(x) __builtin_expect(!!(x), 1)
66 #define unlikely(x) __builtin_expect(!!(x), 0)
67
68 /* Fastest comparison : t1 > t2 */
69 static inline int ltt_time_compare(LttTime t1, LttTime t2)
70 {
71 int ret=0;
72 if(likely(t1.tv_sec > t2.tv_sec)) ret = 1;
73 else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1;
74 else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1;
75 else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1;
76
77 return ret;
78 }
79
80 #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b))
81 #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b))
82
83 #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
84 static inline double ltt_time_to_double(LttTime t1)
85 {
86 /* We lose precision if tv_sec is > than (2^23)-1
87 *
88 * Max values that fits in a double (53 bits precision on normalised
89 * mantissa):
90 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
91 *
92 * So we have 53-30 = 23 bits left for tv_sec.
93 * */
94 #ifdef EXTRA_CHECK
95 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
96 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
97 g_warning("Precision loss in conversion LttTime to double");
98 #endif //EXTRA_CHECK
99 return ((double)t1.tv_sec * (double)NANOSECONDS_PER_SECOND) + (double)t1.tv_nsec;
100 }
101
102
103 static inline LttTime ltt_time_from_double(double t1)
104 {
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 * */
113 #ifdef EXTRA_CHECK
114 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
115 if(t1 > MAX_TV_SEC_TO_DOUBLE)
116 g_warning("Conversion from non precise double to LttTime");
117 #endif //EXTRA_CHECK
118 LttTime res;
119 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
120 res.tv_sec = (guint64)(t1 * SHIFT_CONST) >> 30;
121 res.tv_nsec = (t1 - (res.tv_sec*NANOSECONDS_PER_SECOND));
122 return res;
123 }
124
125 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
126 * of precision.
127 */
128 static inline LttTime ltt_time_mul(LttTime t1, double d)
129 {
130 LttTime res;
131
132 double time_double = ltt_time_to_double(t1);
133
134 time_double = time_double * d;
135
136 res = ltt_time_from_double(time_double);
137
138 return res;
139
140 #if 0
141 /* What is that ? (Mathieu) */
142 if(f == 0.0){
143 res.tv_sec = 0;
144 res.tv_nsec = 0;
145 }else{
146 double d;
147 d = 1.0/f;
148 sec = t1.tv_sec / (double)d;
149 res.tv_sec = sec;
150 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
151 NANOSECONDS_PER_SECOND;
152 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
153 res.tv_nsec %= NANOSECONDS_PER_SECOND;
154 }
155 return res;
156 #endif //0
157 }
158
159
160 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
161 * of precision.
162 */
163 static inline LttTime ltt_time_div(LttTime t1, double d)
164 {
165 LttTime res;
166
167 double time_double = ltt_time_to_double(t1);
168
169 time_double = time_double / d;
170
171 res = ltt_time_from_double(time_double);
172
173 return res;
174
175
176 #if 0
177 double sec;
178 LttTime res;
179
180 sec = t1.tv_sec / (double)f;
181 res.tv_sec = sec;
182 res.tv_nsec = t1.tv_nsec / (double)f + (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 return res;
187 #endif //0
188 }
189
190 static inline guint64 ltt_time_to_uint64(LttTime t1)
191 {
192 return (guint64)t1.tv_sec*NANOSECONDS_PER_SECOND
193 + (guint64)t1.tv_nsec;
194 }
195
196
197 #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
198 static inline LttTime ltt_time_from_uint64(guint64 t1)
199 {
200 /* We lose precision if tv_sec is > than (2^62)-1
201 * */
202 #ifdef EXTRA_CHECK
203 g_assert(t1 <= MAX_TV_SEC_TO_UINT64);
204 if(t1 > MAX_TV_SEC_TO_UINT64)
205 g_warning("Conversion from non precise uint64 to LttTime");
206 #endif //EXTRA_CHECK
207 LttTime res;
208 res.tv_sec = t1/NANOSECONDS_PER_SECOND;
209 res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND);
210 return res;
211 }
212
213 #endif // LTT_TIME_H
This page took 0.03446 seconds and 5 git commands to generate.