Skip to content

Commit 18be4ca

Browse files
flynnjiangPaul Walmsley
authored andcommitted
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@kylinos.cn> Link: https://patch.msgid.link/20251218032614.57356-1-jiangfeng@kylinos.cn Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 098921e commit 18be4ca

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

arch/riscv/lib/strlen.S

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ SYM_FUNC_START(strlen)
2121
* Clobbers:
2222
* t0, t1
2323
*/
24-
mv t1, a0
24+
addi t1, a0, -1
2525
1:
26-
lbu t0, 0(t1)
27-
beqz t0, 2f
2826
addi t1, t1, 1
29-
j 1b
30-
2:
27+
lbu t0, 0(t1)
28+
bnez t0, 1b
3129
sub a0, t1, a0
3230
ret
3331

0 commit comments

Comments
 (0)