xref: /freebsd/contrib/arm-optimized-routines/string/include/benchlib.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Benchmark support functions.
331914882SAlex Richardson  *
431914882SAlex Richardson  * Copyright (c) 2020, Arm Limited.
5072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson  */
731914882SAlex Richardson 
831914882SAlex Richardson #include <stdint.h>
931914882SAlex Richardson #include <time.h>
1031914882SAlex Richardson 
1131914882SAlex Richardson /* Fast and accurate timer returning nanoseconds.  */
1231914882SAlex Richardson static inline uint64_t
clock_get_ns(void)1331914882SAlex Richardson clock_get_ns (void)
1431914882SAlex Richardson {
1531914882SAlex Richardson   struct timespec ts;
1631914882SAlex Richardson   clock_gettime (CLOCK_MONOTONIC, &ts);
1731914882SAlex Richardson   return ts.tv_sec * (uint64_t) 1000000000 + ts.tv_nsec;
1831914882SAlex Richardson }
1931914882SAlex Richardson 
2031914882SAlex Richardson /* Fast 32-bit random number generator.  Passing a non-zero seed
2131914882SAlex Richardson    value resets the internal state.  */
2231914882SAlex Richardson static inline uint32_t
rand32(uint32_t seed)2331914882SAlex Richardson rand32 (uint32_t seed)
2431914882SAlex Richardson {
2531914882SAlex Richardson   static uint64_t state = 0xb707be451df0bb19ULL;
2631914882SAlex Richardson   if (seed != 0)
2731914882SAlex Richardson     state = seed;
2831914882SAlex Richardson   uint32_t res = state >> 32;
2931914882SAlex Richardson   state = state * 6364136223846793005ULL + 1;
3031914882SAlex Richardson   return res;
3131914882SAlex Richardson }
3231914882SAlex Richardson 
33*f3087befSAndrew Turner /* Macros to run a benchmark BENCH using string function FN.  */
34*f3087befSAndrew Turner #define RUN(BENCH, FN) BENCH(#FN, FN)
3531914882SAlex Richardson 
36*f3087befSAndrew Turner #if __aarch64__
37*f3087befSAndrew Turner # define RUNA64(BENCH, FN) BENCH(#FN, FN)
38*f3087befSAndrew Turner #else
39*f3087befSAndrew Turner # define RUNA64(BENCH, FN)
40*f3087befSAndrew Turner #endif
41*f3087befSAndrew Turner 
42*f3087befSAndrew Turner #if __ARM_FEATURE_SVE
43*f3087befSAndrew Turner # define RUNSVE(BENCH, FN) BENCH(#FN, FN)
44*f3087befSAndrew Turner #else
45*f3087befSAndrew Turner # define RUNSVE(BENCH, FN)
46*f3087befSAndrew Turner #endif
47*f3087befSAndrew Turner 
48*f3087befSAndrew Turner #if WANT_MOPS
49*f3087befSAndrew Turner # define RUNMOPS(BENCH, FN) BENCH(#FN, FN)
50*f3087befSAndrew Turner #else
51*f3087befSAndrew Turner # define RUNMOPS(BENCH, FN)
52*f3087befSAndrew Turner #endif
53*f3087befSAndrew Turner 
54*f3087befSAndrew Turner #if __arm__
55*f3087befSAndrew Turner # define RUNA32(BENCH, FN) BENCH(#FN, FN)
56*f3087befSAndrew Turner #else
57*f3087befSAndrew Turner # define RUNA32(BENCH, FN)
58*f3087befSAndrew Turner #endif
59*f3087befSAndrew Turner 
60*f3087befSAndrew Turner #if __arm__ && __ARM_ARCH >= 6 && __ARM_ARCH_ISA_THUMB == 2
61*f3087befSAndrew Turner # define RUNT32(BENCH, FN) BENCH(#FN, FN)
62*f3087befSAndrew Turner #else
63*f3087befSAndrew Turner # define RUNT32(BENCH, FN)
64*f3087befSAndrew Turner #endif
65