1 /***************************************************************************//**
2  * @file
3  * @brief Implementation of bit operations.
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2019 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #ifndef SL_BIT_H
32 #define SL_BIT_H
33 
34 /***************************************************************************//**
35  * @addtogroup bit Bit Manipulation
36  * @brief Bitwise operations
37  * @{
38  ******************************************************************************/
39 
40 /****************************************************************************************************//**
41  *                                               SL_DEF_BIT()
42  *
43  * @brief    Create bit mask with single, specified bit set.
44  *
45  * @param    bit     Bit number of bit to set.
46  *
47  * @return   Bit mask with single, specified bit set.
48  *
49  * @note     (1) 'bit' SHOULD be a non-negative integer.
50  *
51  * @note     (2) 'bit' values that overflow the target CPU &/or compiler environment (e.g. negative
52  *               or greater-than-CPU-data-size values) MAY generate compiler warnings &/or errors.
53  *******************************************************************************************************/
54 
55 #define  SL_DEF_BIT(bit)            (1u << (bit))
56 
57 /****************************************************************************************************//**
58  *                                               SL_SET_BIT()
59  *
60  * @brief    Set specified bit(s) in a value.
61  *
62  * @param    val     Value to modify by setting specified bit(s).
63  *
64  * @param    mask    Mask of bits to set.
65  *
66  * @return   Modified value with specified bit(s) set.
67  *
68  * @note     'val' & 'mask' SHOULD be unsigned integers.
69  *******************************************************************************************************/
70 
71 #define SL_SET_BIT(val, mask)                      ((val) = ((val) | (mask)))
72 
73 /****************************************************************************************************//**
74  *                                               SL_CLEAR_BIT()
75  *
76  * @brief    Clear specified bit(s) in a value.
77  *
78  * @param    val     Value to modify by clearing specified bit(s).
79  *
80  * @param    mask    Mask of bits to clear.
81  *
82  * @return   Modified value with specified bit(s) clear.
83  *
84  * @note     'val' & 'mask' SHOULD be unsigned integers.
85  *
86  * @note     'mask' SHOULD be cast with the same data type than 'val'.
87  *******************************************************************************************************/
88 
89 #define SL_CLEAR_BIT(val, mask)  ((val) = ((val) & (~(mask))))
90 
91 /****************************************************************************************************//**
92  *                                               SL_IS_BIT_SET()
93  *
94  * @brief    Determine whether the specified bit(s) in a value are set.
95  *
96  * @param    val     Value to check for specified bit(s) set.
97  *
98  * @param    mask    Mask of bits to check if set.
99  *
100  * @return   true,  if ALL specified bit(s) are set in value.
101  *
102  *           false, if ALL specified bit(s) are NOT set in value.
103  *
104  * @note     'val' & 'mask' SHOULD be unsigned integers.
105  *
106  * @note     NULL 'mask' allowed; returns 'false' since NO mask bits specified.
107  *******************************************************************************************************/
108 
109 #define SL_IS_BIT_SET(val, mask)  (((((val) & (mask)) == (mask)) && ((mask) != 0u)) ? (true) : (false))
110 
111 /****************************************************************************************************//**
112  *                                               SL_IS_BIT_CLEAR()
113  *
114  * @brief    Determine whether the specified bit(s) in a value are clear.
115  *
116  * @param    val     Value to check for specified bit(s) clear.
117  *
118  * @param    mask    Mask of bits to check if clear.
119  *
120  * @return   true,  if ALL specified bit(s) are clear in value.
121  *
122  *           false, if ALL specified bit(s) are NOT clear in value.
123  *
124  * @note     val' & 'mask' SHOULD be unsigned integers.
125  *
126  * @note     NULL 'mask' allowed; returns 'false' since NO mask bits specified.
127  *******************************************************************************************************/
128 #define SL_IS_BIT_CLEAR(val, mask)  (((((val) & (mask)) == 0u) && ((mask) != 0u)) ? (true) : (false))
129 
130 /****************************************************************************************************//**
131  *                                           SL_IS_ANY_BIT_SET()
132  *
133  * @brief    Determine whether any specified bit(s) in a value are set.
134  *
135  * @param    val     Value to check for specified bit(s) set.
136  *
137  * @param    mask    Mask of bits to check if set (see Note #2).
138  *
139  * @return   true,  if ANY specified bit(s) are set in value.
140  *
141  *           false, if ALL specified bit(s) are NOT set in value.
142  *
143  * @note     'val' & 'mask' SHOULD be unsigned integers.
144  *
145  * @note     NULL 'mask' allowed; returns 'false' since NO mask bits specified.
146  *******************************************************************************************************/
147 
148 #define  SL_IS_ANY_BIT_SET(val, mask)  ((((val) & (mask)) == 0u) ? (false) : (true))
149 
150 /****************************************************************************************************//**
151  *                                           SL_IS_ANY_BIT_CLEAR()
152  *
153  * @brief    Determine whether any specified bit(s) in a value are clear.
154  *
155  * @param    val     Value to check for specified bit(s) clear.
156  *
157  * @param    mask    Mask of bits to check if clear (see Note #2).
158  *
159  * @return   true,   if ANY specified bit(s) are clear in value.
160  *
161  *           false,  if ALL specified bit(s) are NOT clear in value.
162  *
163  * @note     'val' & 'mask' SHOULD be unsigned integers.
164  *
165  * @note     NULL 'mask' allowed; returns 'false' since NO mask bits specified.
166  *******************************************************************************************************/
167 
168 #define  SL_IS_ANY_BIT_CLEAR(val, mask)  ((((val) & (mask)) == (mask))  ? (false) : (true))
169 
170 /****************************************************************************************************//**
171  *                                            SL_MATH_IS_PWR2()
172  *
173  * @brief    Determine if a value is a power of 2.
174  *
175  * @param    val     Value.
176  *
177  * @return   true,  'val' is a power of 2.
178  *           false, 'val' is not a power of 2.
179  *******************************************************************************************************/
180 
181 #define  SL_MATH_IS_PWR2(val)   ((((val) != 0u) && (((val) & ((val) - 1u)) == 0u)) ? true : false)
182 
183 /*******************************************************************************
184  ******************************   DEFINES   ************************************
185  ******************************************************************************/
186 
187 /** @} (end addtogroup bit) */
188 
189 #endif /* SL_BIT_H */
190