Packet API: use status
[babeltrace.git] / include / babeltrace / trace-ir / utils-internal.h
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
31 struct search_query {
32 gpointer value;
33 int found;
34 };
35
36 static inline
37 uint64_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
57 static inline
58 int bt_util_ns_from_origin(const struct bt_clock_class *clock_class,
59 uint64_t value, int64_t *ns_from_origin)
60 {
61 int ret = 0;
62 uint64_t value_ns_unsigned;
63 int64_t value_ns_signed;
64
65 if (clock_class->base_offset.overflows) {
66 ret = -1;
67 goto end;
68 }
69
70 /* Initialize to clock class's base offset */
71 *ns_from_origin = clock_class->base_offset.value_ns;
72
73 /* Add given value in cycles */
74 value_ns_unsigned = bt_util_ns_from_value(clock_class->frequency, value);
75 if (value_ns_unsigned >= (uint64_t) INT64_MAX) {
76 /*
77 * FIXME: `value_ns_unsigned` could be greater than
78 * `INT64_MAX` in fact: in this case, we need to
79 * subtract `INT64_MAX` from `value_ns_unsigned`, make
80 * sure that the difference is less than `INT64_MAX`,
81 * and try to add them one after the other to
82 * `*ns_from_origin`.
83 */
84 ret = -1;
85 goto end;
86 }
87
88 value_ns_signed = (int64_t) value_ns_unsigned;
89 BT_ASSERT(value_ns_signed >= 0);
90
91 if (*ns_from_origin <= 0) {
92 goto add_value;
93 }
94
95 if (value_ns_signed > INT64_MAX - *ns_from_origin) {
96 ret = -1;
97 goto end;
98 }
99
100 add_value:
101 *ns_from_origin += value_ns_signed;
102
103 end:
104 return ret;
105 }
106
107 static inline
108 bool bt_util_value_is_in_range_signed(uint64_t size, int64_t value)
109 {
110 int64_t min_value = UINT64_C(-1) << (size - 1);
111 int64_t max_value = (UINT64_C(1) << (size - 1)) - 1;
112 return value >= min_value && value <= max_value;
113 }
114
115 static inline
116 bool bt_util_value_is_in_range_unsigned(unsigned int size, uint64_t value)
117 {
118 uint64_t max_value = (size == 64) ? UINT64_MAX :
119 (UINT64_C(1) << size) - 1;
120 return value <= max_value;
121 }
122
123 #endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.031389 seconds and 4 git commands to generate.