[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20251218032614.57356-1-jiangfeng@kylinos.cn>
Date: Thu, 18 Dec 2025 11:26:14 +0800
From: Feng Jiang <jiangfeng@...inos.cn>
To: pjw@...nel.org,
palmer@...belt.com,
aou@...s.berkeley.edu,
alex@...ti.fr,
samuel.holland@...ive.com,
charlie@...osinc.com,
conor.dooley@...rochip.com,
jiangfeng@...inos.cn
Cc: linux-riscv@...ts.infradead.org,
linux-kernel@...r.kernel.org
Subject: [PATCH] riscv: lib: optimize strlen loop efficiency
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@...inos.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
Powered by blists - more mailing lists