aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/arch/common/odp/api/abi/time_cpu_inlines.h
blob: c154c5f1a86ef1c4eb92aca2b40e4b6b4f9e0055 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright (c) 2013-2018, Linaro Limited
 * Copyright (c) 2020-2023, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#ifndef ODP_ARCH_TIME_CPU_INLINES_H_
#define ODP_ARCH_TIME_CPU_INLINES_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <odp/api/time_types.h>

#include <odp/api/abi/time_cpu.h>

#include <stdint.h>

#define _ODP_TIME_GIGA_HZ  1000000000ULL

typedef struct _odp_time_global_t {
	uint64_t start_time;
	uint64_t freq_hz;

} _odp_time_global_t;

extern _odp_time_global_t _odp_time_glob;

static inline odp_time_t _odp_time_cur(void)
{
	odp_time_t time;

	time.count = _odp_time_cpu_global() - _odp_time_glob.start_time;
	return time;
}

static inline odp_time_t _odp_time_cur_strict(void)
{
	odp_time_t time;

	time.count = _odp_time_cpu_global_strict() - _odp_time_glob.start_time;
	return time;
}

static inline uint64_t _odp_time_to_ns(odp_time_t time)
{
	uint64_t nsec;
	uint64_t freq_hz = _odp_time_glob.freq_hz;
	uint64_t count = time.count;
	uint64_t sec = 0;

	if (count >= freq_hz) {
		sec   = count / freq_hz;
		count = count - sec * freq_hz;
	}

	nsec = (_ODP_TIME_GIGA_HZ * count) / freq_hz;

	return (sec * _ODP_TIME_GIGA_HZ) + nsec;
}

static inline odp_time_t _odp_time_from_ns(uint64_t ns)
{
	odp_time_t time;
	uint64_t count;
	uint64_t freq_hz = _odp_time_glob.freq_hz;
	uint64_t sec = 0;

	if (ns >= ODP_TIME_SEC_IN_NS) {
		sec = ns / ODP_TIME_SEC_IN_NS;
		ns  = ns - sec * ODP_TIME_SEC_IN_NS;
	}

	count  = sec * freq_hz;
	count += (ns * freq_hz) / ODP_TIME_SEC_IN_NS;

	time.count = count;

	return time;
}

static inline uint64_t _odp_time_res(void)
{
	return _odp_time_glob.freq_hz;
}

#ifdef __cplusplus
}
#endif

#endif