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