2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
8 #define BABELTRACE_TRACE_IR_UTILS_INTERNAL_H
10 #include <babeltrace2/trace-ir/field-class.h>
14 #include "clock-class.h"
22 uint64_t bt_util_ns_from_value(uint64_t frequency
, uint64_t value_cycles
)
26 if (frequency
== UINT64_C(1000000000)) {
29 double dblres
= ((1e9
* (double) value_cycles
) / (double) frequency
);
31 if (dblres
>= (double) UINT64_MAX
) {
32 /* Overflows uint64_t */
35 ns
= (uint64_t) dblres
;
43 bool bt_util_get_base_offset_ns(int64_t offset_seconds
, uint64_t offset_cycles
,
44 uint64_t frequency
, int64_t *base_offset_ns
)
46 bool overflows
= false;
47 uint64_t offset_cycles_ns
;
49 BT_ASSERT_DBG(base_offset_ns
);
51 /* Initialize nanosecond timestamp to clock's offset in seconds */
52 if (offset_seconds
<= (INT64_MIN
/ INT64_C(1000000000) - 1) ||
53 offset_seconds
>= (INT64_MAX
/ INT64_C(1000000000)) - 1) {
55 * Overflow: offset in seconds converted to nanoseconds
56 * is outside the int64_t range. We also subtract 1 here
57 * to leave "space" for the offset in cycles converted
58 * to nanoseconds (which is always less than 1 second by
65 /* Offset (seconds) to nanoseconds */
66 *base_offset_ns
= offset_seconds
* INT64_C(1000000000);
68 /* Add offset in cycles */
69 BT_ASSERT_DBG(offset_cycles
< frequency
);
70 offset_cycles_ns
= bt_util_ns_from_value(frequency
,
72 BT_ASSERT_DBG(offset_cycles_ns
< 1000000000);
73 *base_offset_ns
+= (int64_t) offset_cycles_ns
;
80 int bt_util_ns_from_origin_inline(int64_t base_offset_ns
,
81 uint64_t frequency
, uint64_t value
, int64_t *ns_from_origin
)
84 uint64_t value_ns_unsigned
;
85 int64_t value_ns_signed
;
87 /* Initialize to clock class's base offset */
88 *ns_from_origin
= base_offset_ns
;
90 /* Add given value in cycles */
91 value_ns_unsigned
= bt_util_ns_from_value(frequency
, value
);
92 if (value_ns_unsigned
>= (uint64_t) INT64_MAX
) {
94 * FIXME: `value_ns_unsigned` could be greater than
95 * `INT64_MAX` in fact: in this case, we need to
96 * subtract `INT64_MAX` from `value_ns_unsigned`, make
97 * sure that the difference is less than `INT64_MAX`,
98 * and try to add them one after the other to
105 value_ns_signed
= (int64_t) value_ns_unsigned
;
106 BT_ASSERT_DBG(value_ns_signed
>= 0);
108 if (*ns_from_origin
<= 0) {
112 if (value_ns_signed
> INT64_MAX
- *ns_from_origin
) {
118 *ns_from_origin
+= value_ns_signed
;
125 int bt_util_ns_from_origin_clock_class(const struct bt_clock_class
*clock_class
,
126 uint64_t value
, int64_t *ns_from_origin
)
130 if (clock_class
->base_offset
.overflows
) {
135 ret
= bt_util_ns_from_origin_inline(clock_class
->base_offset
.value_ns
,
136 clock_class
->frequency
, value
, ns_from_origin
);
143 bool bt_util_value_is_in_range_signed(uint64_t size
, int64_t value
)
145 int64_t min_value
= UINT64_C(-1) << (size
- 1);
146 int64_t max_value
= (UINT64_C(1) << (size
- 1)) - 1;
147 return value
>= min_value
&& value
<= max_value
;
151 bool bt_util_value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
153 uint64_t max_value
= (size
== 64) ? UINT64_MAX
:
154 (UINT64_C(1) << size
) - 1;
155 return value
<= max_value
;
158 #endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.032926 seconds and 4 git commands to generate.