1 /*
2 * SPDX-FileCopyrightText: 2015 Wind River Systems, Inc.
3 * SPDX-FileCopyrightText: 2017 Oticon A/S
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #ifndef _BLE_MESH_FFS_H_
9 #define _BLE_MESH_FFS_H_
10
11 #include "mesh_types.h"
12 #include "mesh_compiler.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 /**
19 *
20 * @brief find most significant bit set in a 32-bit word
21 *
22 * This routine finds the first bit set starting from the most significant bit
23 * in the argument passed in and returns the index of that bit. Bits are
24 * numbered starting at 1 from the least significant bit. A return value of
25 * zero indicates that the value passed is zero.
26 *
27 * @return most significant bit set, 0 if @a op is 0
28 */
29
find_msb_set(uint32_t op)30 static ALWAYS_INLINE unsigned int find_msb_set(uint32_t op)
31 {
32 if (op == 0) {
33 return 0;
34 }
35
36 return 32 - __builtin_clz(op);
37 }
38
39 /**
40 *
41 * @brief find least significant bit set in a 32-bit word
42 *
43 * This routine finds the first bit set starting from the least significant bit
44 * in the argument passed in and returns the index of that bit. Bits are
45 * numbered starting at 1 from the least significant bit. A return value of
46 * zero indicates that the value passed is zero.
47 *
48 * @return least significant bit set, 0 if @a op is 0
49 */
50
find_lsb_set(uint32_t op)51 static ALWAYS_INLINE unsigned int find_lsb_set(uint32_t op)
52 {
53 return __builtin_ffs(op);
54 }
55
56 #ifdef __cplusplus
57 }
58 #endif
59
60 #endif /* _BLE_MESH_FFS_H_ */
61