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