Message ID | 20231220061054.35685-1-zengxiao@eswincomputing.com |
---|---|
State | New |
Headers |
Return-Path: <newlib-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7FD253861842 for <patchwork@sourceware.org>; Wed, 20 Dec 2023 06:12:13 +0000 (GMT) X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from zg8tmtu5ljg5lje1ms4xmtka.icoremail.net (zg8tmtu5ljg5lje1ms4xmtka.icoremail.net [159.89.151.119]) by sourceware.org (Postfix) with ESMTP id C4E77385E02F for <newlib@sourceware.org>; Wed, 20 Dec 2023 06:12:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C4E77385E02F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eswincomputing.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C4E77385E02F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=159.89.151.119 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703052724; cv=none; b=vVZVqEbmmzm04yq65HaiSzgHCKGLnZ7klZPuETgFridEHlmo57BwK5QCEiok3PGl8oHhWcj7Dq/SKorAsMJLqJO+XO8qcbwZ7X4WEfP3dPhZ+So5W6/YTPmTxrqAZBIbqqb5fJTDRijAo2IJMSGng6gm1Py8jsZHIrsaqvLDYJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703052724; c=relaxed/simple; bh=1NPct5mq1DSoPuRgIQewcA86hX1DDUDusCEPT4c2Mzw=; h=From:To:Subject:Date:Message-Id; b=XqgF3PHK8DAuaBI5t/NqEFQlC6+/RQwp4rDPng8bbfCbwO/OvL5gYCLyPWPGtzd2Xz50W5SkrQk1m3pjJiqFqB9BZ+2fqc4HWRIJFJK4J94UQptRDWHj+7gw/y4RM6GplSrYzz6YdlvdSJDVkuucIjV61Vtg18DwMhR+gDma/aw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost.localdomain (unknown [10.12.130.31]) by app1 (Coremail) with SMTP id TAJkCgAXlvtWhYJl+k0CAA--.15040S4; Wed, 20 Dec 2023 14:10:31 +0800 (CST) From: Xiao Zeng <zengxiao@eswincomputing.com> To: newlib@sourceware.org Cc: jjohnstn@redhat.com, palmer@rivosinc.com, jeffreyalaw@gmail.com, torbjorn.svensson@foss.st.com, Xiao Zeng <zengxiao@eswincomputing.com> Subject: [PATCH] newlib: libc: Improved the readability of strpbrk with minor optimization Date: Wed, 20 Dec 2023 14:10:54 +0800 Message-Id: <20231220061054.35685-1-zengxiao@eswincomputing.com> X-Mailer: git-send-email 2.17.1 X-CM-TRANSID: TAJkCgAXlvtWhYJl+k0CAA--.15040S4 X-Coremail-Antispam: 1UD129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UjIYCTnIWjp_UUUY87AC8VAFwI0_Gr0_Xr1l1xkIjI8I6I8E 6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28Cjx kF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVWDJVCq3wA2z4x0Y4vE2Ix0cI8I cVCY1x0267AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2js IEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE 5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeV CFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACjI8F5VA0II8E6IAqYI8I648v4I1l c2xSY4AK6svPMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I 8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWU twCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x 0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_ Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7VUb XdbUUUUUU== X-CM-SenderInfo: p2hqw5xldrqvxvzl0uprps33xlqjhudrp/ X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list <newlib.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/newlib>, <mailto:newlib-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/newlib/> List-Post: <mailto:newlib@sourceware.org> List-Help: <mailto:newlib-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/newlib>, <mailto:newlib-request@sourceware.org?subject=subscribe> Errors-To: newlib-bounces+patchwork=sourceware.org@sourceware.org |
Series |
newlib: libc: Improved the readability of strpbrk with minor optimization
|
|
Commit Message
Xiao Zeng
Dec. 20, 2023, 6:10 a.m. UTC
Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
---
newlib/libc/string/strpbrk.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
Comments
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-strpbrk.o: file format elf64-littleriscv Disassembly of section .text: 0000000000000000 <strpbrk>: 0: 00054683 lbu a3,0(a0) 0000000000000004 <.LVL1>: 4: 06068463 beqz a3,6c <.L8> 8: 0005c603 lbu a2,0(a1) 000000000000000c <.LVL2>: c: 02060e63 beqz a2,48 <.L14> 0000000000000010 <.L6>: 10: 00058713 mv a4,a1 14: 00060793 mv a5,a2 0000000000000018 <.L5>: 18: 00f68e63 beq a3,a5,34 <.L4> 1c: 00174783 lbu a5,1(a4) 20: 00170713 addi a4,a4,1 0000000000000024 <.LVL4>: 24: fe079ae3 bnez a5,18 <.L5> 28: 00154683 lbu a3,1(a0) 2c: 00150513 addi a0,a0,1 0000000000000030 <.LVL5>: 30: fe0690e3 bnez a3,10 <.L6> 0000000000000034 <.L4>: 34: 00074783 lbu a5,0(a4) 38: 00f037b3 snez a5,a5 3c: 40f007b3 neg a5,a5 40: 00f57533 and a0,a0,a5 44: 00008067 ret 0000000000000048 <.L14>: 48: 00154683 lbu a3,1(a0) 4c: 00150513 addi a0,a0,1 0000000000000050 <.LVL8>: 50: fe069ce3 bnez a3,48 <.L14> 54: 00058713 mv a4,a1 58: 00074783 lbu a5,0(a4) 5c: 00f037b3 snez a5,a5 60: 40f007b3 neg a5,a5 64: 00f57533 and a0,a0,a5 0000000000000068 <.LVL9>: 68: 00008067 ret 000000000000006c <.L8>: 6c: 00000513 li a0,0 0000000000000070 <.LVL11>: 70: 00008067 ret --------------------------------------------------------- patch --------------------------------------------------------- libc_a-strpbrk.o: file format elf64-littleriscv Disassembly of section .text: 0000000000000000 <strpbrk>: 0: 00054683 lbu a3,0(a0) 0000000000000004 <.LVL1>: 4: 04068063 beqz a3,44 <.L10> 8: 0005c603 lbu a2,0(a1) c: 04060063 beqz a2,4c <.L14> 0000000000000010 <.L5>: 10: 00058793 mv a5,a1 14: 00060713 mv a4,a2 18: 00c0006f j 24 <.L4> 000000000000001c <.L17>: 1c: 0007c703 lbu a4,0(a5) 20: 00070863 beqz a4,30 <.L16> 0000000000000024 <.L4>: 24: 00178793 addi a5,a5,1 0000000000000028 <.LVL5>: 28: fee69ae3 bne a3,a4,1c <.L17> 000000000000002c <.L1>: 2c: 00008067 ret 0000000000000030 <.L16>: 30: 00154683 lbu a3,1(a0) 34: 00150513 addi a0,a0,1 0000000000000038 <.LVL8>: 38: fc069ce3 bnez a3,10 <.L5> 000000000000003c <.L6>: 3c: 0007c783 lbu a5,0(a5) 0000000000000040 <.LVL9>: 40: fe0796e3 bnez a5,2c <.L1> 0000000000000044 <.L10>: 44: 00000513 li a0,0 0000000000000048 <.LVL11>: 48: 00008067 ret 000000000000004c <.L14>: 4c: 00154683 lbu a3,1(a0) 50: 00150513 addi a0,a0,1 54: fe069ce3 bnez a3,4c <.L14> 58: 00058793 mv a5,a1 5c: fe1ff06f j 3c <.L6> --------------------------------------------------------- After careful comparison, it was found that there are fewer assembly instructions after the patch. 2023-12-20 14:10 Xiao Zeng <zengxiao@eswincomputing.com> wrote: > >Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com> >--- > newlib/libc/string/strpbrk.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > >diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c >index 774db1e6d..95e89c20c 100644 >--- a/newlib/libc/string/strpbrk.c >+++ b/newlib/libc/string/strpbrk.c >@@ -37,15 +37,14 @@ strpbrk (const char *s1, > for (c = s2; *c; c++) > { > if (*s1 == *c) >- break; >+ goto end; > } >- if (*c) >- break; > s1++; > } > > if (*c == '\0') > s1 = NULL; > >+end: > return (char *) s1; > } >-- >2.17.1 Thanks Xiao Zeng
Unless I am missing something, I think the code could be made even simpler to return the pointer when found and return NULL otherwise. index 774db1e..d984745 100644 --- a/newlib/libc/string/strpbrk.c +++ b/newlib/libc/string/strpbrk.c @@ -29,23 +29,16 @@ strpbrk (const char *s1, const char *s2) { const char *c = s2; - if (!*s1) - return (char *) NULL; while (*s1) { for (c = s2; *c; c++) { if (*s1 == *c) - break; + return (char *) s1; } - if (*c) - break; s1++; } - if (*c == '\0') - s1 = NULL; - - return (char *) s1; + return (char *) NULL; } On Wed, Dec 20, 2023 at 1:12 AM Xiao Zeng <zengxiao@eswincomputing.com> wrote: > Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com> > --- > newlib/libc/string/strpbrk.c | 5 ++--- > 1 file changed, 2 insertions(+), 3 deletions(-) > > diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c > index 774db1e6d..95e89c20c 100644 > --- a/newlib/libc/string/strpbrk.c > +++ b/newlib/libc/string/strpbrk.c > @@ -37,15 +37,14 @@ strpbrk (const char *s1, > for (c = s2; *c; c++) > { > if (*s1 == *c) > - break; > + goto end; > } > - if (*c) > - break; > s1++; > } > > if (*c == '\0') > s1 = NULL; > > +end: > return (char *) s1; > } > -- > 2.17.1 > >
2023-12-21 06:48 Jeff Johnston <jjohnstn@redhat.com> wrote: > >Unless I am missing something, I think the code could be made even simpler >to return the pointer when found and return NULL otherwise. Yes, you have found a more efficient method. After my testing, fewer assembly instructions were generated. This is really exciting. Xiao's patch: --------------------------------------------------------- libc_a-strpbrk.o: file format elf64-littleriscv Disassembly of section .text: 0000000000000000 <strpbrk>: 0: 00054683 lbu a3,0(a0) 0000000000000004 <.LVL1>: 4: 04068063 beqz a3,44 <.L10> 8: 0005c603 lbu a2,0(a1) c: 04060063 beqz a2,4c <.L14> 0000000000000010 <.L5>: 10: 00058793 mv a5,a1 14: 00060713 mv a4,a2 18: 00c0006f j 24 <.L4> 000000000000001c <.L17>: 1c: 0007c703 lbu a4,0(a5) 20: 00070863 beqz a4,30 <.L16> 0000000000000024 <.L4>: 24: 00178793 addi a5,a5,1 0000000000000028 <.LVL5>: 28: fee69ae3 bne a3,a4,1c <.L17> 000000000000002c <.L1>: 2c: 00008067 ret 0000000000000030 <.L16>: 30: 00154683 lbu a3,1(a0) 34: 00150513 addi a0,a0,1 0000000000000038 <.LVL8>: 38: fc069ce3 bnez a3,10 <.L5> 000000000000003c <.L6>: 3c: 0007c783 lbu a5,0(a5) 0000000000000040 <.LVL9>: 40: fe0796e3 bnez a5,2c <.L1> 0000000000000044 <.L10>: 44: 00000513 li a0,0 0000000000000048 <.LVL11>: 48: 00008067 ret 000000000000004c <.L14>: 4c: 00154683 lbu a3,1(a0) 50: 00150513 addi a0,a0,1 54: fe069ce3 bnez a3,4c <.L14> 58: 00058793 mv a5,a1 5c: fe1ff06f j 3c <.L6> --------------------------------------------------------- Jeff's patch --------------------------------------------------------- libc_a-strpbrk.o: file format elf64-littleriscv Disassembly of section .text: 0000000000000000 <strpbrk>: 0: 00054683 lbu a3,0(a0) 4: 02068c63 beqz a3,3c <.L8> 8: 0005c603 lbu a2,0(a1) c: 02060c63 beqz a2,44 <.L12> 0000000000000010 <.L5>: 10: 00058793 mv a5,a1 14: 00060713 mv a4,a2 18: 00c0006f j 24 <.L4> 000000000000001c <.L15>: 1c: 0007c703 lbu a4,0(a5) 20: 00070863 beqz a4,30 <.L14> 0000000000000024 <.L4>: 24: 00178793 addi a5,a5,1 0000000000000028 <.LVL4>: 28: fed71ae3 bne a4,a3,1c <.L15> 2c: 00008067 ret 0000000000000030 <.L14>: 30: 00154683 lbu a3,1(a0) 34: 00150513 addi a0,a0,1 0000000000000038 <.LVL6>: 38: fc069ce3 bnez a3,10 <.L5> 000000000000003c <.L8>: 3c: 00000513 li a0,0 0000000000000040 <.LVL7>: 40: 00008067 ret 0000000000000044 <.L12>: 44: 00154683 lbu a3,1(a0) 48: 00150513 addi a0,a0,1 4c: fe069ce3 bnez a3,44 <.L12> 50: fedff06f j 3c <.L8> --------------------------------------------------------- After careful comparison, it was found that there are fewer assembly instructions after the Jeff's patch. > >index 774db1e..d984745 100644 >--- a/newlib/libc/string/strpbrk.c >+++ b/newlib/libc/string/strpbrk.c >@@ -29,23 +29,16 @@ strpbrk (const char *s1, > const char *s2) > { > const char *c = s2; >- if (!*s1) >- return (char *) NULL; > > while (*s1) > { > for (c = s2; *c; c++) > { > if (*s1 == *c) >- break; >+ return (char *) s1; > } >- if (*c) >- break; > s1++; > } > >- if (*c == '\0') >- s1 = NULL; >- >- return (char *) s1; >+ return (char *) NULL; > } > >On Wed, Dec 20, 2023 at 1:12 AM Xiao Zeng <zengxiao@eswincomputing.com> >wrote: > >> Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com> >> --- >> newlib/libc/string/strpbrk.c | 5 ++--- >> 1 file changed, 2 insertions(+), 3 deletions(-) >> >> diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c >> index 774db1e6d..95e89c20c 100644 >> --- a/newlib/libc/string/strpbrk.c >> +++ b/newlib/libc/string/strpbrk.c >> @@ -37,15 +37,14 @@ strpbrk (const char *s1, >> for (c = s2; *c; c++) >> { >> if (*s1 == *c) >> - break; >> + goto end; >> } >> - if (*c) >> - break; >> s1++; >> } >> >> if (*c == '\0') >> s1 = NULL; >> >> +end: >> return (char *) s1; >> } >> -- >> 2.17.1 >> >> Thanks Xiao Zeng
Patch merged. Thanks. -- Jeff J. On Wed, Dec 20, 2023 at 8:16 PM Xiao Zeng <zengxiao@eswincomputing.com> wrote: > 2023-12-21 06:48 Jeff Johnston <jjohnstn@redhat.com> wrote: > > > > >Unless I am missing something, I think the code could be made even simpler > >to return the pointer when found and return NULL otherwise. > Yes, you have found a more efficient method. > After my testing, fewer assembly instructions were generated. This is > really exciting. > > Xiao's patch: > --------------------------------------------------------- > libc_a-strpbrk.o: file format elf64-littleriscv > > > Disassembly of section .text: > > 0000000000000000 <strpbrk>: > 0: 00054683 lbu a3,0(a0) > > 0000000000000004 <.LVL1>: > 4: 04068063 beqz a3,44 <.L10> > 8: 0005c603 lbu a2,0(a1) > c: 04060063 beqz a2,4c <.L14> > > 0000000000000010 <.L5>: > 10: 00058793 mv a5,a1 > 14: 00060713 mv a4,a2 > 18: 00c0006f j 24 <.L4> > > 000000000000001c <.L17>: > 1c: 0007c703 lbu a4,0(a5) > 20: 00070863 beqz a4,30 <.L16> > > 0000000000000024 <.L4>: > 24: 00178793 addi a5,a5,1 > > 0000000000000028 <.LVL5>: > 28: fee69ae3 bne a3,a4,1c <.L17> > > 000000000000002c <.L1>: > 2c: 00008067 ret > > 0000000000000030 <.L16>: > 30: 00154683 lbu a3,1(a0) > 34: 00150513 addi a0,a0,1 > > 0000000000000038 <.LVL8>: > 38: fc069ce3 bnez a3,10 <.L5> > > 000000000000003c <.L6>: > 3c: 0007c783 lbu a5,0(a5) > > 0000000000000040 <.LVL9>: > 40: fe0796e3 bnez a5,2c <.L1> > > 0000000000000044 <.L10>: > 44: 00000513 li a0,0 > > 0000000000000048 <.LVL11>: > 48: 00008067 ret > > 000000000000004c <.L14>: > 4c: 00154683 lbu a3,1(a0) > 50: 00150513 addi a0,a0,1 > 54: fe069ce3 bnez a3,4c <.L14> > 58: 00058793 mv a5,a1 > 5c: fe1ff06f j 3c <.L6> > --------------------------------------------------------- > > Jeff's patch > --------------------------------------------------------- > libc_a-strpbrk.o: file format elf64-littleriscv > > > Disassembly of section .text: > > 0000000000000000 <strpbrk>: > 0: 00054683 lbu a3,0(a0) > 4: 02068c63 beqz a3,3c <.L8> > 8: 0005c603 lbu a2,0(a1) > c: 02060c63 beqz a2,44 <.L12> > > 0000000000000010 <.L5>: > 10: 00058793 mv a5,a1 > 14: 00060713 mv a4,a2 > 18: 00c0006f j 24 <.L4> > > 000000000000001c <.L15>: > 1c: 0007c703 lbu a4,0(a5) > 20: 00070863 beqz a4,30 <.L14> > > 0000000000000024 <.L4>: > 24: 00178793 addi a5,a5,1 > > 0000000000000028 <.LVL4>: > 28: fed71ae3 bne a4,a3,1c <.L15> > 2c: 00008067 ret > > 0000000000000030 <.L14>: > 30: 00154683 lbu a3,1(a0) > 34: 00150513 addi a0,a0,1 > > 0000000000000038 <.LVL6>: > 38: fc069ce3 bnez a3,10 <.L5> > > 000000000000003c <.L8>: > 3c: 00000513 li a0,0 > > 0000000000000040 <.LVL7>: > 40: 00008067 ret > > 0000000000000044 <.L12>: > 44: 00154683 lbu a3,1(a0) > 48: 00150513 addi a0,a0,1 > 4c: fe069ce3 bnez a3,44 <.L12> > 50: fedff06f j 3c <.L8> > --------------------------------------------------------- > After careful comparison, it was found that there are fewer assembly > instructions after the Jeff's patch. > > > > >index 774db1e..d984745 100644 > >--- a/newlib/libc/string/strpbrk.c > >+++ b/newlib/libc/string/strpbrk.c > >@@ -29,23 +29,16 @@ strpbrk (const char *s1, > > const char *s2) > > { > > const char *c = s2; > >- if (!*s1) > >- return (char *) NULL; > > > > while (*s1) > > { > > for (c = s2; *c; c++) > > { > > if (*s1 == *c) > >- break; > >+ return (char *) s1; > > } > >- if (*c) > >- break; > > s1++; > > } > > > >- if (*c == '\0') > >- s1 = NULL; > >- > >- return (char *) s1; > >+ return (char *) NULL; > > } > > > >On Wed, Dec 20, 2023 at 1:12 AM Xiao Zeng <zengxiao@eswincomputing.com> > >wrote: > > > >> Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com> > >> --- > >> newlib/libc/string/strpbrk.c | 5 ++--- > >> 1 file changed, 2 insertions(+), 3 deletions(-) > >> > >> diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c > >> index 774db1e6d..95e89c20c 100644 > >> --- a/newlib/libc/string/strpbrk.c > >> +++ b/newlib/libc/string/strpbrk.c > >> @@ -37,15 +37,14 @@ strpbrk (const char *s1, > >> for (c = s2; *c; c++) > >> { > >> if (*s1 == *c) > >> - break; > >> + goto end; > >> } > >> - if (*c) > >> - break; > >> s1++; > >> } > >> > >> if (*c == '\0') > >> s1 = NULL; > >> > >> +end: > >> return (char *) s1; > >> } > >> -- > >> 2.17.1 > >> > >> > > Thanks > Xiao Zeng > >
2023-12-22 03:05 Jeff Johnston <jjohnstn@redhat.com> wrote: > >Patch merged. Thanks. Thank you, Jeff. Perhaps the two emails are too similar, I would like to remind you that there is a similar optimization on <https://sourceware.org/pipermail/newlib/2023/020771.html>. > >-- Jeff J. > >On Wed, Dec 20, 2023 at 8:16 PM Xiao Zeng <zengxiao@eswincomputing.com> >wrote: > >> 2023-12-21 06:48 Jeff Johnston <jjohnstn@redhat.com> wrote: >> > >> >> >Unless I am missing something, I think the code could be made even simpler >> >to return the pointer when found and return NULL otherwise. >> Yes, you have found a more efficient method. >> After my testing, fewer assembly instructions were generated. This is >> really exciting. >> >> Xiao's patch: >> --------------------------------------------------------- >> libc_a-strpbrk.o: file format elf64-littleriscv >> >> >> Disassembly of section .text: >> >> 0000000000000000 <strpbrk>: >> 0: 00054683 lbu a3,0(a0) >> >> 0000000000000004 <.LVL1>: >> 4: 04068063 beqz a3,44 <.L10> >> 8: 0005c603 lbu a2,0(a1) >> c: 04060063 beqz a2,4c <.L14> >> >> 0000000000000010 <.L5>: >> 10: 00058793 mv a5,a1 >> 14: 00060713 mv a4,a2 >> 18: 00c0006f j 24 <.L4> >> >> 000000000000001c <.L17>: >> 1c: 0007c703 lbu a4,0(a5) >> 20: 00070863 beqz a4,30 <.L16> >> >> 0000000000000024 <.L4>: >> 24: 00178793 addi a5,a5,1 >> >> 0000000000000028 <.LVL5>: >> 28: fee69ae3 bne a3,a4,1c <.L17> >> >> 000000000000002c <.L1>: >> 2c: 00008067 ret >> >> 0000000000000030 <.L16>: >> 30: 00154683 lbu a3,1(a0) >> 34: 00150513 addi a0,a0,1 >> >> 0000000000000038 <.LVL8>: >> 38: fc069ce3 bnez a3,10 <.L5> >> >> 000000000000003c <.L6>: >> 3c: 0007c783 lbu a5,0(a5) >> >> 0000000000000040 <.LVL9>: >> 40: fe0796e3 bnez a5,2c <.L1> >> >> 0000000000000044 <.L10>: >> 44: 00000513 li a0,0 >> >> 0000000000000048 <.LVL11>: >> 48: 00008067 ret >> >> 000000000000004c <.L14>: >> 4c: 00154683 lbu a3,1(a0) >> 50: 00150513 addi a0,a0,1 >> 54: fe069ce3 bnez a3,4c <.L14> >> 58: 00058793 mv a5,a1 >> 5c: fe1ff06f j 3c <.L6> >> --------------------------------------------------------- >> >> Jeff's patch >> --------------------------------------------------------- >> libc_a-strpbrk.o: file format elf64-littleriscv >> >> >> Disassembly of section .text: >> >> 0000000000000000 <strpbrk>: >> 0: 00054683 lbu a3,0(a0) >> 4: 02068c63 beqz a3,3c <.L8> >> 8: 0005c603 lbu a2,0(a1) >> c: 02060c63 beqz a2,44 <.L12> >> >> 0000000000000010 <.L5>: >> 10: 00058793 mv a5,a1 >> 14: 00060713 mv a4,a2 >> 18: 00c0006f j 24 <.L4> >> >> 000000000000001c <.L15>: >> 1c: 0007c703 lbu a4,0(a5) >> 20: 00070863 beqz a4,30 <.L14> >> >> 0000000000000024 <.L4>: >> 24: 00178793 addi a5,a5,1 >> >> 0000000000000028 <.LVL4>: >> 28: fed71ae3 bne a4,a3,1c <.L15> >> 2c: 00008067 ret >> >> 0000000000000030 <.L14>: >> 30: 00154683 lbu a3,1(a0) >> 34: 00150513 addi a0,a0,1 >> >> 0000000000000038 <.LVL6>: >> 38: fc069ce3 bnez a3,10 <.L5> >> >> 000000000000003c <.L8>: >> 3c: 00000513 li a0,0 >> >> 0000000000000040 <.LVL7>: >> 40: 00008067 ret >> >> 0000000000000044 <.L12>: >> 44: 00154683 lbu a3,1(a0) >> 48: 00150513 addi a0,a0,1 >> 4c: fe069ce3 bnez a3,44 <.L12> >> 50: fedff06f j 3c <.L8> >> --------------------------------------------------------- >> After careful comparison, it was found that there are fewer assembly >> instructions after the Jeff's patch. >> >> > >> >index 774db1e..d984745 100644 >> >--- a/newlib/libc/string/strpbrk.c >> >+++ b/newlib/libc/string/strpbrk.c >> >@@ -29,23 +29,16 @@ strpbrk (const char *s1, >> > const char *s2) >> > { >> > const char *c = s2; >> >- if (!*s1) >> >- return (char *) NULL; >> > >> > while (*s1) >> > { >> > for (c = s2; *c; c++) >> > { >> > if (*s1 == *c) >> >- break; >> >+ return (char *) s1; >> > } >> >- if (*c) >> >- break; >> > s1++; >> > } >> > >> >- if (*c == '\0') >> >- s1 = NULL; >> >- >> >- return (char *) s1; >> >+ return (char *) NULL; >> > } >> > >> >On Wed, Dec 20, 2023 at 1:12 AM Xiao Zeng <zengxiao@eswincomputing.com> >> >wrote: >> > >> >> Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com> >> >> --- >> >> newlib/libc/string/strpbrk.c | 5 ++--- >> >> 1 file changed, 2 insertions(+), 3 deletions(-) >> >> >> >> diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c >> >> index 774db1e6d..95e89c20c 100644 >> >> --- a/newlib/libc/string/strpbrk.c >> >> +++ b/newlib/libc/string/strpbrk.c >> >> @@ -37,15 +37,14 @@ strpbrk (const char *s1, >> >> for (c = s2; *c; c++) >> >> { >> >> if (*s1 == *c) >> >> - break; >> >> + goto end; >> >> } >> >> - if (*c) >> >> - break; >> >> s1++; >> >> } >> >> >> >> if (*c == '\0') >> >> s1 = NULL; >> >> >> >> +end: >> >> return (char *) s1; >> >> } >> >> -- >> >> 2.17.1 >> >> >> >> >> >> Thanks >> Xiao Zeng >> >> Thanks Xiao Zeng
diff --git a/newlib/libc/string/strpbrk.c b/newlib/libc/string/strpbrk.c index 774db1e6d..95e89c20c 100644 --- a/newlib/libc/string/strpbrk.c +++ b/newlib/libc/string/strpbrk.c @@ -37,15 +37,14 @@ strpbrk (const char *s1, for (c = s2; *c; c++) { if (*s1 == *c) - break; + goto end; } - if (*c) - break; s1++; } if (*c == '\0') s1 = NULL; +end: return (char *) s1; }