1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef NSI_COMMON_SRC_NSI_INTERNAL_H
8 #define NSI_COMMON_SRC_NSI_INTERNAL_H
9 
10 #include <stdint.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /**
17  * @brief find least significant bit set in a 32-bit word
18  *
19  * This routine finds the first bit set starting from the least significant bit
20  * in the argument passed in and returns the index of that bit. Bits are
21  * numbered starting at 1 from the least significant bit.  A return value of
22  * zero indicates that the value passed is zero.
23  *
24  * @return least significant bit set, 0 if @a op is 0
25  */
26 
nsi_find_lsb_set(uint32_t op)27 static inline unsigned int nsi_find_lsb_set(uint32_t op)
28 {
29 	return __builtin_ffs(op);
30 }
31 
32 /**
33  * @brief find least significant bit set in a 64-bit word
34  *
35  * This routine finds the first bit set starting from the least significant bit
36  * in the argument passed in and returns the index of that bit. Bits are
37  * numbered starting at 1 from the least significant bit.  A return value of
38  * zero indicates that the value passed is zero.
39  *
40  * @return least significant bit set, 0 if @a op is 0
41  */
42 
nsi_find_lsb_set64(uint64_t op)43 static inline unsigned int nsi_find_lsb_set64(uint64_t op)
44 {
45 	return __builtin_ffsll(op);
46 }
47 
48 #ifdef __cplusplus
49 }
50 #endif
51 
52 #endif /* NSI_COMMON_SRC_NSI_INTERNAL_H */
53