Line data Source code
1 : /* xtime -- extended-resolution integer time stamps
2 :
3 : Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 :
5 : This program is free software: you can redistribute it and/or modify
6 : it under the terms of the GNU General Public License as published by
7 : the Free Software Foundation; either version 3 of the License, or
8 : (at your option) any later version.
9 :
10 : This program is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public License
16 : along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 :
18 : /* Written by Paul Eggert. */
19 :
20 : #ifndef XTIME_H_
21 : # define XTIME_H_ 1
22 :
23 : /* xtime_t is a signed type used for time stamps. It is an integer
24 : type that is a count of nanoseconds -- except for obsolescent hosts
25 : without sufficiently-wide integers, where it is a count of
26 : seconds. */
27 : # if HAVE_LONG_LONG_INT
28 : typedef long long int xtime_t;
29 : # define XTIME_PRECISION 1000000000
30 : # else
31 : # include <limits.h>
32 : typedef long int xtime_t;
33 : # if LONG_MAX >> 31 >> 31 == 0
34 : # define XTIME_PRECISION 1
35 : # else
36 : # define XTIME_PRECISION 1000000000
37 : # endif
38 : # endif
39 :
40 : /* Return an extended time value that contains S seconds and NS
41 : nanoseconds, without any overflow checking. */
42 : static inline xtime_t
43 22 : xtime_make (xtime_t s, long int ns)
44 : {
45 : if (XTIME_PRECISION == 1)
46 : return s;
47 : else
48 22 : return XTIME_PRECISION * s + ns;
49 : }
50 :
51 : /* Return the number of seconds in T, which must be nonnegative. */
52 : static inline xtime_t
53 : xtime_nonnegative_sec (xtime_t t)
54 : {
55 : return t / XTIME_PRECISION;
56 : }
57 :
58 : /* Return the number of seconds in T. */
59 : static inline xtime_t
60 : xtime_sec (xtime_t t)
61 : {
62 : return (XTIME_PRECISION == 1
63 : ? t
64 : : t < 0
65 : ? (t + XTIME_PRECISION - 1) / XTIME_PRECISION - 1
66 : : xtime_nonnegative_sec (t));
67 : }
68 :
69 : /* Return the number of nanoseconds in T, which must be nonnegative. */
70 : static inline long int
71 : xtime_nonnegative_nsec (xtime_t t)
72 : {
73 : return t % XTIME_PRECISION;
74 : }
75 :
76 : /* Return the number of nanoseconds in T. */
77 : static inline long int
78 : xtime_nsec (xtime_t t)
79 : {
80 : long int ns = t % XTIME_PRECISION;
81 : if (ns < 0)
82 : ns += XTIME_PRECISION;
83 : return ns;
84 : }
85 :
86 : #endif
|