Skip to content

Commit dfb7df4

Browse files
authored
Punny __rt_ffs() does not use extra space
1 parent 03a9729 commit dfb7df4

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/kservice.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,37 @@ RTM_EXPORT(rt_free_align);
10261026
#endif /* RT_USING_HEAP */
10271027

10281028
#ifndef RT_USING_CPU_FFS
1029-
#ifdef RT_USING_TINY_FFS
1029+
#ifdef RT_USING_PUNY_FFS
1030+
int __rt_ffs(rt_int32_t value) {
1031+
if (value == 0)
1032+
return 0; // 0 means no bit 1
1033+
1034+
int position = 1; // position start from 1
1035+
1036+
// search half range
1037+
if ((value & 0xFFFF) == 0) { // is lower 16bit 0
1038+
position += 16;
1039+
value >>= 16;
1040+
}
1041+
if ((value & 0xFF) == 0) { // is lower 8bit 0
1042+
position += 8;
1043+
value >>= 8;
1044+
}
1045+
if ((value & 0xF) == 0) { // is lower 4bit 0
1046+
position += 4;
1047+
value >>= 4;
1048+
}
1049+
if ((value & 0x3) == 0) { // is lower 2bit 0
1050+
position += 2;
1051+
value >>= 2;
1052+
}
1053+
if ((value & 0x1) == 0) { // is lower 1bit 0
1054+
position += 1;
1055+
}
1056+
1057+
return position;
1058+
}
1059+
#elif defined(RT_USING_TINY_FFS)
10301060
const rt_uint8_t __lowest_bit_bitmap[] =
10311061
{
10321062
/* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32,

0 commit comments

Comments
 (0)