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