1 /* High precision, low overhead timing functions. IA-64 version.
2 Copyright (C) 2001-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2001.
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/>. */
21 #define _HP_TIMING_H 1
24 #include <sys/param.h>
26 #include <ia64intrin.h>
28 /* The macros defined here use the timestamp counter in IA-64. They
29 provide a very accurate way to measure the time with very little
30 overhead. The time values themself have no real meaning, only
31 differences are interesting.
33 The list of macros we need includes the following:
35 - HP_TIMING_AVAIL: test for availability.
37 - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
38 implemented using function calls but instead uses some inlined code
39 which might simply consist of a few assembler instructions. We have to
40 know this since we might want to use the macros here in places where we
41 cannot make function calls.
43 - hp_timing_t: This is the type for variables used to store the time
46 - HP_TIMING_ZERO: clear `hp_timing_t' object.
48 - HP_TIMING_NOW: place timestamp for current time in variable given as
51 - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
54 - HP_TIMING_DIFF: compute difference between two times and store it
55 in a third. Source and destination might overlap.
57 - HP_TIMING_ACCUM: add time difference to another variable. This might
58 be a bit more complicated to implement for some platforms as the
59 operation should be thread-safe and 64bit arithmetic on 32bit platforms
62 - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
63 there are no threads involved.
65 - HP_TIMING_PRINT: write decimal representation of the timing value into
66 the given string. This operation need not be inline even though
67 HP_TIMING_INLINE is specified.
71 /* We always assume having the timestamp register. */
72 #define HP_TIMING_AVAIL (1)
74 /* We indeed have inlined functions. */
75 #define HP_TIMING_INLINE (1)
77 /* We use 64bit values for the times. */
78 typedef unsigned long int hp_timing_t
;
80 /* Set timestamp value to zero. */
81 #define HP_TIMING_ZERO(Var) (Var) = (0)
84 /* The Itanium/Merced has a bug where the ar.itc register value read
85 is not correct in some situations. The solution is to read again.
86 For now we always do this until we know how to recognize a fixed
87 processor implementation. */
88 #define REPEAT_READ(val) __builtin_expect ((long int) val == -1, 0)
90 /* That's quite simple. Use the `ar.itc' instruction. */
91 #define HP_TIMING_NOW(Var) \
92 ({ unsigned long int __itc; \
94 asm volatile ("mov %0=ar.itc" : "=r" (__itc) : : "memory"); \
95 while (REPEAT_READ (__itc)); \
98 /* Use two 'ar.itc' instructions in a row to find out how long it takes. */
99 #define HP_TIMING_DIFF_INIT() \
102 GLRO(dl_hp_timing_overhead) = ~0ul; \
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); \
114 /* It's simple arithmetic for us. */
115 #define HP_TIMING_DIFF(Diff, Start, End) (Diff) = ((End) - (Start))
117 /* We have to jump through hoops to get this correctly implemented. */
118 #define HP_TIMING_ACCUM(Sum, Diff) \
120 hp_timing_t __oldval; \
121 hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead); \
122 hp_timing_t __newval; \
126 __newval = __oldval + __diff; \
128 while (! __sync_bool_compare_and_swap (&Sum, __oldvar, __newval)); \
131 /* No threads, no extra work. */
132 #define HP_TIMING_ACCUM_NT(Sum, Diff) (Sum) += (Diff)
134 /* Print the time value. */
135 #define HP_TIMING_PRINT(Buf, Len, Val) \
138 char *__cp = _itoa_word (Val, __buf + sizeof (__buf), 10, 0); \
140 char *__dest = (Buf); \
141 while (__len-- > 0 && __cp < __buf + sizeof (__buf)) \
142 *__dest++ = *__cp++; \
143 memcpy (__dest, " clock cycles", MIN (__len, \
144 (int) sizeof (" clock cycles"))); \
147 #endif /* hp-timing.h */