1 /* This file is part of the Linux Trace Toolkit trace reading library
2 * Copyright (C) 2003-2004 Michel Dagenais
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.
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.
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.
25 typedef struct _LttTime
{
27 unsigned long tv_nsec
;
31 #define NANOSECONDS_PER_SECOND 1000000000
33 static const LttTime ltt_time_zero
= { 0, 0 };
35 static const LttTime ltt_time_one
= { 0, 1 };
37 static const LttTime ltt_time_infinite
= { G_MAXUINT
, NANOSECONDS_PER_SECOND
};
39 static inline LttTime
ltt_time_sub(LttTime t1
, LttTime t2
)
42 res
.tv_sec
= t1
.tv_sec
- t2
.tv_sec
;
43 if(t1
.tv_nsec
< t2
.tv_nsec
) {
45 res
.tv_nsec
= NANOSECONDS_PER_SECOND
+ t1
.tv_nsec
- t2
.tv_nsec
;
48 res
.tv_nsec
= t1
.tv_nsec
- t2
.tv_nsec
;
54 static inline LttTime
ltt_time_add(LttTime t1
, LttTime t2
)
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
) {
61 res
.tv_nsec
-= NANOSECONDS_PER_SECOND
;
67 static inline LttTime
ltt_time_mul(LttTime t1
, float f
)
78 sec
= t1
.tv_sec
/ (double)d
;
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
;
89 static inline LttTime
ltt_time_div(LttTime t1
, float f
)
94 sec
= t1
.tv_sec
/ (double)f
;
96 res
.tv_nsec
= t1
.tv_nsec
/ (double)f
+ (sec
- res
.tv_sec
) *
97 NANOSECONDS_PER_SECOND
;
98 res
.tv_sec
+= res
.tv_nsec
/ NANOSECONDS_PER_SECOND
;
99 res
.tv_nsec
%= NANOSECONDS_PER_SECOND
;
104 static inline int ltt_time_compare(LttTime t1
, LttTime t2
)
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;
113 #define LTT_TIME_MIN(a,b) ((ltt_time_compare((a),(b)) < 0) ? (a) : (b))
114 #define LTT_TIME_MAX(a,b) ((ltt_time_compare((a),(b)) > 0) ? (a) : (b))
116 #define MAX_TV_SEC_TO_DOUBLE 0x7FFFFF
117 static inline double ltt_time_to_double(LttTime t1
)
119 /* We lose precision if tv_sec is > than (2^23)-1
121 * Max values that fits in a double (53 bits precision on normalised
123 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
125 * So we have 53-30 = 23 bits left for tv_sec.
127 g_assert(t1
.tv_sec
<= MAX_TV_SEC_TO_DOUBLE
);
128 if(t1
.tv_sec
> MAX_TV_SEC_TO_DOUBLE
)
129 g_warning("Precision loss in conversion LttTime to double");
130 return (double)t1
.tv_sec
+ (double)t1
.tv_nsec
/ NANOSECONDS_PER_SECOND
;
134 static inline LttTime
ltt_time_from_double(double t1
)
136 /* We lose precision if tv_sec is > than (2^23)-1
138 * Max values that fits in a double (53 bits precision on normalised
140 * tv_nsec : NANOSECONDS_PER_SECONDS : 2^30
142 * So we have 53-30 = 23 bits left for tv_sec.
144 g_assert(t1
<= MAX_TV_SEC_TO_DOUBLE
);
145 if(t1
> MAX_TV_SEC_TO_DOUBLE
)
146 g_warning("Conversion from non precise double to LttTime");
149 res
.tv_nsec
= (t1
- res
.tv_sec
) * NANOSECONDS_PER_SECOND
;