public inbox for linux-riscv@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] riscv: lib: optimize strlen loop efficiency
@ 2025-12-18  3:26 Feng Jiang
  2026-01-15  2:03 ` Paul Walmsley
  0 siblings, 1 reply; 10+ messages in thread
From: Feng Jiang @ 2025-12-18  3:26 UTC (permalink / raw)
  To: pjw, palmer, aou, alex, samuel.holland, charlie, conor.dooley,
	jiangfeng
  Cc: linux-riscv, linux-kernel

Optimize the generic strlen implementation by using a pre-decrement
pointer. This reduces the loop body from 4 instructions to 3 and
eliminates the unconditional jump ('j').

Old loop (4 instructions, 2 branches):
  1: lbu t0, 0(t1); beqz t0, 2f; addi t1, t1, 1; j 1b

New loop (3 instructions, 1 branch):
  1: addi t1, t1, 1; lbu t0, 0(t1); bnez t0, 1b

This change improves execution efficiency and reduces branch pressure
for systems without the Zbb extension.

Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
---
 arch/riscv/lib/strlen.S | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/arch/riscv/lib/strlen.S b/arch/riscv/lib/strlen.S
index eb4d2b7ed22b..e7736ccda514 100644
--- a/arch/riscv/lib/strlen.S
+++ b/arch/riscv/lib/strlen.S
@@ -21,13 +21,11 @@ SYM_FUNC_START(strlen)
 	 * Clobbers:
 	 *   t0, t1
 	 */
-	mv	t1, a0
+	addi	t1, a0, -1
 1:
-	lbu	t0, 0(t1)
-	beqz	t0, 2f
 	addi	t1, t1, 1
-	j	1b
-2:
+	lbu	t0, 0(t1)
+	bnez	t0, 1b
 	sub	a0, t1, a0
 	ret
 
-- 
2.25.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-01-29  8:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-18  3:26 [PATCH] riscv: lib: optimize strlen loop efficiency Feng Jiang
2026-01-15  2:03 ` Paul Walmsley
2026-01-15  3:23   ` Feng Jiang
2026-01-24  8:14     ` Paul Walmsley
2026-01-26  3:05       ` Feng Jiang
2026-01-15 11:19   ` David Laight
2026-01-15 18:46     ` David Laight
2026-01-26  2:52       ` Feng Jiang
2026-01-28 18:59       ` David Laight
2026-01-29  8:34         ` Feng Jiang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox