lib: make trace IR API const-correct
[babeltrace.git] / include / babeltrace / trace-ir / utils-internal.h
CommitLineData
56e18c4c
PP
1#ifndef BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
2#define BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
726106d3
PP
3
4/*
726106d3
PP
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24#include <babeltrace/babeltrace-internal.h>
5cd6d0e5 25#include <babeltrace/trace-ir/field-classes.h>
56e18c4c 26#include <babeltrace/trace-ir/clock-class-internal.h>
726106d3
PP
27#include <stdint.h>
28
3dca2276
PP
29struct search_query {
30 gpointer value;
31 int found;
32};
33
44c440bc
PP
34static inline
35uint64_t bt_util_ns_from_value(uint64_t frequency, uint64_t value_cycles)
36{
37 uint64_t ns;
38
39 if (frequency == UINT64_C(1000000000)) {
40 ns = value_cycles;
41 } else {
42 double dblres = ((1e9 * (double) value_cycles) / (double) frequency);
43
44 if (dblres >= (double) UINT64_MAX) {
45 /* Overflows uint64_t */
46 ns = UINT64_C(-1);
47 } else {
48 ns = (uint64_t) dblres;
49 }
50 }
51
52 return ns;
53}
54
55static inline
40f4ba76
PP
56int bt_util_ns_from_origin(const struct bt_clock_class *clock_class,
57 uint64_t value, int64_t *ns_from_origin)
44c440bc
PP
58{
59 int ret = 0;
60 uint64_t value_ns_unsigned;
61 int64_t value_ns_signed;
62
63 if (clock_class->base_offset.overflows) {
64 ret = -1;
65 goto end;
66 }
67
68 /* Initialize to clock class's base offset */
69 *ns_from_origin = clock_class->base_offset.value_ns;
70
71 /* Add given value in cycles */
72 value_ns_unsigned = bt_util_ns_from_value(clock_class->frequency, value);
73 if (value_ns_unsigned >= (uint64_t) INT64_MAX) {
74 /*
75 * FIXME: `value_ns_unsigned` could be greater than
76 * `INT64_MAX` in fact: in this case, we need to
77 * subtract `INT64_MAX` from `value_ns_unsigned`, make
78 * sure that the difference is less than `INT64_MAX`,
79 * and try to add them one after the other to
80 * `*ns_from_origin`.
81 */
82 ret = -1;
83 goto end;
84 }
85
86 value_ns_signed = (int64_t) value_ns_unsigned;
87 BT_ASSERT(value_ns_signed >= 0);
88
89 if (*ns_from_origin <= 0) {
90 goto add_value;
91 }
92
93 if (value_ns_signed > INT64_MAX - *ns_from_origin) {
94 ret = -1;
95 goto end;
96 }
97
98add_value:
99 *ns_from_origin += value_ns_signed;
100
101end:
102 return ret;
103}
104
105static inline
106bool bt_util_value_is_in_range_signed(uint64_t size, int64_t value)
107{
108 int64_t min_value = UINT64_C(-1) << (size - 1);
109 int64_t max_value = (UINT64_C(1) << (size - 1)) - 1;
110 return value >= min_value && value <= max_value;
111}
112
113static inline
114bool bt_util_value_is_in_range_unsigned(unsigned int size, uint64_t value)
115{
116 uint64_t max_value = (size == 64) ? UINT64_MAX :
117 (UINT64_C(1) << size) - 1;
118 return value <= max_value;
119}
726106d3 120
56e18c4c 121#endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.028489 seconds and 4 git commands to generate.