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 "common/macros.h"
11 #include <babeltrace2/trace-ir/field-class.h>
15 #include "clock-class.h"
23 uint64_t bt_util_ns_from_value(uint64_t frequency
, uint64_t value_cycles
)
27 if (frequency
== UINT64_C(1000000000)) {
30 double dblres
= ((1e9
* (double) value_cycles
) / (double) frequency
);
32 if (dblres
>= (double) UINT64_MAX
) {
33 /* Overflows uint64_t */
36 ns
= (uint64_t) dblres
;
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
)
47 bool overflows
= false;
48 uint64_t offset_cycles_ns
;
50 BT_ASSERT_DBG(base_offset_ns
);
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) {
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
66 /* Offset (seconds) to nanoseconds */
67 *base_offset_ns
= offset_seconds
* INT64_C(1000000000);
69 /* Add offset in cycles */
70 BT_ASSERT_DBG(offset_cycles
< frequency
);
71 offset_cycles_ns
= bt_util_ns_from_value(frequency
,
73 BT_ASSERT_DBG(offset_cycles_ns
< 1000000000);
74 *base_offset_ns
+= (int64_t) offset_cycles_ns
;
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
)
86 uint64_t value_ns_unsigned
;
87 int64_t value_ns_signed
;
89 /* Initialize to clock class's base offset */
90 *ns_from_origin
= base_offset_ns
;
92 /* Add given value in cycles */
93 value_ns_unsigned
= bt_util_ns_from_value(frequency
, value
);
94 if (value_ns_unsigned
>= (uint64_t) INT64_MAX
) {
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
107 value_ns_signed
= (int64_t) value_ns_unsigned
;
108 BT_ASSERT_DBG(value_ns_signed
>= 0);
110 if (*ns_from_origin
<= 0) {
114 if (value_ns_signed
> INT64_MAX
- *ns_from_origin
) {
120 *ns_from_origin
+= value_ns_signed
;
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
)
132 if (clock_class
->base_offset
.overflows
) {
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
);
146 bool bt_util_value_is_in_range_signed(uint64_t size
, int64_t value
)
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
;
154 bool bt_util_value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
156 uint64_t max_value
= (size
== 64) ? UINT64_MAX
:
157 (UINT64_C(1) << size
) - 1;
158 return value
<= max_value
;
161 #endif /* BABELTRACE_TRACE_IR_UTILS_INTERNAL_H */
This page took 0.032199 seconds and 4 git commands to generate.