xref: /freebsd/contrib/arm-optimized-routines/string/include/benchlib.h (revision 31914882fca502069810b9e9ddea4bcd8136a4f4)
1*31914882SAlex Richardson /*
2*31914882SAlex Richardson  * Benchmark support functions.
3*31914882SAlex Richardson  *
4*31914882SAlex Richardson  * Copyright (c) 2020, Arm Limited.
5*31914882SAlex Richardson  * SPDX-License-Identifier: MIT
6*31914882SAlex Richardson  */
7*31914882SAlex Richardson 
8*31914882SAlex Richardson #include <stdint.h>
9*31914882SAlex Richardson #include <time.h>
10*31914882SAlex Richardson 
11*31914882SAlex Richardson /* Fast and accurate timer returning nanoseconds.  */
12*31914882SAlex Richardson static inline uint64_t
13*31914882SAlex Richardson clock_get_ns (void)
14*31914882SAlex Richardson {
15*31914882SAlex Richardson   struct timespec ts;
16*31914882SAlex Richardson   clock_gettime (CLOCK_MONOTONIC, &ts);
17*31914882SAlex Richardson   return ts.tv_sec * (uint64_t) 1000000000 + ts.tv_nsec;
18*31914882SAlex Richardson }
19*31914882SAlex Richardson 
20*31914882SAlex Richardson /* Fast 32-bit random number generator.  Passing a non-zero seed
21*31914882SAlex Richardson    value resets the internal state.  */
22*31914882SAlex Richardson static inline uint32_t
23*31914882SAlex Richardson rand32 (uint32_t seed)
24*31914882SAlex Richardson {
25*31914882SAlex Richardson   static uint64_t state = 0xb707be451df0bb19ULL;
26*31914882SAlex Richardson   if (seed != 0)
27*31914882SAlex Richardson     state = seed;
28*31914882SAlex Richardson   uint32_t res = state >> 32;
29*31914882SAlex Richardson   state = state * 6364136223846793005ULL + 1;
30*31914882SAlex Richardson   return res;
31*31914882SAlex Richardson }
32*31914882SAlex Richardson 
33*31914882SAlex Richardson 
34