From dfb7df4962cadd9bab96f7ad9e7e46d205e8343c Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sat, 30 Nov 2024 12:18:06 -0800 Subject: [PATCH 01/10] Punny __rt_ffs() does not use extra space --- src/kservice.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/kservice.c b/src/kservice.c index 5a59345b3e6..2abea04ffdc 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1026,7 +1026,37 @@ RTM_EXPORT(rt_free_align); #endif /* RT_USING_HEAP */ #ifndef RT_USING_CPU_FFS -#ifdef RT_USING_TINY_FFS +#ifdef RT_USING_PUNY_FFS +int __rt_ffs(rt_int32_t value) { + if (value == 0) + return 0; // 0 means no bit 1 + + int position = 1; // position start from 1 + + // search half range + if ((value & 0xFFFF) == 0) { // is lower 16bit 0 + position += 16; + value >>= 16; + } + if ((value & 0xFF) == 0) { // is lower 8bit 0 + position += 8; + value >>= 8; + } + if ((value & 0xF) == 0) { // is lower 4bit 0 + position += 4; + value >>= 4; + } + if ((value & 0x3) == 0) { // is lower 2bit 0 + position += 2; + value >>= 2; + } + if ((value & 0x1) == 0) { // is lower 1bit 0 + position += 1; + } + + return position; +} +#elif defined(RT_USING_TINY_FFS) const rt_uint8_t __lowest_bit_bitmap[] = { /* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32, From ef1fd2cf4be62958b5497425961f567b766dcb88 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sat, 30 Nov 2024 12:31:29 -0800 Subject: [PATCH 02/10] Update Kconfig for puny __rt_ffs() --- src/Kconfig | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Kconfig b/src/Kconfig index e7f2a5108b6..3441132f346 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -197,12 +197,21 @@ config RT_USING_CPU_USAGE_TRACER default y if RT_USING_SMART default n -menu "kservice options" - config RT_USING_TINY_FFS - bool "Enable kservice to use tiny finding first bit set method" - default n -endmenu +choice + prompt "Choose finding first bit set method" + default RT_USING_TINY_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" + help + Select this if you want pyny method. +endchoice menuconfig RT_USING_DEBUG bool "Enable debugging features" default y From 9fd71b3c4980165c4d258c4ab19a80ccf3d87473 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sat, 30 Nov 2024 13:03:07 -0800 Subject: [PATCH 03/10] Format code --- src/kservice.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/kservice.c b/src/kservice.c index 2abea04ffdc..d2d9796fdba 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1028,29 +1028,41 @@ RTM_EXPORT(rt_free_align); #ifndef RT_USING_CPU_FFS #ifdef RT_USING_PUNY_FFS int __rt_ffs(rt_int32_t value) { + int position = 1; // position start from 1 + if (value == 0) + { return 0; // 0 means no bit 1 - - int position = 1; // position start from 1 - + } + // search half range - if ((value & 0xFFFF) == 0) { // is lower 16bit 0 + if ((value & 0xFFFF) == 0) + { + // is lower 16bit 0 position += 16; value >>= 16; } - if ((value & 0xFF) == 0) { // is lower 8bit 0 + if ((value & 0xFF) == 0) + { + // is lower 8bit 0 position += 8; value >>= 8; } - if ((value & 0xF) == 0) { // is lower 4bit 0 + if ((value & 0xF) == 0) + { + // is lower 4bit 0 position += 4; value >>= 4; } - if ((value & 0x3) == 0) { // is lower 2bit 0 + if ((value & 0x3) == 0) + { + // is lower 2bit 0 position += 2; value >>= 2; } - if ((value & 0x1) == 0) { // is lower 1bit 0 + if ((value & 0x1) == 0) + { + // is lower 1bit 0 position += 1; } From 60f5f0e628341122a1e10b81b373fb0263deb979 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sat, 30 Nov 2024 13:05:37 -0800 Subject: [PATCH 04/10] Update manual_dist.yml to default select RT_USING_PUNY_FFS --- .github/workflows/manual_dist.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/manual_dist.yml b/.github/workflows/manual_dist.yml index c2cf7910e90..75b36e57dc6 100644 --- a/.github/workflows/manual_dist.yml +++ b/.github/workflows/manual_dist.yml @@ -38,7 +38,7 @@ on: description: 'Type a config you want mannual test in .config, like: CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' required: false type: string - default: 'CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' + default: 'RT_USING_TINY_FFS=n,RT_USING_PUNY_FFS=y,CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' dist_flag: description: 'True to dist all bsp, False not dist' required: true From a063f50568d59d5cc61e45b4216b5a1f04d14ecd Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sat, 30 Nov 2024 21:28:08 -0800 Subject: [PATCH 05/10] Format code --- src/kservice.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kservice.c b/src/kservice.c index d2d9796fdba..11f6202eef6 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1029,12 +1029,12 @@ RTM_EXPORT(rt_free_align); #ifdef RT_USING_PUNY_FFS int __rt_ffs(rt_int32_t value) { int position = 1; // position start from 1 - + if (value == 0) { return 0; // 0 means no bit 1 } - + // search half range if ((value & 0xFFFF) == 0) { From 0e9c6c830f7c2bb162ac285bc144fd7efeda2441 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sun, 1 Dec 2024 00:48:47 -0800 Subject: [PATCH 06/10] Fix legacy __rt_ffs_tiny() Some compiler complains value & (value - 1) ^ value better (value & (value - 1)) ^ value --- src/kservice.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kservice.c b/src/kservice.c index 11f6202eef6..b1c275e079e 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1092,7 +1092,7 @@ const rt_uint8_t __lowest_bit_bitmap[] = */ int __rt_ffs(int value) { - return __lowest_bit_bitmap[(rt_uint32_t)(value & (value - 1) ^ value) % 37]; + return __lowest_bit_bitmap[(rt_uint32_t)((value & (value - 1)) ^ value) % 37]; } #else const rt_uint8_t __lowest_bit_bitmap[] = From 96a6d043170aea49a223a468a2fb2814ea1cc9be Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sun, 1 Dec 2024 03:53:33 -0800 Subject: [PATCH 07/10] typo --- src/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kconfig b/src/Kconfig index 3441132f346..75b223aa187 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -209,7 +209,7 @@ config RT_USING_TINY_FFS config RT_USING_PUNY_FFS bool "Puny: 0 bytes" help - Select this if you want pyny method. + Select this if you want puny method. endchoice menuconfig RT_USING_DEBUG From 4ba25940ea4a6c75d2a2f82c498d5f4e87ed7858 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Sun, 1 Dec 2024 21:55:03 -0800 Subject: [PATCH 08/10] Using builtin ctz instruction to calculate ffs, GCC/KEIL/IAR --- src/Kconfig | 8 ++++---- src/kservice.c | 28 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Kconfig b/src/Kconfig index 75b223aa187..a3b2ef90408 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -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 diff --git a/src/kservice.c b/src/kservice.c index b1c275e079e..7114bab22ee 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -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 +#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 @@ -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[] = { From 1118c9611b9400cbbacf59a6c8f3935ba642a215 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Mon, 2 Dec 2024 12:15:12 -0800 Subject: [PATCH 09/10] rt_int32_t -> int --- src/kservice.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kservice.c b/src/kservice.c index 7114bab22ee..e1e1c0e9c53 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1047,12 +1047,12 @@ RTM_EXPORT(rt_free_align); #endif #ifdef __HAS_BUILTIN_CTZ__ -int __rt_ffs(rt_int32_t value) +int __rt_ffs(int value) { return CTZ(value); } #else -int __rt_ffs(rt_int32_t value) { +int __rt_ffs(int value) { int position = 1; // position start from 1 if (value == 0) From e41a3af73a722ac6b8dc8c5e6cd136ca5ff32129 Mon Sep 17 00:00:00 2001 From: Zhao Zhenping <1927818778@qq.com> Date: Mon, 2 Dec 2024 22:01:35 -0800 Subject: [PATCH 10/10] RevokeRevoke manual_dist.yml --- .github/workflows/manual_dist.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/manual_dist.yml b/.github/workflows/manual_dist.yml index 75b36e57dc6..c2cf7910e90 100644 --- a/.github/workflows/manual_dist.yml +++ b/.github/workflows/manual_dist.yml @@ -38,7 +38,7 @@ on: description: 'Type a config you want mannual test in .config, like: CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' required: false type: string - default: 'RT_USING_TINY_FFS=n,RT_USING_PUNY_FFS=y,CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' + default: 'CONFIG_RT_USING_DEBUG=y,CONFIG_RT_DEBUGING_COLOR=y,CONFIG_RT_DEBUGING_CONTEXT=y' dist_flag: description: 'True to dist all bsp, False not dist' required: true