2.9
[glibc/nacl-glibc.git] / sysdeps / i386 / i686 / hp-timing.h
blobb924869649ebdcea7042ededddaa3d1560b22753
1 /* High precision, low overhead timing functions. i686 version.
2 Copyright (C) 1998, 2002, 2003, 2004, 2005 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _HP_TIMING_H
22 #define _HP_TIMING_H 1
24 #include <string.h>
25 #include <sys/param.h>
26 #include <stdio-common/_itoa.h>
28 /* The macros defined here use the timestamp counter in i586 and up versions
29 of the x86 processors. They provide a very accurate way to measure the
30 time with very little overhead. The time values themself have no real
31 meaning, only differences are interesting.
33 This version is for the i686 processors. The difference to the i586
34 version is that the timerstamp register is unconditionally used. This is
35 not the case for the i586 version where we have to perform runtime test
36 whether the processor really has this capability. We have to make this
37 distinction since the sysdeps/i386/i586 code is supposed to work on all
38 platforms while the i686 already contains i686-specific code.
40 The list of macros we need includes the following:
42 - HP_TIMING_AVAIL: test for availability.
44 - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
45 implemented using function calls but instead uses some inlined code
46 which might simply consist of a few assembler instructions. We have to
47 know this since we might want to use the macros here in places where we
48 cannot make function calls.
50 - hp_timing_t: This is the type for variables used to store the time
51 values.
53 - HP_TIMING_ZERO: clear `hp_timing_t' object.
55 - HP_TIMING_NOW: place timestamp for current time in variable given as
56 parameter.
58 - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
59 HP_TIMING_DIFF macro.
61 - HP_TIMING_DIFF: compute difference between two times and store it
62 in a third. Source and destination might overlap.
64 - HP_TIMING_ACCUM: add time difference to another variable. This might
65 be a bit more complicated to implement for some platforms as the
66 operation should be thread-safe and 64bit arithmetic on 32bit platforms
67 is not.
69 - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
70 there are no threads involved.
72 - HP_TIMING_PRINT: write decimal representation of the timing value into
73 the given string. This operation need not be inline even though
74 HP_TIMING_INLINE is specified.
78 /* We always assume having the timestamp register. */
79 #define HP_TIMING_AVAIL (1)
81 /* We indeed have inlined functions. */
82 #define HP_TIMING_INLINE (1)
84 /* We use 64bit values for the times. */
85 typedef unsigned long long int hp_timing_t;
87 /* Set timestamp value to zero. */
88 #define HP_TIMING_ZERO(Var) (Var) = (0)
90 /* That's quite simple. Use the `rdtsc' instruction. Note that the value
91 might not be 100% accurate since there might be some more instructions
92 running in this moment. This could be changed by using a barrier like
93 'cpuid' right before the `rdtsc' instruciton. But we are not interested
94 in accurate clock cycles here so we don't do this. */
95 #define HP_TIMING_NOW(Var) __asm__ __volatile__ ("rdtsc" : "=A" (Var))
97 /* Use two 'rdtsc' instructions in a row to find out how long it takes. */
98 #define HP_TIMING_DIFF_INIT() \
99 do { \
100 if (GLRO(dl_hp_timing_overhead) == 0) \
102 int __cnt = 5; \
103 GLRO(dl_hp_timing_overhead) = ~0ull; \
104 do \
106 hp_timing_t __t1, __t2; \
107 HP_TIMING_NOW (__t1); \
108 HP_TIMING_NOW (__t2); \
109 if (__t2 - __t1 < GLRO(dl_hp_timing_overhead)) \
110 GLRO(dl_hp_timing_overhead) = __t2 - __t1; \
112 while (--__cnt > 0); \
114 } while (0)
116 /* It's simple arithmetic for us. */
117 #define HP_TIMING_DIFF(Diff, Start, End) (Diff) = ((End) - (Start))
119 /* We have to jump through hoops to get this correctly implemented. */
120 #define HP_TIMING_ACCUM(Sum, Diff) \
121 do { \
122 int __not_done; \
123 hp_timing_t __oldval = (Sum); \
124 hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead); \
125 do \
127 hp_timing_t __newval = __oldval + __diff; \
128 int __temp0, __temp1; \
129 __asm__ __volatile__ ("xchgl %0, %%ebx\n\t" \
130 "lock; cmpxchg8b %1\n\t" \
131 "sete %%bl\n\t" \
132 "xchgl %0, %%ebx" \
133 : "=SD" (__not_done), "=m" (Sum), \
134 "=A" (__oldval), "=c" (__temp0) \
135 : "m" (Sum), "2" (__oldval), \
136 "3" ((unsigned int) (__newval >> 32)), \
137 "0" ((unsigned int) __newval)); \
139 while ((unsigned char) __not_done); \
140 } while (0)
142 /* No threads, no extra work. */
143 #define HP_TIMING_ACCUM_NT(Sum, Diff) (Sum) += (Diff)
145 /* Print the time value. */
146 #define HP_TIMING_PRINT(Buf, Len, Val) \
147 do { \
148 char __buf[20]; \
149 char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0); \
150 size_t __len = (Len); \
151 char *__dest = (Buf); \
152 while (__len-- > 0 && __cp < __buf + sizeof (__buf)) \
153 *__dest++ = *__cp++; \
154 memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles"))); \
155 } while (0)
157 #endif /* hp-timing.h */