Re: [PATCH] newlib: libc: Improved the readability of strspn with minor optimization

"Xiao Zeng" <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
As mentioned in [PATCH] newlib: libc: Improved the readability of strcspn with minor optimization: <https://sourceware.org/pipermail/newlib/2023/020766.html>

This patch did the same thing and passed the testcases.

Similarly, provide a comparison of risc-v assembly:

no-patch:
---------------------------------------------------------
libc_a-strspn.o:     file format elf64-littleriscv


Disassembly of section .text:

0000000000000000 <strspn>:
   0:   00054683                lbu     a3,0(a0)
   4:   00050893                mv      a7,a0

0000000000000008 <.LVL1>:
   8:   04068463                beqz    a3,50 <.L6>
   c:   0005c803                lbu     a6,0(a1)

0000000000000010 <.LVL2>:
  10:   00000513                li      a0,0

0000000000000014 <.LVL3>:
  14:   00081463                bnez    a6,1c <.L13>
  18:   00008067                ret

000000000000001c <.L13>:
  1c:   00088613                mv      a2,a7

0000000000000020 <.L5>:
  20:   00058793                mv      a5,a1
  24:   00080713                mv      a4,a6
  28:   00c0006f                j       34 <.L4>

000000000000002c <.L14>:
  2c:   0007c703                lbu     a4,0(a5)
  30:   00070c63                beqz    a4,48 <.L11>

0000000000000034 <.L4>:
  34:   00178793                addi    a5,a5,1

0000000000000038 <.LVL7>:
  38:   fee69ae3                bne     a3,a4,2c <.L14>
  3c:   00164683                lbu     a3,1(a2)
  40:   00160613                addi    a2,a2,1
  44:   fc069ee3                bnez    a3,20 <.L5>

0000000000000048 <.L11>:
  48:   41160533                sub     a0,a2,a7
  4c:   00008067                ret

0000000000000050 <.L6>:
  50:   00000513                li      a0,0

0000000000000054 <.LVL10>:
  54:   00008067                ret
---------------------------------------------------------

patch
---------------------------------------------------------
libc_a-strspn.o:     file format elf64-littleriscv


Disassembly of section .text:

0000000000000000 <strspn>:
   0:   00054683                lbu     a3,0(a0)
   4:   04068063                beqz    a3,44 <.L11>
   8:   0005c803                lbu     a6,0(a1)

000000000000000c <.LVL1>:
   c:   00050613                mv      a2,a0
  10:   02080a63                beqz    a6,44 <.L11>

0000000000000014 <.L5>:
  14:   00058793                mv      a5,a1
  18:   00080713                mv      a4,a6
  1c:   00c0006f                j       28 <.L6>

0000000000000020 <.L4>:
  20:   0007c703                lbu     a4,0(a5)
  24:   00070c63                beqz    a4,3c <.L12>

0000000000000028 <.L6>:
  28:   00178793                addi    a5,a5,1

000000000000002c <.LVL5>:
  2c:   fee69ae3                bne     a3,a4,20 <.L4>

0000000000000030 <.LDL1>:
  30:   00164683                lbu     a3,1(a2)
  34:   00160613                addi    a2,a2,1
  38:   fc069ee3                bnez    a3,14 <.L5>

000000000000003c <.L12>:
  3c:   40a60533                sub     a0,a2,a0

0000000000000040 <.LVL7>:
  40:   00008067                ret

0000000000000044 <.L11>:
  44:   00000513                li      a0,0

0000000000000048 <.LVL9>:
  48:   00008067                ret
---------------------------------------------------------
After careful comparison, it was found that there are fewer assembly
instructions after the patch.

2023-12-20 14:43  Xiao Zeng <[email protected]> wrote:
>
 
>Signed-off-by: Xiao Zeng <[email protected]>
>---
> newlib/libc/string/strspn.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
>diff --git a/newlib/libc/string/strspn.c b/newlib/libc/string/strspn.c
>index baf239947..9d46ce2eb 100644
>--- a/newlib/libc/string/strspn.c
>+++ b/newlib/libc/string/strspn.c
>@@ -41,10 +41,11 @@ strspn (const char *s1,
>       for (c = s2; *c; c++)
> {
>   if (*s1 == *c)
>-	    break;
>+	    goto end;
> }
>       if (*c == '\0')
> break;
>+end:
>       s1++;
>     }
>
>--
>2.17.1
 
Thanks
Xiao Zeng
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.