1 #ifndef BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
2 #define BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/trace-ir/field-classes.h>
26 #include <babeltrace/trace-ir/clock-class-internal.h>
35 uint64_t bt_util_ns_from_value(uint64_t frequency
, uint64_t value_cycles
)
39 if (frequency
== UINT64_C(1000000000)) {
42 double dblres
= ((1e9
* (double) value_cycles
) / (double) frequency
);
44 if (dblres
>= (double) UINT64_MAX
) {
45 /* Overflows uint64_t */
48 ns
= (uint64_t) dblres
;
56 int bt_util_ns_from_origin(struct bt_clock_class
*clock_class
, uint64_t value
,
57 int64_t *ns_from_origin
)
60 uint64_t value_ns_unsigned
;
61 int64_t value_ns_signed
;
63 if (clock_class
->base_offset
.overflows
) {
68 /* Initialize to clock class's base offset */
69 *ns_from_origin
= clock_class
->base_offset
.value_ns
;
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
) {
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
86 value_ns_signed
= (int64_t) value_ns_unsigned
;
87 BT_ASSERT(value_ns_signed
>= 0);
89 if (*ns_from_origin
<= 0) {
93 if (value_ns_signed
> INT64_MAX
- *ns_from_origin
) {
99 *ns_from_origin
+= value_ns_signed
;
106 bool bt_util_value_is_in_range_signed(uint64_t size
, int64_t value
)
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
;
114 bool bt_util_value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
116 uint64_t max_value
= (size
== 64) ? UINT64_MAX
:
117 (UINT64_C(1) << size
) - 1;
118 return value
<= max_value
;
121 #endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.043935 seconds and 4 git commands to generate.