Back to index

glibc  2.9
hp-timing.h
Go to the documentation of this file.
00001 /* High precision, low overhead timing functions.  sparcv9 version.
00002    Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by David S. Miller <davem@redhat.com>, 2001.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef _HP_TIMING_H
00022 #define _HP_TIMING_H 1
00023 
00024 #include <string.h>
00025 #include <sys/param.h>
00026 #include <stdio-common/_itoa.h>
00027 
00028 #define HP_TIMING_AVAIL            (1)
00029 #define HP_TIMING_INLINE    (1)
00030 
00031 typedef unsigned long long int hp_timing_t;
00032 
00033 #define HP_TIMING_ZERO(Var) (Var) = (0)
00034 
00035 #define HP_TIMING_NOW(Var) \
00036       __asm__ __volatile__ ("rd %%tick, %L0\n\t" \
00037                          "srlx %L0, 32, %H0" \
00038                          : "=r" (Var))
00039 
00040 #define HP_TIMING_DIFF_INIT() \
00041   do {                                                               \
00042     int __cnt = 5;                                                   \
00043     GLRO(dl_hp_timing_overhead) = ~0ull;                             \
00044     do                                                               \
00045       {                                                                     \
00046        hp_timing_t __t1, __t2;                                              \
00047        HP_TIMING_NOW (__t1);                                                \
00048        HP_TIMING_NOW (__t2);                                                \
00049        if (__t2 - __t1 < GLRO(dl_hp_timing_overhead))                       \
00050          GLRO(dl_hp_timing_overhead) = __t2 - __t1;                         \
00051       }                                                                     \
00052     while (--__cnt > 0);                                             \
00053   } while (0)
00054 
00055 #define HP_TIMING_DIFF(Diff, Start, End)  (Diff) = ((End) - (Start))
00056 
00057 #define HP_TIMING_ACCUM(Sum, Diff)                      \
00058 do {                                                    \
00059   hp_timing_t __diff = (Diff) - GLRO(dl_hp_timing_overhead);   \
00060   __asm__ __volatile__("srl %L0, 0, %%g1\n\t"           \
00061                      "sllx  %H0, 32, %%g6\n\t"          \
00062                      "or    %%g1, %%g6, %%g1\n\t"              \
00063                      "1: ldx       [%1], %%g5\n\t"                    \
00064                      "add   %%g5, %%g1, %%g6\n\t"              \
00065                      "casx  [%1], %%g5,  %%g6\n\t"             \
00066                      "cmp   %%g5, %%g6\n\t"                    \
00067                      "bne,pn       %%xcc, 1b\n\t"                     \
00068                      " nop"                             \
00069                      : /* no outputs */                 \
00070                      : "r" (__diff), "r" (&(Sum))              \
00071                      : "memory", "g1", "g5", "g6");            \
00072 } while(0)
00073 
00074 #define HP_TIMING_ACCUM_NT(Sum, Diff)     (Sum) += (Diff)
00075 
00076 #define HP_TIMING_PRINT(Buf, Len, Val) \
00077   do {                                                               \
00078     char __buf[20];                                                  \
00079     char *__cp = _itoa (Val, __buf + sizeof (__buf), 10, 0);                \
00080     int __len = (Len);                                                      \
00081     char *__dest = (Buf);                                            \
00082     while (__len-- > 0 && __cp < __buf + sizeof (__buf))                    \
00083       *__dest++ = *__cp++;                                           \
00084     memcpy (__dest, " clock cycles", MIN (__len, sizeof (" clock cycles")));  \
00085   } while (0)
00086 
00087 #endif /* hp-timing.h */