4c1ea33f66f45feabf52c7eab02f40c5d582df14
1 // SPDX-License-Identifier: MIT
2 // SPDX-FileCopyrightText: 2024 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 #ifndef _RSEQ_COMMON_UTILS_H
5 #define _RSEQ_COMMON_UTILS_H
10 #define RSEQ_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
12 #define __rseq_align_mask(v, mask) (((v) + (mask)) & ~(mask))
13 #define rseq_align(v, align) __rseq_align_mask(v, (__typeof__(v)) (align) - 1)
16 unsigned int rseq_fls_u64(uint64_t x
)
23 if (!(x
& 0xFFFFFFFF00000000ULL
)) {
27 if (!(x
& 0xFFFF000000000000ULL
)) {
31 if (!(x
& 0xFF00000000000000ULL
)) {
35 if (!(x
& 0xF000000000000000ULL
)) {
39 if (!(x
& 0xC000000000000000ULL
)) {
43 if (!(x
& 0x8000000000000000ULL
)) {
51 unsigned int rseq_fls_u32(uint32_t x
)
57 if (!(x
& 0xFFFF0000U
)) {
61 if (!(x
& 0xFF000000U
)) {
65 if (!(x
& 0xF0000000U
)) {
69 if (!(x
& 0xC0000000U
)) {
73 if (!(x
& 0x80000000U
)) {
81 unsigned int rseq_fls_ulong(unsigned long x
)
83 #if RSEQ_BITS_PER_LONG == 32
84 return rseq_fls_u32(x
);
86 return rseq_fls_u64(x
);
91 * Return the minimum order for which x <= (1UL << order).
92 * Return -1 if x is 0.
95 int rseq_get_count_order_ulong(unsigned long x
)
100 return rseq_fls_ulong(x
- 1);
103 #define RSEQ_DEFAULT_PAGE_SIZE 4096
106 unsigned long rseq_get_page_len(void)
108 long page_len
= sysconf(_SC_PAGE_SIZE
);
111 page_len
= RSEQ_DEFAULT_PAGE_SIZE
;
112 return (unsigned long) page_len
;
116 int rseq_hweight_ulong(unsigned long v
)
118 return __builtin_popcountl(v
);
122 bool is_pow2(uint64_t x
)
124 return !(x
& (x
- 1));
128 * Calculate offset needed to align p on alignment towards higher
129 * addresses. Alignment must be a power of 2
132 off_t
offset_align(uintptr_t p
, size_t alignment
)
134 return (alignment
- p
) & (alignment
- 1);
137 #endif /* _RSEQ_COMMON_UTILS_H */
This page took 0.030959 seconds and 4 git commands to generate.