likely/unlikely branch prediction
[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 #include <ltt/compiler.h>
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 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
46 * higher probability of low value for t2.tv_sec, we will habitually
47 * not wrap.
48 */
49 if(unlikely(t1.tv_nsec < t2.tv_nsec)) {
50 res.tv_sec--;
51 res.tv_nsec += NANOSECONDS_PER_SECOND;
52 }
53 return res;
54 }
55
56
57 static inline LttTime ltt_time_add(LttTime t1, LttTime t2)
58 {
59 LttTime res;
60 res.tv_nsec = t1.tv_nsec + t2.tv_nsec;
61 res.tv_sec = t1.tv_sec + t2.tv_sec;
62 /* unlikely : given equal chance to be anywhere in t1.tv_nsec, and
63 * higher probability of low value for t2.tv_sec, we will habitually
64 * not wrap.
65 */
66 if(unlikely(res.tv_nsec >= NANOSECONDS_PER_SECOND)) {
67 res.tv_sec++;
68 res.tv_nsec -= NANOSECONDS_PER_SECOND;
69 }
70 return res;
71 }
72
73 /* Fastest comparison : t1 > t2 */
74 static inline int ltt_time_compare(LttTime t1, LttTime t2)
75 {
76 int ret=0;
77 if(likely(t1.tv_sec > t2.tv_sec)) ret = 1;
78 else if(unlikely(t1.tv_sec < t2.tv_sec)) ret = -1;
79 else if(likely(t1.tv_nsec > t2.tv_nsec)) ret = 1;
80 else if(unlikely(t1.tv_nsec < t2.tv_nsec)) ret = -1;
81
82 return ret;
83 }
84
85 #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b))
86 #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b))
87
88 #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
89 static inline double ltt_time_to_double(LttTime t1)
90 {
91 /* We lose precision if tv_sec is > than (2^23)-1
92 *
93 * Max values that fits in a double (53 bits precision on normalised
94 * mantissa):
95 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
96 *
97 * So we have 53-30 = 23 bits left for tv_sec.
98 * */
99 #ifdef EXTRA_CHECK
100 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
101 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
102 g_warning("Precision loss in conversion LttTime to double");
103 #endif //EXTRA_CHECK
104 return ((double)t1.tv_sec * (double)NANOSECONDS_PER_SECOND) + (double)t1.tv_nsec;
105 }
106
107
108 static inline LttTime ltt_time_from_double(double t1)
109 {
110 /* We lose precision if tv_sec is > than (2^23)-1
111 *
112 * Max values that fits in a double (53 bits precision on normalised
113 * mantissa):
114 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
115 *
116 * So we have 53-30 = 23 bits left for tv_sec.
117 * */
118 #ifdef EXTRA_CHECK
119 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
120 if(t1 > MAX_TV_SEC_TO_DOUBLE)
121 g_warning("Conversion from non precise double to LttTime");
122 #endif //EXTRA_CHECK
123 LttTime res;
124 //res.tv_sec = t1/(double)NANOSECONDS_PER_SECOND;
125 res.tv_sec = (guint64)(t1 * SHIFT_CONST) >> 30;
126 res.tv_nsec = (t1 - (res.tv_sec*NANOSECONDS_PER_SECOND));
127 return res;
128 }
129
130 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
131 * of precision.
132 */
133 static inline LttTime ltt_time_mul(LttTime t1, double d)
134 {
135 LttTime res;
136
137 double time_double = ltt_time_to_double(t1);
138
139 time_double = time_double * d;
140
141 res = ltt_time_from_double(time_double);
142
143 return res;
144
145 #if 0
146 /* What is that ? (Mathieu) */
147 if(f == 0.0){
148 res.tv_sec = 0;
149 res.tv_nsec = 0;
150 }else{
151 double d;
152 d = 1.0/f;
153 sec = t1.tv_sec / (double)d;
154 res.tv_sec = sec;
155 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
156 NANOSECONDS_PER_SECOND;
157 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
158 res.tv_nsec %= NANOSECONDS_PER_SECOND;
159 }
160 return res;
161 #endif //0
162 }
163
164
165 /* Use ltt_time_to_double and ltt_time_from_double to check for lack
166 * of precision.
167 */
168 static inline LttTime ltt_time_div(LttTime t1, double d)
169 {
170 LttTime res;
171
172 double time_double = ltt_time_to_double(t1);
173
174 time_double = time_double / d;
175
176 res = ltt_time_from_double(time_double);
177
178 return res;
179
180
181 #if 0
182 double sec;
183 LttTime res;
184
185 sec = t1.tv_sec / (double)f;
186 res.tv_sec = sec;
187 res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) *
188 NANOSECONDS_PER_SECOND;
189 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
190 res.tv_nsec %= NANOSECONDS_PER_SECOND;
191 return res;
192 #endif //0
193 }
194
195 static inline guint64 ltt_time_to_uint64(LttTime t1)
196 {
197 return (guint64)t1.tv_sec*NANOSECONDS_PER_SECOND
198 + (guint64)t1.tv_nsec;
199 }
200
201
202 #define MAX_TV_SEC_TO_UINT64 0x3FFFFFFFFFFFFFFFULL
203 static inline LttTime ltt_time_from_uint64(guint64 t1)
204 {
205 /* We lose precision if tv_sec is > than (2^62)-1
206 * */
207 #ifdef EXTRA_CHECK
208 g_assert(t1 <= MAX_TV_SEC_TO_UINT64);
209 if(t1 > MAX_TV_SEC_TO_UINT64)
210 g_warning("Conversion from non precise uint64 to LttTime");
211 #endif //EXTRA_CHECK
212 LttTime res;
213 res.tv_sec = t1/NANOSECONDS_PER_SECOND;
214 res.tv_nsec = (t1 - res.tv_sec*NANOSECONDS_PER_SECOND);
215 return res;
216 }
217
218 #endif // LTT_TIME_H
This page took 0.032839 seconds and 4 git commands to generate.