Skip to content

Commit

Permalink
Using builtin ctz instruction to calculate ffs, GCC/KEIL/IAR
Browse files Browse the repository at this point in the history
  • Loading branch information
pegasusplus authored and Zhenping Zhao committed Dec 2, 2024
1 parent 96a6d04 commit 4ba2594
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ config RT_USING_CPU_USAGE_TRACER

choice
prompt "Choose finding first bit set method"
default RT_USING_TINY_FFS
default RT_USING_BUILTIN_FFS

config RT_USING_TINY_FFS
bool "Tiny: 37 bytes"
help
Select this if you want tiny method.

config RT_USING_PUNY_FFS
bool "Puny: 0 bytes"
config RT_USING_BUILTIN_FFS
bool "Builtin: 0 bytes"
help
Select this if you want puny method.
Select this if you want builtin method.

endchoice
menuconfig RT_USING_DEBUG
Expand Down
28 changes: 27 additions & 1 deletion src/kservice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,32 @@ RTM_EXPORT(rt_free_align);
#endif /* RT_USING_HEAP */

#ifndef RT_USING_CPU_FFS
#ifdef RT_USING_PUNY_FFS
#ifdef RT_USING_BUILTIN_FFS

#if defined(__GNUC__) || defined(__clang__)
#define __HAS_BUILTIN_CTZ__
#define CTZ(x) ((x) ? 1 + __builtin_ctz(x) : 0)

#elif defined(__ARMCC_VERSION) // Keil ARM Compiler
#include "arm_math.h"
#define __HAS_BUILTIN_CTZ__
#define CTZ(x) ((x) ? 1 + __CLZ(__RBIT(x)) : 0)

#elif defined(__ICCARM__) // IAR ARM Compiler
#include <intrinsics.h>
#define __HAS_BUILTIN_CTZ__
#define CTZ(x) ((x) ? 1 + __CLZ(__RBIT(x)) : 0)

#else
#message "Don't know if compiler has builtin ctz"
#endif

#ifdef __HAS_BUILTIN_CTZ__
int __rt_ffs(rt_int32_t value)
{
return CTZ(value);
}
#else
int __rt_ffs(rt_int32_t value) {
int position = 1; // position start from 1

Expand Down Expand Up @@ -1068,6 +1093,7 @@ int __rt_ffs(rt_int32_t value) {

return position;
}
#endif
#elif defined(RT_USING_TINY_FFS)
const rt_uint8_t __lowest_bit_bitmap[] =
{
Expand Down

0 comments on commit 4ba2594

Please sign in to comment.