xref: /freebsd/sys/contrib/zstd/programs/timefn.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
12b9c00cbSConrad Meyer /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
32b9c00cbSConrad Meyer  * All rights reserved.
42b9c00cbSConrad Meyer  *
52b9c00cbSConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
62b9c00cbSConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
72b9c00cbSConrad Meyer  * in the COPYING file in the root directory of this source tree).
82b9c00cbSConrad Meyer  * You may select, at your option, one of the above-listed licenses.
92b9c00cbSConrad Meyer  */
102b9c00cbSConrad Meyer 
112b9c00cbSConrad Meyer #ifndef TIME_FN_H_MODULE_287987
122b9c00cbSConrad Meyer #define TIME_FN_H_MODULE_287987
132b9c00cbSConrad Meyer 
142b9c00cbSConrad Meyer #if defined (__cplusplus)
152b9c00cbSConrad Meyer extern "C" {
162b9c00cbSConrad Meyer #endif
172b9c00cbSConrad Meyer 
182b9c00cbSConrad Meyer 
192b9c00cbSConrad Meyer /*-****************************************
202b9c00cbSConrad Meyer *  Dependencies
212b9c00cbSConrad Meyer ******************************************/
222b9c00cbSConrad Meyer #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC */
232b9c00cbSConrad Meyer 
242b9c00cbSConrad Meyer 
252b9c00cbSConrad Meyer 
262b9c00cbSConrad Meyer /*-****************************************
272b9c00cbSConrad Meyer *  Local Types
282b9c00cbSConrad Meyer ******************************************/
292b9c00cbSConrad Meyer 
302b9c00cbSConrad Meyer #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
31f7cd7fe5SConrad Meyer # if defined(_AIX)
32f7cd7fe5SConrad Meyer #  include <inttypes.h>
33f7cd7fe5SConrad Meyer # else
34f7cd7fe5SConrad Meyer #  include <stdint.h> /* intptr_t */
35f7cd7fe5SConrad Meyer # endif
362b9c00cbSConrad Meyer   typedef uint64_t           PTime;  /* Precise Time */
372b9c00cbSConrad Meyer #else
382b9c00cbSConrad Meyer   typedef unsigned long long PTime;  /* does not support compilers without long long support */
392b9c00cbSConrad Meyer #endif
402b9c00cbSConrad Meyer 
412b9c00cbSConrad Meyer 
422b9c00cbSConrad Meyer 
432b9c00cbSConrad Meyer /*-****************************************
442b9c00cbSConrad Meyer *  Time functions
452b9c00cbSConrad Meyer ******************************************/
462b9c00cbSConrad Meyer #if defined(_WIN32)   /* Windows */
472b9c00cbSConrad Meyer 
4837f1f268SConrad Meyer     #include <windows.h>   /* LARGE_INTEGER */
492b9c00cbSConrad Meyer     typedef LARGE_INTEGER UTIL_time_t;
502b9c00cbSConrad Meyer     #define UTIL_TIME_INITIALIZER { { 0, 0 } }
512b9c00cbSConrad Meyer 
522b9c00cbSConrad Meyer #elif defined(__APPLE__) && defined(__MACH__)
532b9c00cbSConrad Meyer 
542b9c00cbSConrad Meyer     #include <mach/mach_time.h>
552b9c00cbSConrad Meyer     typedef PTime UTIL_time_t;
562b9c00cbSConrad Meyer     #define UTIL_TIME_INITIALIZER 0
572b9c00cbSConrad Meyer 
5837f1f268SConrad Meyer /* C11 requires timespec_get, but FreeBSD 11 lacks it, while still claiming C11 compliance.
5937f1f268SConrad Meyer    Android also lacks it but does define TIME_UTC. */
602b9c00cbSConrad Meyer #elif (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */) \
6137f1f268SConrad Meyer     && defined(TIME_UTC) && !defined(__ANDROID__)
622b9c00cbSConrad Meyer 
632b9c00cbSConrad Meyer     typedef struct timespec UTIL_time_t;
642b9c00cbSConrad Meyer     #define UTIL_TIME_INITIALIZER { 0, 0 }
652b9c00cbSConrad Meyer 
662b9c00cbSConrad Meyer #else   /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
672b9c00cbSConrad Meyer 
682b9c00cbSConrad Meyer     typedef clock_t UTIL_time_t;
692b9c00cbSConrad Meyer     #define UTIL_TIME_INITIALIZER 0
702b9c00cbSConrad Meyer 
712b9c00cbSConrad Meyer #endif
722b9c00cbSConrad Meyer 
732b9c00cbSConrad Meyer 
742b9c00cbSConrad Meyer UTIL_time_t UTIL_getTime(void);
752b9c00cbSConrad Meyer PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
762b9c00cbSConrad Meyer PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
772b9c00cbSConrad Meyer 
782b9c00cbSConrad Meyer #define SEC_TO_MICRO ((PTime)1000000)
792b9c00cbSConrad Meyer PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
802b9c00cbSConrad Meyer PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
812b9c00cbSConrad Meyer 
822b9c00cbSConrad Meyer void UTIL_waitForNextTick(void);
832b9c00cbSConrad Meyer 
842b9c00cbSConrad Meyer 
852b9c00cbSConrad Meyer #if defined (__cplusplus)
862b9c00cbSConrad Meyer }
872b9c00cbSConrad Meyer #endif
882b9c00cbSConrad Meyer 
892b9c00cbSConrad Meyer #endif /* TIME_FN_H_MODULE_287987 */
90