fixed scroll up
[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>
23
308711e5 24
25typedef struct _LttTime {
26 unsigned long tv_sec;
27 unsigned long tv_nsec;
28} LttTime;
29
30
31static const unsigned long NANOSECONDS_PER_SECOND = 1000000000;
32
33static const LttTime ltt_time_zero = { 0, 0};
34
18206708 35static const LttTime ltt_time_one = { 0, 1 };
36
a00149f6 37static const LttTime ltt_time_infinite = { G_MAXUINT, G_MAXUINT };
308711e5 38
39static 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
54static 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
67static inline LttTime ltt_time_mul(LttTime t1, float f)
68{
308711e5 69 LttTime res;
e17c04d2 70 float d;
1f1ae829 71 double sec;
308711e5 72
1f1ae829 73 if(f == 0.0){
74 res.tv_sec = 0;
75 res.tv_nsec = 0;
76 }else{
e17c04d2 77 d = 1.0/f;
1f1ae829 78 sec = t1.tv_sec / (double)d;
79 res.tv_sec = sec;
80 res.tv_nsec = t1.tv_nsec / (double)d + (sec - res.tv_sec) *
81 NANOSECONDS_PER_SECOND;
82 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
83 res.tv_nsec %= NANOSECONDS_PER_SECOND;
84 }
308711e5 85 return res;
86}
87
88
89static inline LttTime ltt_time_div(LttTime t1, float f)
90{
91 double sec;
92 LttTime res;
93
94 sec = t1.tv_sec / (double)f;
95 res.tv_sec = sec;
96 res.tv_nsec = t1.tv_nsec / (double)f + (sec - res.tv_sec) *
97 NANOSECONDS_PER_SECOND;
1f1ae829 98 res.tv_sec += res.tv_nsec / NANOSECONDS_PER_SECOND;
99 res.tv_nsec %= NANOSECONDS_PER_SECOND;
308711e5 100 return res;
101}
102
103
104static inline int ltt_time_compare(LttTime t1, LttTime t2)
105{
106 if(t1.tv_sec > t2.tv_sec) return 1;
107 if(t1.tv_sec < t2.tv_sec) return -1;
108 if(t1.tv_nsec > t2.tv_nsec) return 1;
109 if(t1.tv_nsec < t2.tv_nsec) return -1;
110 return 0;
111}
112
8aee234c 113#define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
308711e5 114static inline double ltt_time_to_double(LttTime t1)
115{
8aee234c 116 /* We lose precision if tv_sec is > than (2^23)-1
117 *
118 * Max values that fits in a double (53 bits precision on normalised
119 * mantissa):
120 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
121 *
122 * So we have 53-30 = 23 bits left for tv_sec.
123 * */
0c5dbe3b 124 g_assert(t1.tv_sec <= MAX_TV_SEC_TO_DOUBLE);
8aee234c 125 if(t1.tv_sec > MAX_TV_SEC_TO_DOUBLE)
126 g_warning("Precision loss in conversion LttTime to double");
308711e5 127 return (double)t1.tv_sec + (double)t1.tv_nsec / NANOSECONDS_PER_SECOND;
128}
129
130
131static inline LttTime ltt_time_from_double(double t1)
132{
8aee234c 133 /* We lose precision if tv_sec is > than (2^23)-1
134 *
135 * Max values that fits in a double (53 bits precision on normalised
136 * mantissa):
137 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
138 *
139 * So we have 53-30 = 23 bits left for tv_sec.
140 * */
0c5dbe3b 141 g_assert(t1 <= MAX_TV_SEC_TO_DOUBLE);
8aee234c 142 if(t1 > MAX_TV_SEC_TO_DOUBLE)
143 g_warning("Conversion from non precise double to LttTime");
308711e5 144 LttTime res;
145 res.tv_sec = t1;
146 res.tv_nsec = (t1 - res.tv_sec) * NANOSECONDS_PER_SECOND;
147 return res;
148}
149
150#endif // LTT_TIME_H
This page took 0.029991 seconds and 4 git commands to generate.