High-preicion timer access code for 32-bit UltraSPARC III.
[kopensolaris-gnu/glibc.git] / sysdeps / sparc / sparc32 / sparcv9 / hp-timing.h
1 /* High precision, low overhead timing functions.  sparcv9 version.
2    Copyright (C) 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by David S. Miller <davem@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 Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    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    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #ifndef _HP_TIMING_H
22 #define _HP_TIMING_H    1
23
24 #include <string.h>
25 #include <sys/param.h>
26 #include <stdio-common/_itoa.h>
27
28 #define HP_TIMING_AVAIL         (1)
29 #define HP_TIMING_INLINE        (1)
30
31 typedef unsigned long long int hp_timing_t;
32
33 extern hp_timing_t __libc_hp_timing_overhead;
34
35 #define HP_TIMING_ZERO(Var)     (Var) = (0)
36
37 #define HP_TIMING_NOW(Var) \
38       __asm__ __volatile__ ("rd %%tick, %L0\n\t" \
39                             "srlx %L0, 32, %H0" \
40                             : "=r" (Var))
41
42 #define HP_TIMING_DIFF_INIT() \
43   do {                                                                        \
44     int __cnt = 5;                                                            \
45     __libc_hp_timing_overhead = ~0ull;                                        \
46     do                                                                        \
47       {                                                                       \
48         hp_timing_t __t1, __t2;                                               \
49         HP_TIMING_NOW (__t1);                                                 \
50         HP_TIMING_NOW (__t2);                                                 \
51         if (__t2 - __t1 < __libc_hp_timing_overhead)                          \
52           __libc_hp_timing_overhead = __t2 - __t1;                            \
53       }                                                                       \
54     while (--__cnt > 0);                                                      \
55   } while (0)
56
57 #define HP_TIMING_DIFF(Diff, Start, End)        (Diff) = ((End) - (Start))
58
59 #define HP_TIMING_ACCUM(Sum, Diff)                              \
60 do {                                                            \
61   hp_timing_t __diff = (Diff) - __libc_hp_timing_overhead;      \
62   __asm__ __volatile__("srl     %L0, 0, %%g1\n\t"               \
63                        "sllx    %H0, 32, %%g7\n\t"              \
64                        "or      %%g1, %%g7, %%g1\n\t"           \
65                        "1: ldx  [%1], %%g5\n\t"                 \
66                        "add     %%g5, %%g1, %%g7\n\t"           \
67                        "casx    [%1], %%g5,  %%g7\n\t"          \
68                        "cmp     %%g5, %%g7\n\t"                 \
69                        "bne,pn  %%xcc, 1b\n\t"                  \
70                        " nop"                                   \
71                        : /* no outputs */                       \
72                        : "r" (__diff), "r" (&(Sum))             \
73                        : "memory", "g1", "g5", "g7");           \
74 } while(0)
75
76 #define HP_TIMING_ACCUM_NT(Sum, Diff)   (Sum) += (Diff)
77
78 #define HP_TIMING_PRINT(Buf, Len, Val) \
79   do {                                                                        \
80     char __buf[20];                                                           \
81     char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0);                  \
82     int __len = (Len);                                                        \
83     char *__dest = (Buf);                                                     \
84     while (__len-- > 0 && __cp < __buf + sizeof (__buf))                      \
85       *__dest++ = *__cp++;                                                    \
86     memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles")));  \
87   } while (0)
88
89 #endif  /* hp-timing.h */