Move all files into ports/ subdirectory in preparation for merge with glibc
[jlayton/glibc.git] / ports / sysdeps / alpha / hp-timing.h
1 /* High precision, low overhead timing functions.  Alpha version.
2    Copyright (C) 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Richard Henderson <rth@redhat.com>, 2001.
5
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.
10
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.
15
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/>.  */
19
20 #ifndef _HP_TIMING_H
21 #define _HP_TIMING_H    1
22
23 #include <string.h>
24 #include <sys/param.h>
25 #include <_itoa.h>
26
27 /* The macros defined here use the timestamp counter in IA-64.  They
28    provide a very accurate way to measure the time with very little
29    overhead.  The time values themself have no real meaning, only
30    differences are interesting.
31
32    The list of macros we need includes the following:
33
34    - HP_TIMING_AVAIL: test for availability.
35
36    - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
37      implemented using function calls but instead uses some inlined code
38      which might simply consist of a few assembler instructions.  We have to
39      know this since we might want to use the macros here in places where we
40      cannot make function calls.
41
42    - hp_timing_t: This is the type for variables used to store the time
43      values.
44
45    - HP_TIMING_ZERO: clear `hp_timing_t' object.
46
47    - HP_TIMING_NOW: place timestamp for current time in variable given as
48      parameter.
49
50    - HP_TIMING_DIFF_INIT: do whatever is necessary to be able to use the
51      HP_TIMING_DIFF macro.
52
53    - HP_TIMING_DIFF: compute difference between two times and store it
54      in a third.  Source and destination might overlap.
55
56    - HP_TIMING_ACCUM: add time difference to another variable.  This might
57      be a bit more complicated to implement for some platforms as the
58      operation should be thread-safe and 64bit arithmetic on 32bit platforms
59      is not.
60
61    - HP_TIMING_ACCUM_NT: this is the variant for situations where we know
62      there are no threads involved.
63
64    - HP_TIMING_PRINT: write decimal representation of the timing value into
65      the given string.  This operation need not be inline even though
66      HP_TIMING_INLINE is specified.
67 */
68
69 /* We always have the timestamp register, but it's got only a 4 second
70    range.  Use it for ld.so profiling only.  */
71 #define HP_TIMING_AVAIL         (0)
72 #define HP_SMALL_TIMING_AVAIL   (1)
73
74 /* We indeed have inlined functions.  */
75 #define HP_TIMING_INLINE        (1)
76
77 /* We use 32 bit values for the times.  */
78 typedef unsigned int hp_timing_t;
79
80 /* Set timestamp value to zero.  */
81 #define HP_TIMING_ZERO(VAR)     (VAR) = (0)
82
83 /* The "rpcc" instruction returns a 32-bit counting half and a 32-bit
84    "virtual cycle counter displacement".  Subtracting the two gives us
85    a virtual cycle count.  */
86 #define HP_TIMING_NOW(VAR) \
87   do {                                                                        \
88     unsigned long int x_;                                                     \
89     asm volatile ("rpcc %0" : "=r"(x_));                                      \
90     (VAR) = (int) (x_) - (int) (x_ >> 32);                                    \
91   } while (0)
92
93 /* ??? Two rpcc instructions can be scheduled simultaneously.  */
94 #define HP_TIMING_DIFF_INIT() do { } while (0)
95
96 /* It's simple arithmetic for us.  */
97 #define HP_TIMING_DIFF(Diff, Start, End)        (Diff) = ((End) - (Start))
98
99 /* ??? Don't bother, since we're only used for ld.so.  */
100 #define HP_TIMING_ACCUM(Sum, Diff)  not implemented
101
102 /* No threads, no extra work.  */
103 #define HP_TIMING_ACCUM_NT(Sum, Diff)   (Sum) += (Diff)
104
105 /* Print the time value.  */
106 #define HP_TIMING_PRINT(Buf, Len, Val) \
107   do {                                                                        \
108     char __buf[20];                                                           \
109     char *__cp = _itoa_word (Val, __buf + sizeof (__buf), 10, 0);             \
110     int __len = (Len);                                                        \
111     char *__dest = (Buf);                                                     \
112     while (__len-- > 0 && __cp < __buf + sizeof (__buf))                      \
113       *__dest++ = *__cp++;                                                    \
114     memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles")));  \
115   } while (0)
116
117 #endif  /* hp-timing.h */