Branch data Line data Source code
1 : : /*
2 : : * SPDX-FileCopyrightText: Copyright (c) 2026 Silicon Laboratories Inc.
3 : : * SPDX-License-Identifier: Apache-2.0
4 : : */
5 : :
6 : : #include <limits.h>
7 : : #include <stddef.h>
8 : : #include <stdint.h>
9 : :
10 : : #include <zephyr/sys/bitmap.h>
11 : : #include <zephyr/sys/byteorder.h>
12 : : #include <zephyr/sys/math_extras.h>
13 : : #include <zephyr/sys/util.h>
14 : :
15 : : #if ULONG_MAX > UINT32_MAX
16 : : #define le_ulong_to_cpu(w) sys_le64_to_cpu(w)
17 : : #define ulong_count_trailing_zeros(w) u64_count_trailing_zeros(w)
18 : : #else
19 : : #define le_ulong_to_cpu(w) sys_le32_to_cpu(w)
20 : : #define ulong_count_trailing_zeros(w) u32_count_trailing_zeros(w)
21 : : #endif
22 : :
23 : : /* A match in the unused bits of the last byte is no match. */
24 : 0 : static size_t match(size_t byte, unsigned int bit, size_t num_bits)
25 : : {
26 : 0 : return MIN((byte * BITS_PER_BYTE) + bit, num_bits);
27 : : }
28 : :
29 : : /* Searches for a cleared bit by inverting every byte with invert = 0xff. */
30 : 0 : static size_t find_next(const uint8_t *bitmap, size_t num_bits, size_t start, uint8_t invert)
31 : : {
32 : 0 : const size_t num_bytes = SYS_BITMAP_SIZE(num_bits);
33 [ # # ]: 0 : const unsigned long word_invert = (invert != 0U) ? ~0UL : 0UL;
34 : 0 : size_t byte;
35 : 0 : uint8_t bits;
36 : :
37 [ # # ]: 0 : if (start >= num_bits) {
38 : : return num_bits;
39 : : }
40 : :
41 : 0 : byte = start / BITS_PER_BYTE;
42 : 0 : bits = (uint8_t)((bitmap[byte] ^ invert) & (UINT8_MAX << (start % BITS_PER_BYTE)));
43 [ # # ]: 0 : if (bits != 0U) {
44 : 0 : return match(byte, u32_count_trailing_zeros(bits), num_bits);
45 : : }
46 : :
47 : : /* Bytes up to a word boundary */
48 [ # # # # ]: 0 : for (byte++; (byte < num_bytes) && !IS_ALIGNED(&bitmap[byte], sizeof(unsigned long));
49 : 0 : byte++) {
50 : 0 : bits = (uint8_t)(bitmap[byte] ^ invert);
51 [ # # ]: 0 : if (bits != 0U) {
52 : 0 : return match(byte, u32_count_trailing_zeros(bits), num_bits);
53 : : }
54 : : }
55 : :
56 : : /* Whole words: bit n of a little-endian word is bit n % 8 of byte n / 8 */
57 [ # # ]: 0 : for (; (num_bytes - byte) >= sizeof(unsigned long); byte += sizeof(unsigned long)) {
58 : 0 : unsigned long word =
59 : 0 : le_ulong_to_cpu(*(const unsigned long *)&bitmap[byte]) ^ word_invert;
60 : :
61 [ # # ]: 0 : if (word != 0UL) {
62 : 0 : return match(byte, ulong_count_trailing_zeros(word), num_bits);
63 : : }
64 : : }
65 : :
66 : : /* Remaining bytes */
67 [ # # ]: 0 : for (; byte < num_bytes; byte++) {
68 : 0 : bits = (uint8_t)(bitmap[byte] ^ invert);
69 [ # # ]: 0 : if (bits != 0U) {
70 : 0 : return match(byte, u32_count_trailing_zeros(bits), num_bits);
71 : : }
72 : : }
73 : :
74 : : return num_bits;
75 : : }
76 : :
77 : 0 : size_t sys_bitmap_find_next_set(const uint8_t *bitmap, size_t num_bits, size_t start)
78 : : {
79 : 0 : return find_next(bitmap, num_bits, start, 0U);
80 : : }
81 : :
82 : 0 : size_t sys_bitmap_find_next_clear(const uint8_t *bitmap, size_t num_bits, size_t start)
83 : : {
84 : 0 : return find_next(bitmap, num_bits, start, UINT8_MAX);
85 : : }
86 : :
87 : 0 : size_t sys_bitmap_count_set(const uint8_t *bitmap, size_t num_bits)
88 : : {
89 : 0 : size_t full_bytes = num_bits / BITS_PER_BYTE;
90 : 0 : size_t count = sys_count_bits(bitmap, full_bytes);
91 : :
92 [ # # ]: 0 : if ((num_bits % BITS_PER_BYTE) != 0U) {
93 : 0 : uint8_t tail = bitmap[full_bytes] & (uint8_t)BIT_MASK(num_bits % BITS_PER_BYTE);
94 : :
95 : 0 : count += sys_count_bits(&tail, sizeof(tail));
96 : : }
97 : :
98 : 0 : return count;
99 : : }
|