aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-generic/odp_time.c
blob: a08833d0e5dba7f633716d60e03264a7e40785db (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
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#define _POSIX_C_SOURCE 200809L

#include <odp/time.h>
#include <odp/hints.h>
#include <odp/system_info.h>

#define GIGA 1000000000

uint64_t odp_time_diff_cycles(uint64_t t1, uint64_t t2)
{
	if (odp_likely(t2 > t1))
		return t2 - t1;

	return t2 + (UINT64_MAX - t1);
}

uint64_t odp_time_cycles_to_ns(uint64_t cycles)
{
	uint64_t hz = odp_sys_cpu_hz();

	if (cycles > (UINT64_MAX / GIGA))
		return (cycles/hz)*GIGA;

	return (cycles*GIGA)/hz;
}


uint64_t odp_time_ns_to_cycles(uint64_t ns)
{
	uint64_t hz = odp_sys_cpu_hz();

	if (ns > (UINT64_MAX / hz))
		return (ns/GIGA)*hz;

	return (ns*hz)/GIGA;
}