xref: /freebsd/contrib/arm-optimized-routines/string/include/benchlib.h (revision 6c05f3a74f30934ee60919cc97e16ec69b542b06)
1 /*
2  * Benchmark support functions.
3  *
4  * Copyright (c) 2020, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include <stdint.h>
9 #include <time.h>
10 
11 /* Fast and accurate timer returning nanoseconds.  */
12 static inline uint64_t
13 clock_get_ns (void)
14 {
15   struct timespec ts;
16   clock_gettime (CLOCK_MONOTONIC, &ts);
17   return ts.tv_sec * (uint64_t) 1000000000 + ts.tv_nsec;
18 }
19 
20 /* Fast 32-bit random number generator.  Passing a non-zero seed
21    value resets the internal state.  */
22 static inline uint32_t
23 rand32 (uint32_t seed)
24 {
25   static uint64_t state = 0xb707be451df0bb19ULL;
26   if (seed != 0)
27     state = seed;
28   uint32_t res = state >> 32;
29   state = state * 6364136223846793005ULL + 1;
30   return res;
31 }
32 
33 /* Macros to run a benchmark BENCH using string function FN.  */
34 #define RUN(BENCH, FN) BENCH(#FN, FN)
35 
36 #if __aarch64__
37 # define RUNA64(BENCH, FN) BENCH(#FN, FN)
38 #else
39 # define RUNA64(BENCH, FN)
40 #endif
41 
42 #if __ARM_FEATURE_SVE
43 # define RUNSVE(BENCH, FN) BENCH(#FN, FN)
44 #else
45 # define RUNSVE(BENCH, FN)
46 #endif
47 
48 #if WANT_MOPS
49 # define RUNMOPS(BENCH, FN) BENCH(#FN, FN)
50 #else
51 # define RUNMOPS(BENCH, FN)
52 #endif
53 
54 #if __arm__
55 # define RUNA32(BENCH, FN) BENCH(#FN, FN)
56 #else
57 # define RUNA32(BENCH, FN)
58 #endif
59 
60 #if __arm__ && __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 2
61 # define RUNT32(BENCH, FN) BENCH(#FN, FN)
62 #else
63 # define RUNT32(BENCH, FN)
64 #endif
65