From patchwork Tue Aug 15 09:43:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dengjianbo X-Patchwork-Id: 74145 Return-Path: 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 A5763385C6EB for ; Tue, 15 Aug 2023 09:44:03 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id BBE993857C5A for ; Tue, 15 Aug 2023 09:43:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BBE993857C5A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.5]) by gateway (Coremail) with SMTP id _____8Cx7+vKSNtkHLYYAA--.49786S3; Tue, 15 Aug 2023 17:43:38 +0800 (CST) Received: from 5.5.5 (unknown [10.2.5.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxfSPJSNtkThBbAA--.45023S3; Tue, 15 Aug 2023 17:43:38 +0800 (CST) From: dengjianbo To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, xry111@xry111.site, caiyinyu@loongson.cn, xuchenghua@loongson.cn, huangpei@loongson.cn, dengjianbo Subject: [PATCH 1/2] Loongarch: Add ifunc support for strchr{aligned, lsx, lasx} and strchrnul{aligned, lsx, lasx} Date: Tue, 15 Aug 2023 17:43:35 +0800 Message-Id: <20230815094336.1722160-2-dengjianbo@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230815094336.1722160-1-dengjianbo@loongson.cn> References: <20230815094336.1722160-1-dengjianbo@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8DxfSPJSNtkThBbAA--.45023S3 X-CM-SenderInfo: pghqwyxldqu0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj9fXoWfurWrCrW7uFWfArW5KFyDurX_yoW8uw18Co WSqF4DJrs2ka9xG395Crn7W3srWr1Sgr4jq3yjv3Z5Jr1xtry5GryFyasxKrZ7Grn5WFs5 Xa4xZr9xArZrGFn3l-sFpf9Il3svdjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8wcxFpf 9Il3svdxBIdaVrn0xqx4xG64xvF2IEw4CE5I8CrVC2j2Jv73VFW2AGmfu7bjvjm3AaLaJ3 UjIYCTnIWjp_UUUY17kC6x804xWl14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI 8IcIk0rVWrJVCq3wAFIxvE14AKwVWUGVWUXwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26r1I6r4UM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26r1j6r4UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_Gr1j6F4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI 0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUXVWUAwAv7VC2z280 aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28Icx kI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2Iq xVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42 IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY 6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aV CY1x0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxU7XTmDUUUU X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, KAM_STOCKGEN, SCC_5_SHORT_WORD_LINES, SPF_HELO_NONE, SPF_PASS, TXREP 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" These implementations improve the time to run strchr{nul} microbenchmark in glibc as below: strchr-lasx reduces the runtime about 50%-83% strchr-lsx reduces the runtime about 30%-67% strchr-aligned reduces the runtime about 10%-20% strchrnul-lasx reduces the runtime about 50%-83% strchrnul-lsx reduces the runtime about 36%-65% strchrnul-aligned reduces the runtime about 6%-10% --- sysdeps/loongarch/lp64/multiarch/Makefile | 6 ++ .../lp64/multiarch/ifunc-impl-list.c | 16 +++ .../loongarch/lp64/multiarch/ifunc-strchr.h | 41 ++++++++ .../lp64/multiarch/ifunc-strchrnul.h | 41 ++++++++ .../loongarch/lp64/multiarch/strchr-aligned.S | 99 +++++++++++++++++++ .../loongarch/lp64/multiarch/strchr-lasx.S | 91 +++++++++++++++++ sysdeps/loongarch/lp64/multiarch/strchr-lsx.S | 73 ++++++++++++++ sysdeps/loongarch/lp64/multiarch/strchr.c | 36 +++++++ .../lp64/multiarch/strchrnul-aligned.S | 95 ++++++++++++++++++ .../loongarch/lp64/multiarch/strchrnul-lasx.S | 22 +++++ .../loongarch/lp64/multiarch/strchrnul-lsx.S | 22 +++++ sysdeps/loongarch/lp64/multiarch/strchrnul.c | 39 ++++++++ 12 files changed, 581 insertions(+) create mode 100644 sysdeps/loongarch/lp64/multiarch/ifunc-strchr.h create mode 100644 sysdeps/loongarch/lp64/multiarch/ifunc-strchrnul.h create mode 100644 sysdeps/loongarch/lp64/multiarch/strchr-aligned.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchr-lasx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchr-lsx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchr.c create mode 100644 sysdeps/loongarch/lp64/multiarch/strchrnul-aligned.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchrnul-lasx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchrnul-lsx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/strchrnul.c diff --git a/sysdeps/loongarch/lp64/multiarch/Makefile b/sysdeps/loongarch/lp64/multiarch/Makefile index 76c506c966..110a8c5ca3 100644 --- a/sysdeps/loongarch/lp64/multiarch/Makefile +++ b/sysdeps/loongarch/lp64/multiarch/Makefile @@ -3,5 +3,11 @@ sysdep_routines += \ strlen-aligned \ strlen-lsx \ strlen-lasx \ + strchr-aligned \ + strchr-lsx \ + strchr-lasx \ + strchrnul-aligned \ + strchrnul-lsx \ + strchrnul-lasx \ # sysdep_routines endif diff --git a/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c b/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c index 1a2a576fcd..c7164b45e6 100644 --- a/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c +++ b/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c @@ -37,5 +37,21 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, #endif IFUNC_IMPL_ADD (array, i, strlen, 1, __strlen_aligned) ) + + IFUNC_IMPL (i, name, strchr, +#if !defined __loongarch_soft_float + IFUNC_IMPL_ADD (array, i, strchr, SUPPORT_LASX, __strchr_lasx) + IFUNC_IMPL_ADD (array, i, strchr, SUPPORT_LSX, __strchr_lsx) +#endif + IFUNC_IMPL_ADD (array, i, strchr, 1, __strchr_aligned) + ) + + IFUNC_IMPL (i, name, strchrnul, +#if !defined __loongarch_soft_float + IFUNC_IMPL_ADD (array, i, strchrnul, SUPPORT_LASX, __strchrnul_lasx) + IFUNC_IMPL_ADD (array, i, strchrnul, SUPPORT_LSX, __strchrnul_lsx) +#endif + IFUNC_IMPL_ADD (array, i, strchrnul, 1, __strchrnul_aligned) + ) return i; } diff --git a/sysdeps/loongarch/lp64/multiarch/ifunc-strchr.h b/sysdeps/loongarch/lp64/multiarch/ifunc-strchr.h new file mode 100644 index 0000000000..4494db79a5 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/ifunc-strchr.h @@ -0,0 +1,41 @@ +/* Common definition for strchr ifunc selections. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#if !defined __loongarch_soft_float +extern __typeof (REDIRECT_NAME) OPTIMIZE (lasx) attribute_hidden; +extern __typeof (REDIRECT_NAME) OPTIMIZE (lsx) attribute_hidden; +#endif + +extern __typeof (REDIRECT_NAME) OPTIMIZE (aligned) attribute_hidden; + +static inline void * +IFUNC_SELECTOR (void) +{ +#if !defined __loongarch_soft_float + if (SUPPORT_LASX) + return OPTIMIZE (lasx); + else if (SUPPORT_LSX) + return OPTIMIZE (lsx); + else +#endif + return OPTIMIZE (aligned); +} diff --git a/sysdeps/loongarch/lp64/multiarch/ifunc-strchrnul.h b/sysdeps/loongarch/lp64/multiarch/ifunc-strchrnul.h new file mode 100644 index 0000000000..8a92512064 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/ifunc-strchrnul.h @@ -0,0 +1,41 @@ +/* Common definition for strchrnul ifunc selections. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#if !defined __loongarch_soft_float +extern __typeof (REDIRECT_NAME) OPTIMIZE (lasx) attribute_hidden; +extern __typeof (REDIRECT_NAME) OPTIMIZE (lsx) attribute_hidden; +#endif + +extern __typeof (REDIRECT_NAME) OPTIMIZE (aligned) attribute_hidden; + +static inline void * +IFUNC_SELECTOR (void) +{ +#if !defined __loongarch_soft_float + if (SUPPORT_LASX) + return OPTIMIZE (lasx); + else if (SUPPORT_LSX) + return OPTIMIZE (lsx); + else +#endif + return OPTIMIZE (aligned); +} diff --git a/sysdeps/loongarch/lp64/multiarch/strchr-aligned.S b/sysdeps/loongarch/lp64/multiarch/strchr-aligned.S new file mode 100644 index 0000000000..5fb01806e4 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchr-aligned.S @@ -0,0 +1,99 @@ +/* Optimized strchr implementation using basic Loongarch instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#include +#include +#include + +#if IS_IN (libc) +# define STRCHR_NAME __strchr_aligned +#else +# define STRCHR_NAME strchr +#endif + +LEAF(STRCHR_NAME, 6) + slli.d t1, a0, 3 + bstrins.d a0, zero, 2, 0 + lu12i.w a2, 0x01010 + ld.d t2, a0, 0 + + ori a2, a2, 0x101 + andi a1, a1, 0xff + bstrins.d a2, a2, 63, 32 + li.w t0, -1 + + mul.d a1, a1, a2 + sll.d t0, t0, t1 + slli.d a3, a2, 7 + orn t2, t2, t0 + + sll.d t3, a1, t1 + xor t4, t2, t3 + sub.d a4, t2, a2 + sub.d a5, t4, a2 + + + andn a4, a4, t2 + andn a5, a5, t4 + or t0, a4, a5 + and t0, t0, a3 + + bnez t0, L(end) + addi.d a0, a0, 8 +L(loop): + ld.d t4, a0, 0 + xor t2, t4, a1 + + sub.d a4, t4, a2 + sub.d a5, t2, a2 + andn a4, a4, t4 + andn a5, a5, t2 + + or t0, a4, a5 + and t0, t0, a3 + bnez t0, L(end) + ld.d t4, a0, 8 + + + addi.d a0, a0, 16 + xor t2, t4, a1 + sub.d a4, t4, a2 + sub.d a5, t2, a2 + + andn a4, a4, t4 + andn a5, a5, t2 + or t0, a4, a5 + and t0, t0, a3 + + beqz t0, L(loop) + addi.d a0, a0, -8 +L(end): + and t0, a5, a3 + and t1, a4, a3 + + ctz.d t0, t0 + ctz.d t1, t1 + srli.w t2, t0, 3 + sltu t3, t1, t0 + + + add.d a0, a0, t2 + masknez a0, a0, t3 + jr ra +END(STRCHR_NAME) diff --git a/sysdeps/loongarch/lp64/multiarch/strchr-lasx.S b/sysdeps/loongarch/lp64/multiarch/strchr-lasx.S new file mode 100644 index 0000000000..254402daa5 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchr-lasx.S @@ -0,0 +1,91 @@ +/* Optimized strchr implementation using loongarch LASX SIMD instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#include +#include +#include + +#if IS_IN (libc) && !defined __loongarch_soft_float + +#ifndef AS_STRCHRNUL +# define STRCHR __strchr_lasx +#endif + +LEAF(STRCHR, 6) + andi t1, a0, 0x1f + bstrins.d a0, zero, 4, 0 + xvld xr0, a0, 0 + li.d t2, -1 + + xvreplgr2vr.b xr1, a1 + sll.d t1, t2, t1 + xvxor.v xr2, xr0, xr1 + xvmin.bu xr0, xr0, xr2 + + xvmsknz.b xr0, xr0 + xvpickve.w xr3, xr0, 4 + vilvl.h vr0, vr3, vr0 + movfr2gr.s t0, fa0 + + orn t0, t0, t1 + bne t0, t2, L(end) + addi.d a0, a0, 32 + nop + + +L(loop): + xvld xr0, a0, 0 + xvxor.v xr2, xr0, xr1 + xvmin.bu xr0, xr0, xr2 + xvsetanyeqz.b fcc0, xr0 + + bcnez fcc0, L(loop_end) + xvld xr0, a0, 32 + addi.d a0, a0, 64 + xvxor.v xr2, xr0, xr1 + + xvmin.bu xr0, xr0, xr2 + xvsetanyeqz.b fcc0, xr0 + bceqz fcc0, L(loop) + addi.d a0, a0, -32 + +L(loop_end): + xvmsknz.b xr0, xr0 + xvpickve.w xr1, xr0, 4 + vilvl.h vr0, vr1, vr0 + movfr2gr.s t0, fa0 + + +L(end): + cto.w t0, t0 + add.d a0, a0, t0 +#ifndef AS_STRCHRNUL + vreplgr2vr.b vr0, t0 + xvpermi.q xr3, xr2, 1 + + vshuf.b vr0, vr3, vr2, vr0 + vpickve2gr.bu t0, vr0, 0 + masknez a0, a0, t0 +#endif + jr ra + +END(STRCHR) + +libc_hidden_builtin_def(STRCHR) +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/strchr-lsx.S b/sysdeps/loongarch/lp64/multiarch/strchr-lsx.S new file mode 100644 index 0000000000..dae98b0a55 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchr-lsx.S @@ -0,0 +1,73 @@ +/* Optimized strlen implementation using loongarch LSX SIMD instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#include +#include +#include + +#if IS_IN (libc) && !defined __loongarch_soft_float + +#ifndef AS_STRCHRNUL +# define STRCHR __strchr_lsx +#endif + +LEAF(STRCHR, 6) + andi t1, a0, 0xf + bstrins.d a0, zero, 3, 0 + vld vr0, a0, 0 + li.d t2, -1 + + vreplgr2vr.b vr1, a1 + sll.d t3, t2, t1 + vxor.v vr2, vr0, vr1 + vmin.bu vr0, vr0, vr2 + + vmsknz.b vr0, vr0 + movfr2gr.s t0, fa0 + ext.w.h t0, t0 + orn t0, t0, t3 + + beq t0, t2, L(loop) +L(found): + cto.w t0, t0 + add.d a0, a0, t0 +#ifndef AS_STRCHRNUL + vreplve.b vr2, vr2, t0 + vpickve2gr.bu t1, vr2, 0 + masknez a0, a0, t1 +#endif + jr ra + + +L(loop): + vld vr0, a0, 16 + addi.d a0, a0, 16 + vxor.v vr2, vr0, vr1 + vmin.bu vr0, vr0, vr2 + + vsetanyeqz.b fcc0, vr0 + bceqz fcc0, L(loop) + vmsknz.b vr0, vr0 + movfr2gr.s t0, fa0 + + b L(found) +END(STRCHR) + +libc_hidden_builtin_def (STRCHR) +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/strchr.c b/sysdeps/loongarch/lp64/multiarch/strchr.c new file mode 100644 index 0000000000..404e97bded --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchr.c @@ -0,0 +1,36 @@ +/* Multiple versions of strchr. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Define multiple versions only for the definition in libc. */ +#if IS_IN (libc) +# define strchr __redirect_strchr +# include +# undef strchr + +# define SYMBOL_NAME strchr +# include "ifunc-strchr.h" + +libc_ifunc_redirected (__redirect_strchr, strchr, IFUNC_SELECTOR ()); +weak_alias(strchr, index) +# ifdef SHARED +__hidden_ver1 (strchr, __GI_strchr, __redirect_strchr) + __attribute__ ((visibility ("hidden"))) __attribute_copy__ (strchr); +# endif + +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/strchrnul-aligned.S b/sysdeps/loongarch/lp64/multiarch/strchrnul-aligned.S new file mode 100644 index 0000000000..1c01a0232d --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchrnul-aligned.S @@ -0,0 +1,95 @@ +/* Optimized strchrnul implementation using basic Loongarch instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#include +#include +#include + +#if IS_IN (libc) +# define STRCHRNUL_NAME __strchrnul_aligned +#else +# define STRCHRNUL_NAME __strchrnul +#endif + +LEAF(STRCHRNUL_NAME, 6) + slli.d t1, a0, 3 + bstrins.d a0, zero, 2, 0 + lu12i.w a2, 0x01010 + ld.d t2, a0, 0 + + ori a2, a2, 0x101 + andi a1, a1, 0xff + bstrins.d a2, a2, 63, 32 + li.w t0, -1 + + mul.d a1, a1, a2 + sll.d t0, t0, t1 + slli.d a3, a2, 7 + orn t2, t2, t0 + + sll.d t3, a1, t1 + xor t4, t2, t3 + sub.d a4, t2, a2 + sub.d a5, t4, a2 + + + andn a4, a4, t2 + andn a5, a5, t4 + or t0, a4, a5 + and t0, t0, a3 + + bnez t0, L(end) + addi.d a0, a0, 8 +L(loop): + ld.d t4, a0, 0 + xor t2, t4, a1 + + sub.d a4, t4, a2 + sub.d a5, t2, a2 + andn a4, a4, t4 + andn a5, a5, t2 + + or t0, a4, a5 + and t0, t0, a3 + bnez t0, L(end) + ld.d t4, a0, 8 + + + addi.d a0, a0, 16 + xor t2, t4, a1 + sub.d a4, t4, a2 + sub.d a5, t2, a2 + + andn a4, a4, t4 + andn a5, a5, t2 + or t0, a4, a5 + and t0, t0, a3 + + beqz t0, L(loop) + addi.d a0, a0, -8 +L(end): + ctz.d t0, t0 + srli.w t0, t0, 3 + + + add.d a0, a0, t0 + jr ra +END(STRCHRNUL_NAME) + +libc_hidden_builtin_def (STRCHRNUL_NAME) diff --git a/sysdeps/loongarch/lp64/multiarch/strchrnul-lasx.S b/sysdeps/loongarch/lp64/multiarch/strchrnul-lasx.S new file mode 100644 index 0000000000..d45495e48f --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchrnul-lasx.S @@ -0,0 +1,22 @@ +/* Optimized strchrnul implementation using loongarch LASX SIMD instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#define STRCHR __strchrnul_lasx +#define AS_STRCHRNUL +#include "strchr-lasx.S" diff --git a/sysdeps/loongarch/lp64/multiarch/strchrnul-lsx.S b/sysdeps/loongarch/lp64/multiarch/strchrnul-lsx.S new file mode 100644 index 0000000000..07d793ae5f --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchrnul-lsx.S @@ -0,0 +1,22 @@ +/* Optimized strchrnul implementation using loongarch LSX SIMD instructions. + Copyright (C) 2023 Free Software Foundation, Inc. + + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library. If not, see + . */ + +#define STRCHR __strchrnul_lsx +#define AS_STRCHRNUL +#include "strchr-lsx.S" diff --git a/sysdeps/loongarch/lp64/multiarch/strchrnul.c b/sysdeps/loongarch/lp64/multiarch/strchrnul.c new file mode 100644 index 0000000000..f3b8296ee8 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/strchrnul.c @@ -0,0 +1,39 @@ +/* Multiple versions of strchrnul. + All versions must be listed in ifunc-impl-list.c. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Define multiple versions only for the definition in libc. */ + +#if IS_IN (libc) +# define strchrnul __redirect_strchrnul +# define __strchrnul __redirect___strchrnul +# include +# undef __strchrnul +# undef strchrnul + +# define SYMBOL_NAME strchrnul +# include "ifunc-strchrnul.h" + +libc_ifunc_redirected (__redirect_strchrnul, __strchrnul, + IFUNC_SELECTOR ()); +weak_alias (__strchrnul, strchrnul) +# ifdef SHARED +__hidden_ver1 (__strchrnul, __GI___strchrnul, __redirect_strchrnul) + __attribute__((visibility ("hidden"))) __attribute_copy__ (strchrnul); +# endif +#endif