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