1 | /* High precision, low overhead timing functions. x86 version. |
2 | Copyright (C) 2018-2019 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 | <http://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 always assume having the timestamp register. */ |
26 | # define HP_TIMING_AVAIL (1) |
27 | # define HP_SMALL_TIMING_AVAIL (1) |
28 | |
29 | /* We indeed have inlined functions. */ |
30 | # define HP_TIMING_INLINE (1) |
31 | |
32 | /* We use 64bit values for the times. */ |
33 | typedef unsigned long long int hp_timing_t; |
34 | |
35 | /* That's quite simple. Use the `rdtsc' instruction. Note that the value |
36 | might not be 100% accurate since there might be some more instructions |
37 | running in this moment. This could be changed by using a barrier like |
38 | 'cpuid' right before the `rdtsc' instruciton. But we are not interested |
39 | in accurate clock cycles here so we don't do this. |
40 | |
41 | NB: Use __builtin_ia32_rdtsc directly since including <x86intrin.h> |
42 | makes building glibc very slow. */ |
43 | # ifdef USE_RDTSCP |
44 | /* RDTSCP waits until all previous instructions have executed and all |
45 | previous loads are globally visible before reading the counter. |
46 | RDTSC doesn't wait until all previous instructions have been executed |
47 | before reading the counter. */ |
48 | # define HP_TIMING_NOW(Var) \ |
49 | (__extension__ ({ \ |
50 | unsigned int __aux; \ |
51 | (Var) = __builtin_ia32_rdtscp (&__aux); \ |
52 | })) |
53 | # else |
54 | # define HP_TIMING_NOW(Var) ((Var) = __builtin_ia32_rdtsc ()) |
55 | # endif |
56 | |
57 | # include <hp-timing-common.h> |
58 | #else |
59 | /* NB: Undefine _HP_TIMING_H so that <sysdeps/generic/hp-timing.h> will |
60 | be included. */ |
61 | # undef _HP_TIMING_H |
62 | # include <sysdeps/generic/hp-timing.h> |
63 | #endif |
64 | |
65 | #endif /* hp-timing.h */ |
66 | |