xref: /freebsd/contrib/arm-optimized-routines/string/include/benchlib.h (revision 072a4ba82a01476eaee33781ccd241033eefcf0b)
131914882SAlex Richardson /*
231914882SAlex Richardson  * Benchmark support functions.
331914882SAlex Richardson  *
431914882SAlex Richardson  * Copyright (c) 2020, Arm Limited.
5*072a4ba8SAndrew 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
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
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 
3331914882SAlex Richardson 
34