Removing HP_TIMING_ZERO as unused
[glibc.git] / sysdeps / i386 / i686 / hp-timing.h
blob18e5b95bbea0f3bbb2e068e602af70b0cbd865bc
1 /* High precision, low overhead timing functions. i686 version.
2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _HP_TIMING_H
21 #define _HP_TIMING_H 1
23 #include <string.h>
24 #include <sys/param.h>
25 #include <_itoa.h>
27 /* The macros defined here use the timestamp counter in i586 and up versions
28 of the x86 processors. They provide a very accurate way to measure the
29 time with very little overhead. The time values themself have no real
30 meaning, only differences are interesting.
32 This version is for the i686 processors. The difference to the i586
33 version is that the timerstamp register is unconditionally used. This is
34 not the case for the i586 version where we have to perform runtime test
35 whether the processor really has this capability. We have to make this
36 distinction since the sysdeps/i386/i586 code is supposed to work on all
37 platforms while the i686 already contains i686-specific code.
39 The list of macros we need includes the following:
41 - HP_TIMING_AVAIL: test for availability.
43 - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
44 implemented using function calls but instead uses some inlined code
45 which might simply consist of a few assembler instructions. We have to
46 know this since we might want to use the macros here in places where we
47 cannot make function calls.
49 - hp_timing_t: This is the type for variables used to store the time
50 values.
52 - HP_TIMING_NOW: place timestamp for current time in variable given as
53 parameter.
55 - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
56 HP_TIMING_DIFF macro.
58 - HP_TIMING_DIFF: compute difference between two times and store it
59 in a third. Source and destination might overlap.
61 - HP_TIMING_ACCUM: add time difference to another variable. This might
62 be a bit more complicated to implement for some platforms as the
63 operation should be thread-safe and 64bit arithmetic on 32bit platforms
64 is not.
66 - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
67 there are no threads involved.
69 - HP_TIMING_PRINT: write decimal representation of the timing value into
70 the given string. This operation need not be inline even though
71 HP_TIMING_INLINE is specified.
75 /* We always assume having the timestamp register. */
76 #define HP_TIMING_AVAIL (1)
78 /* We indeed have inlined functions. */
79 #define HP_TIMING_INLINE (1)
81 /* We use 64bit values for the times. */
82 typedef unsigned long long int hp_timing_t;
84 /* That's quite simple. Use the `rdtsc' instruction. Note that the value
85 might not be 100% accurate since there might be some more instructions
86 running in this moment. This could be changed by using a barrier like
87 'cpuid' right before the `rdtsc' instruciton. But we are not interested
88 in accurate clock cycles here so we don't do this. */
89 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
91 /* Use two 'rdtsc' instructions in a row to find out how long it takes. */
92 #define HP_TIMING_DIFF_INIT() \
93 do { \
94 if (GLRO(dl_hp_timing_overhead) == 0) \
95 { \
96 int __cnt = 5; \
97 GLRO(dl_hp_timing_overhead) = ~0ull; \
98 do \
99 { \
100 hp_timing_t __t1, __t2; \
101 HP_TIMING_NOW (__t1); \
102 HP_TIMING_NOW (__t2); \
103 if (__t2 - __t1 < GLRO(dl_hp_timing_overhead)) \
104 GLRO(dl_hp_timing_overhead) = __t2 - __t1; \
106 while (--__cnt > 0); \
108 } while (0)
110 /* It's simple arithmetic for us. */
111 #define HP_TIMING_DIFF(Diff, Start, End) (Diff) = ((End) - (Start))
113 /* We have to jump through hoops to get this correctly implemented. */
114 #define HP_TIMING_ACCUM(Sum, Diff) \
115 do { \
116 int __not_done; \
117 hp_timing_t __oldval = (Sum); \
118 hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead); \
119 do \
121 hp_timing_t __newval = __oldval + __diff; \
122 int __temp0, __temp1; \
123 __asm__ __volatile__ ("xchgl %0, %%ebx\n\t" \
124 "lock; cmpxchg8b %1\n\t" \
125 "sete %%bl\n\t" \
126 "xchgl %0, %%ebx" \
127 : "=SD" (__not_done), "=m" (Sum), \
128 "=A" (__oldval), "=c" (__temp0) \
129 : "m" (Sum), "2" (__oldval), \
130 "3" ((unsigned int) (__newval >> 32)), \
131 "0" ((unsigned int) __newval)); \
133 while ((unsigned char) __not_done); \
134 } while (0)
136 /* No threads, no extra work. */
137 #define HP_TIMING_ACCUM_NT(Sum, Diff) (Sum) += (Diff)
139 /* Print the time value. */
140 #define HP_TIMING_PRINT(Buf, Len, Val) \
141 do { \
142 char __buf[20]; \
143 char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0); \
144 size_t __len = (Len); \
145 char *__dest = (Buf); \
146 while (__len-- > 0 && __cp < __buf + sizeof (__buf)) \
147 *__dest++ = *__cp++; \
148 memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles"))); \
149 } while (0)
151 #endif /* hp-timing.h */