1/* High precision, low overhead timing functions. x86 version.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _HP_TIMING_H
20#define _HP_TIMING_H 1
21
22#include <isa.h>
23
24#if MINIMUM_ISA == 686 || MINIMUM_ISA == 8664
25/* We indeed have inlined functions. */
26# define HP_TIMING_INLINE (1)
27
28/* We use 64bit values for the times. */
29typedef unsigned long long int hp_timing_t;
30
31/* That's quite simple. Use the `rdtsc' instruction. Note that the value
32 might not be 100% accurate since there might be some more instructions
33 running in this moment. This could be changed by using a barrier like
34 'cpuid' right before the `rdtsc' instruciton. But we are not interested
35 in accurate clock cycles here so we don't do this.
36
37 NB: Use __builtin_ia32_rdtsc directly since including <x86intrin.h>
38 makes building glibc very slow. */
39# ifdef USE_RDTSCP
40/* RDTSCP waits until all previous instructions have executed and all
41 previous loads are globally visible before reading the counter.
42 RDTSC doesn't wait until all previous instructions have been executed
43 before reading the counter. */
44# define HP_TIMING_NOW(Var) \
45 (__extension__ ({ \
46 unsigned int __aux; \
47 (Var) = __builtin_ia32_rdtscp (&__aux); \
48 }))
49# else
50# define HP_TIMING_NOW(Var) ((Var) = __builtin_ia32_rdtsc ())
51# endif
52
53# include <hp-timing-common.h>
54#else
55/* NB: Undefine _HP_TIMING_H so that <sysdeps/generic/hp-timing.h> will
56 be included. */
57# undef _HP_TIMING_H
58# include <sysdeps/generic/hp-timing.h>
59#endif
60
61#endif /* hp-timing.h */
62

source code of glibc/sysdeps/x86/hp-timing.h