1 /* High precision, low overhead timing functions.  Generic version.
2    Copyright (C) 1998-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 /* In case a platform supports timers in the hardware the following macros
20    and types must be defined:
21 
22    - HP_TIMING_INLINE: this macro is non-zero if the functionality is not
23      implemented using function calls but instead uses some inlined code
24      which might simply consist of a few assembler instructions.  We have to
25      know this since we might want to use the macros here in places where we
26      cannot make function calls.
27 
28    - hp_timing_t: This is the type for variables used to store the time
29      values.  This type must be integral.
30 
31    - HP_TIMING_NOW: place timestamp for current time in variable given as
32      parameter.
33 */
34 
35 /* The target supports hp-timing.  Share the common infrastructure.  */
36 
37 #include <string.h>
38 #include <sys/param.h>
39 #include <_itoa.h>
40 
41 /* Compute the difference between START and END, storing into DIFF.  */
42 #define HP_TIMING_DIFF(Diff, Start, End)	((Diff) = (End) - (Start))
43 
44 /* Accumulate ADD into SUM.  No attempt is made to be thread-safe.  */
45 #define HP_TIMING_ACCUM_NT(Sum, Diff)		((Sum) += (Diff))
46 
47 #define HP_TIMING_PRINT_SIZE (3 * sizeof (hp_timing_t) + 1)
48 
49 /* Write a decimal representation of the timing value into the given string.  */
50 #define HP_TIMING_PRINT(Dest, Len, Val) 				\
51   do {									\
52     char __buf[HP_TIMING_PRINT_SIZE];					\
53     char *__dest = (Dest);						\
54     size_t __len = (Len);						\
55     char *__cp = _itoa ((Val), __buf + sizeof (__buf), 10, 0);		\
56     size_t __cp_len = MIN (__buf + sizeof (__buf) - __cp, __len);	\
57     memcpy (__dest, __cp, __cp_len);					\
58     __dest[__cp_len - 1] = '\0';					\
59   } while (0)
60