1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <stdint.h>
16#include <utils/time.h>
17
18#define KHZ (1000)
19#define MHZ (1000 * KHZ)
20#define GHZ (1000 * MHZ)
21
22typedef uint64_t freq_t;
23
24static inline uint64_t freq_cycles_and_hz_to_ns(uint64_t ncycles, freq_t hz)
25{
26    if (hz % GHZ == 0) {
27        return ncycles / (hz / GHZ);
28    } else if (hz % MHZ == 0) {
29        return ncycles * MS_IN_S / (hz / MHZ);
30    } else if (hz % KHZ == 0) {
31        return ncycles * US_IN_S / (hz / KHZ);
32    }
33
34    return (ncycles * NS_IN_S) / hz;
35}
36
37static inline freq_t freq_cycles_and_ns_to_hz(uint64_t ncycles, uint64_t ns)
38{
39    return (ncycles * NS_IN_S) / ns;
40}
41
42static inline uint64_t freq_ns_and_hz_to_cycles(uint64_t ns, freq_t hz)
43{
44    return (ns * hz) / NS_IN_S;
45}
46