1 /* xtime -- extended-resolution integer timestamps 2 3 Copyright (C) 2005-2006, 2009-2021 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 <https://www.gnu.org/licenses/>. */ 17 18 /* Written by Paul Eggert. */ 19 20 #ifndef XTIME_H_ 21 #define XTIME_H_ 1 22 23 #ifndef _GL_INLINE_HEADER_BEGIN 24 #error "Please include config.h first." 25 #endif 26 _GL_INLINE_HEADER_BEGIN 27 #ifndef XTIME_INLINE 28 # define XTIME_INLINE _GL_INLINE 29 #endif 30 31 /* xtime_t is a signed type used for timestamps. It is an integer 32 type that is a count of nanoseconds. */ 33 typedef long long int xtime_t; 34 #define XTIME_PRECISION 1000000000 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 /* Return an extended time value that contains S seconds and NS 41 nanoseconds. S and NS should be nonnegative; otherwise, integer 42 overflow can occur even if the result is in range. */ 43 XTIME_INLINE xtime_t 44 xtime_make (xtime_t s, long int ns) /* */ 45 { 46 return XTIME_PRECISION * s + ns; 47 } 48 49 /* The following functions split an extended time value: 50 T = XTIME_PRECISION * xtime_sec (T) + xtime_nsec (T) 51 with 0 <= xtime_nsec (T) < XTIME_PRECISION. */ 52 53 /* Return the number of seconds in T, which must be nonnegative. */ 54 XTIME_INLINE xtime_t 55 xtime_nonnegative_sec (xtime_t t) /* */ 56 { 57 return t / XTIME_PRECISION; 58 } 59 60 /* Return the number of seconds in T. */ 61 XTIME_INLINE xtime_t 62 xtime_sec (xtime_t t) /* */ 63 { 64 return (t + (t < 0)) / XTIME_PRECISION - (t < 0); 65 } 66 67 /* Return the number of nanoseconds in T, which must be nonnegative. */ 68 XTIME_INLINE long int 69 xtime_nonnegative_nsec (xtime_t t) /* */ 70 { 71 return t % XTIME_PRECISION; 72 } 73 74 /* Return the number of nanoseconds in T. */ 75 XTIME_INLINE long int 76 xtime_nsec (xtime_t t) /* */ 77 { 78 long int ns = t % XTIME_PRECISION; 79 if (ns < 0) 80 ns += XTIME_PRECISION; 81 return ns; 82 } 83 84 #ifdef __cplusplus 85 } 86 #endif 87 88 #endif