riscv: lib: add optimized memchr() and extend KUnit tests
Subsystems (2)
cover riscv: lib: add optimized memchr() and extend KUnit tests
From: Milan Tripkovic <[email protected]>
Add an optimized assembly implementation of memchr() for RISC-V. The implementation uses word-aligned loads and Zbb optimizations where available, with a fallback for non-Zbb hardware.
The series also includes KUnit tests for functional verification and performance benchmarking.
Milan Tripkovic (2):
riscv: lib: add memchr() implementation lib/string_kunit: extend benchmarks and unit test to memchr()
arch/riscv/include/asm/string.h | 3 +- arch/riscv/lib/Makefile | 1 + arch/riscv/lib/memchr.S | 126 ++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 +- lib/tests/string_kunit.c | 53 ++++++++++++++ 5 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/lib/memchr.S
Signature · 1 line
2.43.0
PATCH 1/2 riscv: lib: add memchr() implementation
From: Milan Tripkovic <[email protected]>
This implementation leverages word-at-a-time comparisons and the RISC-V Zbb extension to accelerate character searching, falling back to a generic byte-by-byte loop on systems without Zbb support.
Benchmark results (QEMU TCG, rv64):
| len | zbb | Wozbb | default | % Wozbb | % zbb | |------|--------|-------|---------|---------|---------| | 1 | 22.9 | 26 | 22.1 | 17.64 % | 3.62 %| | 7 | 115.5 | 130.6 | 100.1 | 30.46 % | 15.38 %| | 8 | 184.3 | 143.3 | 117.2 | 22.26 % | 57.25 %| | 16 | 322 | 205.1 | 179.7 | 14.13 % | 79.19 %| | 31 | 361 | 247.4 | 221.7 | 11.59 % | 62.83 %| | 64 | 880 | 280.6 | 233.2 | 20.32 % | 277.35 %| | 127 | 961.3 | 307.9 | 271.4 | 13.44 % | 254.20 %| | 512 | 1812.9 | 325.6 | 294.1 | 10.71 % | 516.42 %| | 1024 | 1973.2 | 335.4 | 273.8 | 22.49 % | 620.67 %| | 3173 | 2245.9 | 338.7 | 288.6 | 17.35 % | 678.20 %| | 4096 | 2327.5 | 345 | 317.5 | 8.661 % | 633.07 %|
--- arch/riscv/include/asm/string.h | 3 +- arch/riscv/lib/Makefile | 1 + arch/riscv/lib/memchr.S | 126 ++++++++++++++++++++++++++++++++ arch/riscv/purgatory/Makefile | 5 +- 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/lib/memchr.S
arch/riscv/include/asm/string.h +2 -1
@@ -18,7 +18,8 @@ extern asmlinkage void *__memcpy(void *, const void *, size_t);#define __HAVE_ARCH_MEMMOVE extern asmlinkage void *memmove(void *, const void *, size_t); extern asmlinkage void *__memmove(void *, const void *, size_t); - +#define __HAVE_ARCH_MEMCHR +extern asmlinkage void *memchr(const void *s, int c, size_t n);#if !(defined(CONFIG_KASAN_GENERIC ) || defined(CONFIG_KASAN_SW_TAGS ))#define __HAVE_ARCH_STRCMP extern asmlinkage int strcmp(const char *cs, const char *ct);
arch/riscv/lib/Makefile +1 -0
@@ -3,6 +3,7 @@ lib-y += delay.o lib-y += memcpy.o lib-y += memset.o lib-y += memmove.o +lib-y += memchr.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) lib-y += strcmp.o lib-y += strlen.o
arch/riscv/lib/memchr.S new file +126 -0
126 lines not on this page · raw message
arch/riscv/purgatory/Makefile +4 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o +purgatory-y := purgatory.o sha256.o entry.o string.o ctype.o memcpy.o memset.o memchr.o ifeq ($(CONFIG_KASAN_GENERIC)$(CONFIG_KASAN_SW_TAGS),) purgatory-y += strcmp.o strlen.o strncmp.o strnlen.o strchr.o strrchr.o endif
@@ -17,6 +17,9 @@ $(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE $(obj)/memcpy.o: $(srctree)/arch/riscv/lib/memcpy.S FORCE $(call if_changed_rule,as_o_S) +$(obj)/memchr.o: $(srctree)/arch/riscv/lib/memchr.S FORCE + $(call if_changed_rule,as_o_S) + $(obj)/memset.o: $(srctree)/arch/riscv/lib/memset.S FORCE $(call if_changed_rule,as_o_S)
Signature · 1 line
2.43.0
PATCH 2/2 lib/string_kunit: extend benchmarks and unit test to memchr()
In reply to Milan TripkovicFrom: Milan Tripkovic <[email protected]>
Extend the string benchmarking suite to include memchr(). Extend the string unit test to include memchr()
--- lib/tests/string_kunit.c | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
lib/tests/string_kunit.c +53 -0
@@ -881,6 +881,57 @@ static void string_bench_strrchr(struct kunit *test)STRING_BENCH_BUF (test, buf, len, strrchr, buf,'\0' ); } +static void string_test_memchr (struct kunit *test) +{ +char *buf; +size_t max_len =128 ; +size_t off, len, match_pos; +char *res; +char target =0x88 ; + + buf =kunit_kzalloc (test, max_len +64 ,GFP_KERNEL ); +KUNIT_ASSERT_NOT_ERR_OR_NULL (test, buf); + +memset (buf,'A' , max_len +64 ); + +for (off =0 ; off <16 ; off++) { +for (len =1 ; len < max_len; len++) { + match_pos = len /2 ; + buf[off + match_pos] = target; + + res =memchr (buf + off, target, len); +KUNIT_EXPECT_PTR_EQ_MSG (test, res, buf + off + match_pos, +"Failed at:%zu len:%zu pos:%zu" , off, len, + match_pos); + + buf[off + match_pos] ='A' ; + } + } + +memset (buf,'A' , max_len); + buf[10 ] ='B' ; + buf[20 ] ='B' ; + res =memchr (buf,'B' , max_len); +KUNIT_EXPECT_PTR_EQ (test, res, &buf[10 ]); + + buf[50 ] = (char )0xFF ; + res =memchr (buf,0xFF , max_len); +KUNIT_EXPECT_PTR_EQ (test, res, &buf[50 ]); + + buf[60 ] ='\0' ; + res =memchr (buf,'\0' , max_len); +KUNIT_EXPECT_PTR_EQ (test, res, &buf[60 ]); + + buf[max_len -1 ] ='Z' ; + res =memchr (buf,'Z' , max_len); +KUNIT_EXPECT_PTR_EQ (test, res, &buf[max_len -1 ]); +} + +static void string_bench_memchr (struct kunit *test) +{ +STRING_BENCH_BUF (test, buf, len, memchr, buf,'\0' , len); +} +static struct kunit_case string_test_cases[] = {KUNIT_CASE (string_test_memset16),KUNIT_CASE (string_test_memset32),
@@ -910,6 +961,8 @@ static struct kunit_case string_test_cases[] = {KUNIT_CASE (string_bench_strnlen),KUNIT_CASE (string_bench_strchr),KUNIT_CASE (string_bench_strrchr), +KUNIT_CASE (string_test_memchr), + KUNIT_CASE(string_bench_memchr), {} };
Signature · 1 line
2.43.0
Paul Walmsley
maintainerRe: [PATCH 2/2] lib/string_kunit: extend benchmarks and unit test to memchr()
Hi,
Quoted 6 lines · Milan Tripkovic
From: Milan Tripkovic <[email protected]>
Extend the string benchmarking suite to include
memchr(). Extend the string unit test to includememchr()Signed-off-by Milan Tripkovic <[email protected]>
This one either needs an ack from the appropriate maintainers of lib/tests/string_kunit.c, or should be posted separately so those maintainers can take it upstream.
thanks
- Paul