1 /* xtime -- extended-resolution integer time stamps
3 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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 2, or (at your option)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Paul Eggert. */
24 /* xtime_t is a signed type used for time stamps. It is an integer
25 type that is a count of nanoseconds -- except for obsolescent hosts
26 without sufficiently-wide integers, where it is a count of
29 typedef long long int xtime_t;
30 # define XTIME_PRECISION 1000000000
33 typedef long int xtime_t;
34 # if LONG_MAX >> 31 >> 31 == 0
35 # define XTIME_PRECISION 1
37 # define XTIME_PRECISION 1000000000
41 /* Return an extended time value that contains S seconds and NS
42 nanoseconds, without any overflow checking. */
44 xtime_make (xtime_t s, long int ns)
46 if (XTIME_PRECISION == 1)
49 return XTIME_PRECISION * s + ns;
52 /* Return the number of seconds in T, which must be nonnegative. */
54 xtime_nonnegative_sec (xtime_t t)
56 return t / XTIME_PRECISION;
59 /* Return the number of seconds in T. */
63 return (XTIME_PRECISION == 1
66 ? (t + XTIME_PRECISION - 1) / XTIME_PRECISION - 1
67 : xtime_nonnegative_sec (t));
70 /* Return the number of nanoseconds in T, which must be nonnegative. */
71 static inline long int
72 xtime_nonnegative_nsec (xtime_t t)
74 return t % XTIME_PRECISION;
77 /* Return the number of nanoseconds in T. */
78 static inline long int
79 xtime_nsec (xtime_t t)
81 long int ns = t % XTIME_PRECISION;
83 ns += XTIME_PRECISION;