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