Cleanup: remove private babeltrace.h
[babeltrace.git] / src / lib / trace-ir / utils.h
CommitLineData
108b91d0
PP
1#ifndef BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
2#define BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
bae99cf9
PP
3
4/*
f2b0325d
PP
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 *
bae99cf9
PP
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
85e7137b 26#include "common/macros.h"
71c5da58 27#include <babeltrace2/trace-ir/field-class.h>
bae99cf9
PP
28#include <stdint.h>
29
57952005
MJ
30#include "clock-class.h"
31
8deee039
PP
32struct search_query {
33 gpointer value;
34 int found;
35};
36
7b33a0e0
PP
37static inline
38uint64_t bt_util_ns_from_value(uint64_t frequency, uint64_t value_cycles)
39{
40 uint64_t ns;
41
42 if (frequency == UINT64_C(1000000000)) {
43 ns = value_cycles;
44 } else {
45 double dblres = ((1e9 * (double) value_cycles) / (double) frequency);
46
47 if (dblres >= (double) UINT64_MAX) {
48 /* Overflows uint64_t */
49 ns = UINT64_C(-1);
50 } else {
51 ns = (uint64_t) dblres;
52 }
53 }
54
55 return ns;
56}
57
58static inline
35fa110e
PP
59bool bt_util_get_base_offset_ns(int64_t offset_seconds, uint64_t offset_cycles,
60 uint64_t frequency, int64_t *base_offset_ns)
7b33a0e0 61{
35fa110e
PP
62 bool overflows = false;
63 uint64_t offset_cycles_ns;
7b33a0e0 64
35fa110e
PP
65 BT_ASSERT(base_offset_ns);
66
67 /* Initialize nanosecond timestamp to clock's offset in seconds */
68 if (offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) ||
69 offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) {
70 /*
71 * Overflow: offset in seconds converted to nanoseconds
72 * is outside the int64_t range. We also subtract 1 here
73 * to leave "space" for the offset in cycles converted
74 * to nanoseconds (which is always less than 1 second by
75 * contract).
76 */
77 overflows = true;
7b33a0e0
PP
78 goto end;
79 }
80
35fa110e
PP
81 /* Offset (seconds) to nanoseconds */
82 *base_offset_ns = offset_seconds * INT64_C(1000000000);
83
84 /* Add offset in cycles */
85 BT_ASSERT(offset_cycles < frequency);
86 offset_cycles_ns = bt_util_ns_from_value(frequency,
87 offset_cycles);
88 BT_ASSERT(offset_cycles_ns < 1000000000);
89 *base_offset_ns += (int64_t) offset_cycles_ns;
90
91end:
92 return overflows;
93}
94
95static inline
96int bt_util_ns_from_origin_inline(int64_t base_offset_ns,
97 int64_t offset_seconds, uint64_t offset_cycles,
98 uint64_t frequency, uint64_t value, int64_t *ns_from_origin)
99{
100 int ret = 0;
101 uint64_t value_ns_unsigned;
102 int64_t value_ns_signed;
103
7b33a0e0 104 /* Initialize to clock class's base offset */
35fa110e 105 *ns_from_origin = base_offset_ns;
7b33a0e0
PP
106
107 /* Add given value in cycles */
35fa110e 108 value_ns_unsigned = bt_util_ns_from_value(frequency, value);
7b33a0e0
PP
109 if (value_ns_unsigned >= (uint64_t) INT64_MAX) {
110 /*
111 * FIXME: `value_ns_unsigned` could be greater than
112 * `INT64_MAX` in fact: in this case, we need to
113 * subtract `INT64_MAX` from `value_ns_unsigned`, make
114 * sure that the difference is less than `INT64_MAX`,
115 * and try to add them one after the other to
116 * `*ns_from_origin`.
117 */
118 ret = -1;
119 goto end;
120 }
121
122 value_ns_signed = (int64_t) value_ns_unsigned;
123 BT_ASSERT(value_ns_signed >= 0);
124
125 if (*ns_from_origin <= 0) {
126 goto add_value;
127 }
128
129 if (value_ns_signed > INT64_MAX - *ns_from_origin) {
130 ret = -1;
131 goto end;
132 }
133
134add_value:
135 *ns_from_origin += value_ns_signed;
136
137end:
138 return ret;
139}
140
35fa110e
PP
141static inline
142int bt_util_ns_from_origin_clock_class(const struct bt_clock_class *clock_class,
143 uint64_t value, int64_t *ns_from_origin)
144{
145 int ret = 0;
146
147 if (clock_class->base_offset.overflows) {
148 ret = -1;
149 goto end;
150 }
151
152 ret = bt_util_ns_from_origin_inline(clock_class->base_offset.value_ns,
153 clock_class->offset_seconds, clock_class->offset_cycles,
154 clock_class->frequency, value, ns_from_origin);
155
156end:
157 return ret;
158}
159
7b33a0e0
PP
160static inline
161bool bt_util_value_is_in_range_signed(uint64_t size, int64_t value)
162{
163 int64_t min_value = UINT64_C(-1) << (size - 1);
164 int64_t max_value = (UINT64_C(1) << (size - 1)) - 1;
165 return value >= min_value && value <= max_value;
166}
167
168static inline
169bool bt_util_value_is_in_range_unsigned(unsigned int size, uint64_t value)
170{
171 uint64_t max_value = (size == 64) ? UINT64_MAX :
172 (UINT64_C(1) << size) - 1;
173 return value <= max_value;
174}
bae99cf9 175
108b91d0 176#endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.050798 seconds and 4 git commands to generate.