Skip to content

Commit 6934cf8

Browse files
Heiko Stuebnerpalmer-dabbelt
authored andcommitted
RISC-V: improve string-function assembly
Adapt the suggestions for the assembly string functions that Andrew suggested but that I didn't manage to include into the series that got applied. This includes improvements to two comments, removal of unneeded labels and moving one instruction slightly higher to contradict an explanatory comment. Suggested-by: Andrew Jones <[email protected]> Signed-off-by: Heiko Stuebner <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Tested-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 01687e7 commit 6934cf8

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

arch/riscv/lib/strcmp.S

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ SYM_FUNC_START(strcmp)
4040
ret
4141

4242
/*
43-
* Variant of strcmp using the ZBB extension if available
43+
* Variant of strcmp using the ZBB extension if available.
44+
* The code was published as part of the bitmanip manual
45+
* in Appendix A.
4446
*/
4547
#ifdef CONFIG_RISCV_ISA_ZBB
4648
strcmp_zbb:
@@ -57,7 +59,7 @@ strcmp_zbb:
5759
* a1 - string2
5860
*
5961
* Clobbers
60-
* t0, t1, t2, t3, t4, t5
62+
* t0, t1, t2, t3, t4
6163
*/
6264

6365
or t2, a0, a1

arch/riscv/lib/strlen.S

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ strlen_zbb:
9696
* of valid bytes in this chunk.
9797
*/
9898
srli a0, t1, 3
99-
bgtu t3, a0, 3f
99+
bgtu t3, a0, 2f
100100

101101
/* Prepare for the word comparison loop. */
102102
addi t2, t0, SZREG
@@ -112,20 +112,20 @@ strlen_zbb:
112112
addi t0, t0, SZREG
113113
orc.b t1, t1
114114
beq t1, t3, 1b
115-
2:
115+
116116
not t1, t1
117117
CZ t1, t1
118+
srli t1, t1, 3
118119

119-
/* Get number of processed words. */
120+
/* Get number of processed bytes. */
120121
sub t2, t0, t2
121122

122123
/* Add number of characters in the first word. */
123124
add a0, a0, t2
124-
srli t1, t1, 3
125125

126126
/* Add number of characters in the last word. */
127127
add a0, a0, t1
128-
3:
128+
2:
129129
ret
130130

131131
.option pop

arch/riscv/lib/strncmp.S

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ strncmp_zbb:
7070
li t5, -1
7171
and t2, t2, SZREG-1
7272
add t4, a0, a2
73-
bnez t2, 4f
73+
bnez t2, 3f
7474

7575
/* Adjust limit for fast-path. */
7676
andi t6, t4, -SZREG
@@ -114,23 +114,21 @@ strncmp_zbb:
114114
ret
115115

116116
/* Simple loop for misaligned strings. */
117-
3:
118-
/* Restore limit for slow-path. */
119117
.p2align 3
120-
4:
121-
bge a0, t4, 6f
118+
3:
119+
bge a0, t4, 5f
122120
lbu t0, 0(a0)
123121
lbu t1, 0(a1)
124122
addi a0, a0, 1
125123
addi a1, a1, 1
126-
bne t0, t1, 5f
127-
bnez t0, 4b
124+
bne t0, t1, 4f
125+
bnez t0, 3b
128126

129-
5:
127+
4:
130128
sub a0, t0, t1
131129
ret
132130

133-
6:
131+
5:
134132
li a0, 0
135133
ret
136134

0 commit comments

Comments
 (0)