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