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, | |
82 | int64_t offset_seconds, uint64_t offset_cycles, | |
83 | uint64_t frequency, uint64_t value, int64_t *ns_from_origin) | |
84 | { | |
85 | int ret = 0; | |
86 | uint64_t value_ns_unsigned; | |
87 | int64_t value_ns_signed; | |
88 | ||
44c440bc | 89 | /* Initialize to clock class's base offset */ |
0f2d58c9 | 90 | *ns_from_origin = base_offset_ns; |
44c440bc PP |
91 | |
92 | /* Add given value in cycles */ | |
0f2d58c9 | 93 | value_ns_unsigned = bt_util_ns_from_value(frequency, value); |
44c440bc PP |
94 | if (value_ns_unsigned >= (uint64_t) INT64_MAX) { |
95 | /* | |
96 | * FIXME: `value_ns_unsigned` could be greater than | |
97 | * `INT64_MAX` in fact: in this case, we need to | |
98 | * subtract `INT64_MAX` from `value_ns_unsigned`, make | |
99 | * sure that the difference is less than `INT64_MAX`, | |
100 | * and try to add them one after the other to | |
101 | * `*ns_from_origin`. | |
102 | */ | |
103 | ret = -1; | |
104 | goto end; | |
105 | } | |
106 | ||
107 | value_ns_signed = (int64_t) value_ns_unsigned; | |
98b15851 | 108 | BT_ASSERT_DBG(value_ns_signed >= 0); |
44c440bc PP |
109 | |
110 | if (*ns_from_origin <= 0) { | |
111 | goto add_value; | |
112 | } | |
113 | ||
114 | if (value_ns_signed > INT64_MAX - *ns_from_origin) { | |
115 | ret = -1; | |
116 | goto end; | |
117 | } | |
118 | ||
119 | add_value: | |
120 | *ns_from_origin += value_ns_signed; | |
121 | ||
122 | end: | |
123 | return ret; | |
124 | } | |
125 | ||
0f2d58c9 PP |
126 | static inline |
127 | int bt_util_ns_from_origin_clock_class(const struct bt_clock_class *clock_class, | |
128 | uint64_t value, int64_t *ns_from_origin) | |
129 | { | |
130 | int ret = 0; | |
131 | ||
132 | if (clock_class->base_offset.overflows) { | |
133 | ret = -1; | |
134 | goto end; | |
135 | } | |
136 | ||
137 | ret = bt_util_ns_from_origin_inline(clock_class->base_offset.value_ns, | |
138 | clock_class->offset_seconds, clock_class->offset_cycles, | |
139 | clock_class->frequency, value, ns_from_origin); | |
140 | ||
141 | end: | |
142 | return ret; | |
143 | } | |
144 | ||
44c440bc PP |
145 | static inline |
146 | bool bt_util_value_is_in_range_signed(uint64_t size, int64_t value) | |
147 | { | |
148 | int64_t min_value = UINT64_C(-1) << (size - 1); | |
149 | int64_t max_value = (UINT64_C(1) << (size - 1)) - 1; | |
150 | return value >= min_value && value <= max_value; | |
151 | } | |
152 | ||
153 | static inline | |
154 | bool bt_util_value_is_in_range_unsigned(unsigned int size, uint64_t value) | |
155 | { | |
156 | uint64_t max_value = (size == 64) ? UINT64_MAX : | |
157 | (UINT64_C(1) << size) - 1; | |
158 | return value <= max_value; | |
159 | } | |
726106d3 | 160 | |
56e18c4c | 161 | #endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */ |