Skip to content

Commit 4ba2594

Browse files
pegasusplusZhenping Zhao
authored and
Zhenping Zhao
committed
Using builtin ctz instruction to calculate ffs, GCC/KEIL/IAR
1 parent 96a6d04 commit 4ba2594

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Kconfig

+4-4
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,17 @@ config RT_USING_CPU_USAGE_TRACER
199199

200200
choice
201201
prompt "Choose finding first bit set method"
202-
default RT_USING_TINY_FFS
202+
default RT_USING_BUILTIN_FFS
203203

204204
config RT_USING_TINY_FFS
205205
bool "Tiny: 37 bytes"
206206
help
207207
Select this if you want tiny method.
208208

209-
config RT_USING_PUNY_FFS
210-
bool "Puny: 0 bytes"
209+
config RT_USING_BUILTIN_FFS
210+
bool "Builtin: 0 bytes"
211211
help
212-
Select this if you want puny method.
212+
Select this if you want builtin method.
213213

214214
endchoice
215215
menuconfig RT_USING_DEBUG

src/kservice.c

+27-1
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,32 @@ RTM_EXPORT(rt_free_align);
10261026
#endif /* RT_USING_HEAP */
10271027

10281028
#ifndef RT_USING_CPU_FFS
1029-
#ifdef RT_USING_PUNY_FFS
1029+
#ifdef RT_USING_BUILTIN_FFS
1030+
1031+
#if defined(__GNUC__) || defined(__clang__)
1032+
#define __HAS_BUILTIN_CTZ__
1033+
#define CTZ(x) ((x) ? 1 + __builtin_ctz(x) : 0)
1034+
1035+
#elif defined(__ARMCC_VERSION) // Keil ARM Compiler
1036+
#include "arm_math.h"
1037+
#define __HAS_BUILTIN_CTZ__
1038+
#define CTZ(x) ((x) ? 1 + __CLZ(__RBIT(x)) : 0)
1039+
1040+
#elif defined(__ICCARM__) // IAR ARM Compiler
1041+
#include <intrinsics.h>
1042+
#define __HAS_BUILTIN_CTZ__
1043+
#define CTZ(x) ((x) ? 1 + __CLZ(__RBIT(x)) : 0)
1044+
1045+
#else
1046+
#message "Don't know if compiler has builtin ctz"
1047+
#endif
1048+
1049+
#ifdef __HAS_BUILTIN_CTZ__
1050+
int __rt_ffs(rt_int32_t value)
1051+
{
1052+
return CTZ(value);
1053+
}
1054+
#else
10301055
int __rt_ffs(rt_int32_t value) {
10311056
int position = 1; // position start from 1
10321057

@@ -1068,6 +1093,7 @@ int __rt_ffs(rt_int32_t value) {
10681093

10691094
return position;
10701095
}
1096+
#endif
10711097
#elif defined(RT_USING_TINY_FFS)
10721098
const rt_uint8_t __lowest_bit_bitmap[] =
10731099
{

0 commit comments

Comments
 (0)