Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / i386 / i686 / hp-timing.h
blob4a2006e7457d022de8315d16fbca7228cfb4908e
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_ZERO: clear `hp_timing_t' object.
54 - HP_TIMING_NOW: place timestamp for current time in variable given as
55 parameter.
57 - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
58 HP_TIMING_DIFF macro.
60 - HP_TIMING_DIFF: compute difference between two times and store it
61 in a third. Source and destination might overlap.
63 - HP_TIMING_ACCUM: add time difference to another variable. This might
64 be a bit more complicated to implement for some platforms as the
65 operation should be thread-safe and 64bit arithmetic on 32bit platforms
66 is not.
68 - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
69 there are no threads involved.
71 - HP_TIMING_PRINT: write decimal representation of the timing value into
72 the given string. This operation need not be inline even though
73 HP_TIMING_INLINE is specified.
77 /* We always assume having the timestamp register. */
78 #define HP_TIMING_AVAIL (1)
80 /* We indeed have inlined functions. */
81 #define HP_TIMING_INLINE (1)
83 /* We use 64bit values for the times. */
84 typedef unsigned long long int hp_timing_t;
86 /* Set timestamp value to zero. */
87 #define HP_TIMING_ZERO(Var) (Var) = (0)
89 /* That's quite simple. Use the `rdtsc' instruction. Note that the value
90 might not be 100% accurate since there might be some more instructions
91 running in this moment. This could be changed by using a barrier like
92 'cpuid' right before the `rdtsc' instruciton. But we are not interested
93 in accurate clock cycles here so we don't do this. */
94 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
96 /* Use two 'rdtsc' instructions in a row to find out how long it takes. */
97 #define HP_TIMING_DIFF_INIT() \
98 do { \
99 if (GLRO(dl_hp_timing_overhead) == 0) \
101 int __cnt = 5; \
102 GLRO(dl_hp_timing_overhead) = ~0ull; \
103 do \
105 hp_timing_t __t1, __t2; \
106 HP_TIMING_NOW (__t1); \
107 HP_TIMING_NOW (__t2); \
108 if (__t2 - __t1 < GLRO(dl_hp_timing_overhead)) \
109 GLRO(dl_hp_timing_overhead) = __t2 - __t1; \
111 while (--__cnt > 0); \
113 } while (0)
115 /* It's simple arithmetic for us. */
116 #define HP_TIMING_DIFF(Diff, Start, End) (Diff) = ((End) - (Start))
118 /* We have to jump through hoops to get this correctly implemented. */
119 #define HP_TIMING_ACCUM(Sum, Diff) \
120 do { \
121 int __not_done; \
122 hp_timing_t __oldval = (Sum); \
123 hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead); \
124 do \
126 hp_timing_t __newval = __oldval + __diff; \
127 int __temp0, __temp1; \
128 __asm__ __volatile__ ("xchgl %0, %%ebx\n\t" \
129 "lock; cmpxchg8b %1\n\t" \
130 "sete %%bl\n\t" \
131 "xchgl %0, %%ebx" \
132 : "=SD" (__not_done), "=m" (Sum), \
133 "=A" (__oldval), "=c" (__temp0) \
134 : "m" (Sum), "2" (__oldval), \
135 "3" ((unsigned int) (__newval >> 32)), \
136 "0" ((unsigned int) __newval)); \
138 while ((unsigned char) __not_done); \
139 } while (0)
141 /* No threads, no extra work. */
142 #define HP_TIMING_ACCUM_NT(Sum, Diff) (Sum) += (Diff)
144 /* Print the time value. */
145 #define HP_TIMING_PRINT(Buf, Len, Val) \
146 do { \
147 char __buf[20]; \
148 char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0); \
149 size_t __len = (Len); \
150 char *__dest = (Buf); \
151 while (__len-- > 0 && __cp < __buf + sizeof (__buf)) \
152 *__dest++ = *__cp++; \
153 memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles"))); \
154 } while (0)
156 #endif /* hp-timing.h */