ffs.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef _BLE_MESH_FFS_H_
  8. #define _BLE_MESH_FFS_H_
  9. #include "mesh/types.h"
  10. #include "mesh/compiler.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. *
  16. * @brief find most significant bit set in a 32-bit word
  17. *
  18. * This routine finds the first bit set starting from the most significant bit
  19. * in the argument passed in and returns the index of that bit. Bits are
  20. * numbered starting at 1 from the least significant bit. A return value of
  21. * zero indicates that the value passed is zero.
  22. *
  23. * @return most significant bit set, 0 if @a op is 0
  24. */
  25. static ALWAYS_INLINE unsigned int find_msb_set(uint32_t op)
  26. {
  27. if (op == 0) {
  28. return 0;
  29. }
  30. return 32 - __builtin_clz(op);
  31. }
  32. /**
  33. *
  34. * @brief find least significant bit set in a 32-bit word
  35. *
  36. * This routine finds the first bit set starting from the least significant bit
  37. * in the argument passed in and returns the index of that bit. Bits are
  38. * numbered starting at 1 from the least significant bit. A return value of
  39. * zero indicates that the value passed is zero.
  40. *
  41. * @return least significant bit set, 0 if @a op is 0
  42. */
  43. static ALWAYS_INLINE unsigned int find_lsb_set(uint32_t op)
  44. {
  45. return __builtin_ffs(op);
  46. }
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #endif /* _BLE_MESH_FFS_H_ */