lib: remove unused parameters bt_util_ns_from_origin_inline
[babeltrace.git] / src / lib / trace-ir / utils.h
CommitLineData
726106d3 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
726106d3 3 *
0235b0db 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
726106d3
PP
5 */
6
0235b0db
MJ
7#ifndef BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
8#define BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
9
91d81473 10#include "common/macros.h"
3fadfbc0 11#include <babeltrace2/trace-ir/field-class.h>
c4f23e30 12#include <stdbool.h>
726106d3
PP
13#include <stdint.h>
14
578e048b
MJ
15#include "clock-class.h"
16
3dca2276
PP
17struct search_query {
18 gpointer value;
19 int found;
20};
21
44c440bc
PP
22static inline
23uint64_t bt_util_ns_from_value(uint64_t frequency, uint64_t value_cycles)
24{
25 uint64_t ns;
26
27 if (frequency == UINT64_C(1000000000)) {
28 ns = value_cycles;
29 } else {
30 double dblres = ((1e9 * (double) value_cycles) / (double) frequency);
31
32 if (dblres >= (double) UINT64_MAX) {
33 /* Overflows uint64_t */
34 ns = UINT64_C(-1);
35 } else {
36 ns = (uint64_t) dblres;
37 }
38 }
39
40 return ns;
41}
42
43static inline
0f2d58c9
PP
44bool bt_util_get_base_offset_ns(int64_t offset_seconds, uint64_t offset_cycles,
45 uint64_t frequency, int64_t *base_offset_ns)
44c440bc 46{
0f2d58c9
PP
47 bool overflows = false;
48 uint64_t offset_cycles_ns;
44c440bc 49
98b15851 50 BT_ASSERT_DBG(base_offset_ns);
0f2d58c9
PP
51
52 /* Initialize nanosecond timestamp to clock's offset in seconds */
53 if (offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) ||
54 offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) {
55 /*
56 * Overflow: offset in seconds converted to nanoseconds
57 * is outside the int64_t range. We also subtract 1 here
58 * to leave "space" for the offset in cycles converted
59 * to nanoseconds (which is always less than 1 second by
60 * contract).
61 */
62 overflows = true;
44c440bc
PP
63 goto end;
64 }
65
0f2d58c9
PP
66 /* Offset (seconds) to nanoseconds */
67 *base_offset_ns = offset_seconds * INT64_C(1000000000);
68
69 /* Add offset in cycles */
98b15851 70 BT_ASSERT_DBG(offset_cycles < frequency);
0f2d58c9
PP
71 offset_cycles_ns = bt_util_ns_from_value(frequency,
72 offset_cycles);
98b15851 73 BT_ASSERT_DBG(offset_cycles_ns < 1000000000);
0f2d58c9
PP
74 *base_offset_ns += (int64_t) offset_cycles_ns;
75
76end:
77 return overflows;
78}
79
80static inline
81int bt_util_ns_from_origin_inline(int64_t base_offset_ns,
0f2d58c9
PP
82 uint64_t frequency, uint64_t value, int64_t *ns_from_origin)
83{
84 int ret = 0;
85 uint64_t value_ns_unsigned;
86 int64_t value_ns_signed;
87
44c440bc 88 /* Initialize to clock class's base offset */
0f2d58c9 89 *ns_from_origin = base_offset_ns;
44c440bc
PP
90
91 /* Add given value in cycles */
0f2d58c9 92 value_ns_unsigned = bt_util_ns_from_value(frequency, value);
44c440bc
PP
93 if (value_ns_unsigned >= (uint64_t) INT64_MAX) {
94 /*
95 * FIXME: `value_ns_unsigned` could be greater than
96 * `INT64_MAX` in fact: in this case, we need to
97 * subtract `INT64_MAX` from `value_ns_unsigned`, make
98 * sure that the difference is less than `INT64_MAX`,
99 * and try to add them one after the other to
100 * `*ns_from_origin`.
101 */
102 ret = -1;
103 goto end;
104 }
105
106 value_ns_signed = (int64_t) value_ns_unsigned;
98b15851 107 BT_ASSERT_DBG(value_ns_signed >= 0);
44c440bc
PP
108
109 if (*ns_from_origin <= 0) {
110 goto add_value;
111 }
112
113 if (value_ns_signed > INT64_MAX - *ns_from_origin) {
114 ret = -1;
115 goto end;
116 }
117
118add_value:
119 *ns_from_origin += value_ns_signed;
120
121end:
122 return ret;
123}
124
0f2d58c9
PP
125static inline
126int bt_util_ns_from_origin_clock_class(const struct bt_clock_class *clock_class,
127 uint64_t value, int64_t *ns_from_origin)
128{
129 int ret = 0;
130
131 if (clock_class->base_offset.overflows) {
132 ret = -1;
133 goto end;
134 }
135
136 ret = bt_util_ns_from_origin_inline(clock_class->base_offset.value_ns,
0f2d58c9
PP
137 clock_class->frequency, value, ns_from_origin);
138
139end:
140 return ret;
141}
142
44c440bc
PP
143static inline
144bool bt_util_value_is_in_range_signed(uint64_t size, int64_t value)
145{
146 int64_t min_value = UINT64_C(-1) << (size - 1);
147 int64_t max_value = (UINT64_C(1) << (size - 1)) - 1;
148 return value >= min_value && value <= max_value;
149}
150
151static inline
152bool bt_util_value_is_in_range_unsigned(unsigned int size, uint64_t value)
153{
154 uint64_t max_value = (size == 64) ? UINT64_MAX :
155 (UINT64_C(1) << size) - 1;
156 return value <= max_value;
157}
726106d3 158
56e18c4c 159#endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.086984 seconds and 4 git commands to generate.