1 /*
2  * Copyright (c) 2016 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRF_BITMASK_H
35 #define NRF_BITMASK_H
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_bitmask Bitmask module
45  * @{
46  * @ingroup nrfx
47  * @brief   Bitmask managing module.
48  */
49 
50 /**
51  * @brief Get number of bytes needed to store the given bitmask.
52  *
53  * @param[in] bits_count Number of bits in the bitmask.
54  *
55  * @return Number of bytes to store requested bit mask.
56  */
57 #define BITMASK_BYTES_CALCULATE(bits_count) NRFX_CEIL_DIV(bits_count, 8)
58 
59 /** @brief Macro for getting index of byte in byte stream where @c abs_bit is put. */
60 #define BITMASK_BYTE_GET(abs_bit) ((abs_bit)/8)
61 
62 /** @brief Macro for getting relative index of bit in byte. */
63 #define BITMASK_RELBIT_GET(abs_bit) ((abs_bit) & 0x7UL)
64 
65 /**
66  * @brief Function for checking if bit in the multi-byte bit mask is set.
67  *
68  * @param[in] bit    Bit index.
69  * @param[in] p_mask Pointer to mask with bit fields.
70  *
71  * @retval true  If the specified bit is set.
72  * @retval false If the specified bit is cleared.
73  */
nrf_bitmask_bit_is_set(uint32_t bit,void const * p_mask)74 __STATIC_INLINE bool nrf_bitmask_bit_is_set(uint32_t bit, void const * p_mask)
75 {
76     uint8_t const * p_mask8 = (uint8_t const *)p_mask;
77     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
78     bit = BITMASK_RELBIT_GET(bit);
79     return ((1U << bit) & p_mask8[byte_idx]) != 0U;
80 }
81 
82 /**
83  * @brief Function for setting a bit in the multi-byte bit mask.
84  *
85  * @param[in]     bit    Bit index.
86  * @param[in,out] p_mask Pointer to mask with bit fields.
87  */
nrf_bitmask_bit_set(uint32_t bit,void * p_mask)88 __STATIC_INLINE void nrf_bitmask_bit_set(uint32_t bit, void * p_mask)
89 {
90     uint8_t * p_mask8 = (uint8_t *)p_mask;
91     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
92     bit = BITMASK_RELBIT_GET(bit);
93     p_mask8[byte_idx] |= (uint8_t)(1U << bit);
94 }
95 
96 /**
97  * @brief Function for clearing a bit in the multi-byte bit mask.
98  *
99  * @param[in]     bit    Bit index.
100  * @param[in,out] p_mask Pointer to mask with bit fields.
101  */
nrf_bitmask_bit_clear(uint32_t bit,void * p_mask)102 __STATIC_INLINE void nrf_bitmask_bit_clear(uint32_t bit, void * p_mask)
103 {
104     uint8_t * p_mask8 = (uint8_t *)p_mask;
105     uint32_t byte_idx = BITMASK_BYTE_GET(bit);
106     bit = BITMASK_RELBIT_GET(bit);
107     p_mask8[byte_idx] &= (uint8_t)~(1U << bit);
108 }
109 
110 /**
111  * @brief Function for performing bitwise OR operation on two multi-byte bit masks.
112  *
113  * @param[in]  p_mask1    Pointer to the first bit mask.
114  * @param[in]  p_mask2    Pointer to the second bit mask.
115  * @param[out] p_out_mask Pointer to the output bit mask.
116  * @param[in]  length     Length of output mask in bytes.
117  */
nrf_bitmask_masks_or(void const * p_mask1,void const * p_mask2,void * p_out_mask,size_t length)118 __STATIC_INLINE void nrf_bitmask_masks_or(void const * p_mask1,
119                                           void const * p_mask2,
120                                           void *       p_out_mask,
121                                           size_t       length)
122 {
123     uint8_t const * p_mask8_1 = (uint8_t const *)p_mask1;
124     uint8_t const * p_mask8_2 = (uint8_t const *)p_mask2;
125     uint8_t * p_mask8_out = (uint8_t *)p_out_mask;
126     for (size_t i = 0; i < length; i++)
127     {
128         p_mask8_out[i] = p_mask8_1[i] | p_mask8_2[i];
129     }
130 }
131 
132 /**
133  * @brief Function for performing bitwise AND operation on two multi-byte bit masks.
134  *
135  * @param[in]  p_mask1    Pointer to the first bit mask.
136  * @param[in]  p_mask2    Pointer to the second bit mask.
137  * @param[out] p_out_mask Pointer to the output bit mask.
138  * @param[in]  length     Length of output mask in bytes.
139  */
nrf_bitmask_masks_and(void const * p_mask1,void const * p_mask2,void * p_out_mask,size_t length)140 __STATIC_INLINE void nrf_bitmask_masks_and(void const * p_mask1,
141                                            void const * p_mask2,
142                                            void *       p_out_mask,
143                                            size_t       length)
144 {
145     uint8_t const * p_mask8_1 = (uint8_t const *)p_mask1;
146     uint8_t const * p_mask8_2 = (uint8_t const *)p_mask2;
147     uint8_t * p_mask8_out = (uint8_t *)p_out_mask;
148     for (size_t i = 0; i < length; i++)
149     {
150         p_mask8_out[i] = p_mask8_1[i] & p_mask8_2[i];
151     }
152 }
153 
154 /** @} */
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif // NRF_BITMASK_H
161