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 #include "nsi_utils.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /**
18  * @brief find least significant bit set in a 32-bit word
19  *
20  * This routine finds the first bit set starting from the least significant bit
21  * in the argument passed in and returns the index of that bit. Bits are
22  * numbered starting at 1 from the least significant bit.  A return value of
23  * zero indicates that the value passed is zero.
24  *
25  * @return least significant bit set, 0 if @a op is 0
26  */
nsi_find_lsb_set(uint32_t op)27 NSI_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  */
nsi_find_lsb_set64(uint64_t op)42 NSI_INLINE unsigned int nsi_find_lsb_set64(uint64_t op)
43 {
44 	return __builtin_ffsll(op);
45 }
46 
47 #ifdef __cplusplus
48 }
49 #endif
50 
51 #endif /* NSI_COMMON_SRC_NSI_INTERNAL_H */
52