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