From patchwork Mon Aug 28 07:26:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dengjianbo X-Patchwork-Id: 1826632 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RZ2GC0yhkz1yfX for ; Mon, 28 Aug 2023 17:28:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 35C66385B81D for ; Mon, 28 Aug 2023 07:28:01 +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 D6AD23858D38 for ; Mon, 28 Aug 2023 07:26:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6AD23858D38 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 _____8CxRuhBTOxkaHUcAA--.4728S3; Mon, 28 Aug 2023 15:26:57 +0800 (CST) Received: from 5.5.5 (unknown [10.2.5.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dx4eQ9TOxkBYBlAA--.49174S5; Mon, 28 Aug 2023 15:26:56 +0800 (CST) From: dengjianbo To: libc-alpha@sourceware.org Subject: [PATCH 3/6] LoongArch: Add ifunc support for memrchr{lsx, lasx} Date: Mon, 28 Aug 2023 15:26:48 +0800 Message-Id: <20230828072651.3085034-4-dengjianbo@loongson.cn> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230828072651.3085034-1-dengjianbo@loongson.cn> References: <20230828072651.3085034-1-dengjianbo@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Dx4eQ9TOxkBYBlAA--.49174S5 X-CM-SenderInfo: pghqwyxldqu0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj93XoW3ZFWDuw18Cr4kXF4DZr17CFX_yoWkuFWfpF Wkur15Gan7CrW7WFWxG3Wav3WrCFs5Jrn0g3WY9rWUXrWkXr1kuF42yFWkW3WkJ3yrGrWY vanIvFyj9F48AagCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q6rW5McIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_Jw0_GFylx2IqxVAq x4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r 1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF 7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxV WUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07jo sjUUUUUU= X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, KAM_STOCKGEN, 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: caiyinyu@loongson.cn, xuchenghua@loongson.cn, huangpei@loongson.cn, dengjianbo Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" According to glibc memrchr microbenchmark, this implementation could reduce the runtime as following: Name Percent of rutime reduced memrchr-lasx 20%-83% memrchr-lsx 20%-64% --- sysdeps/loongarch/lp64/multiarch/Makefile | 3 + .../lp64/multiarch/ifunc-impl-list.c | 8 ++ .../loongarch/lp64/multiarch/ifunc-memrchr.h | 40 ++++++ .../lp64/multiarch/memrchr-generic.c | 23 ++++ .../loongarch/lp64/multiarch/memrchr-lasx.S | 123 ++++++++++++++++++ .../loongarch/lp64/multiarch/memrchr-lsx.S | 105 +++++++++++++++ sysdeps/loongarch/lp64/multiarch/memrchr.c | 33 +++++ 7 files changed, 335 insertions(+) create mode 100644 sysdeps/loongarch/lp64/multiarch/ifunc-memrchr.h create mode 100644 sysdeps/loongarch/lp64/multiarch/memrchr-generic.c create mode 100644 sysdeps/loongarch/lp64/multiarch/memrchr-lasx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/memrchr-lsx.S create mode 100644 sysdeps/loongarch/lp64/multiarch/memrchr.c diff --git a/sysdeps/loongarch/lp64/multiarch/Makefile b/sysdeps/loongarch/lp64/multiarch/Makefile index 2f4802cfa4..7b87bc9055 100644 --- a/sysdeps/loongarch/lp64/multiarch/Makefile +++ b/sysdeps/loongarch/lp64/multiarch/Makefile @@ -27,5 +27,8 @@ sysdep_routines += \ memchr-aligned \ memchr-lsx \ memchr-lasx \ + memrchr-generic \ + memrchr-lsx \ + memrchr-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 a567b9cf4d..8bd5489ee2 100644 --- a/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c +++ b/sysdeps/loongarch/lp64/multiarch/ifunc-impl-list.c @@ -109,5 +109,13 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array, #endif IFUNC_IMPL_ADD (array, i, memchr, 1, __memchr_aligned) ) + + IFUNC_IMPL (i, name, memrchr, +#if !defined __loongarch_soft_float + IFUNC_IMPL_ADD (array, i, memrchr, SUPPORT_LASX, __memrchr_lasx) + IFUNC_IMPL_ADD (array, i, memrchr, SUPPORT_LSX, __memrchr_lsx) +#endif + IFUNC_IMPL_ADD (array, i, memrchr, 1, __memrchr_generic) + ) return i; } diff --git a/sysdeps/loongarch/lp64/multiarch/ifunc-memrchr.h b/sysdeps/loongarch/lp64/multiarch/ifunc-memrchr.h new file mode 100644 index 0000000000..8215f9ad94 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/ifunc-memrchr.h @@ -0,0 +1,40 @@ +/* Common definition for memrchr implementation. + 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 (generic) 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 (generic); +} diff --git a/sysdeps/loongarch/lp64/multiarch/memrchr-generic.c b/sysdeps/loongarch/lp64/multiarch/memrchr-generic.c new file mode 100644 index 0000000000..ced61ebce5 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/memrchr-generic.c @@ -0,0 +1,23 @@ +/* Generic implementation of memrchr. + 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 + . */ + +#if IS_IN (libc) +# define MEMRCHR __memrchr_generic +#endif + +#include diff --git a/sysdeps/loongarch/lp64/multiarch/memrchr-lasx.S b/sysdeps/loongarch/lp64/multiarch/memrchr-lasx.S new file mode 100644 index 0000000000..5f3e0d06d7 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/memrchr-lasx.S @@ -0,0 +1,123 @@ +/* Optimized memrchr implementation using LoongArch LASX 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 MEMRCHR +# define MEMRCHR __memrchr_lasx +#endif + +LEAF(MEMRCHR, 6) + beqz a2, L(ret0) + addi.d a2, a2, -1 + add.d a3, a0, a2 + andi t1, a3, 0x3f + + bstrins.d a3, zero, 5, 0 + addi.d t1, t1, 1 + xvld xr0, a3, 0 + xvld xr1, a3, 32 + + sub.d t2, zero, t1 + li.d t3, -1 + xvreplgr2vr.b xr2, a1 + andi t4, a0, 0x3f + + srl.d t2, t3, t2 + xvseq.b xr0, xr0, xr2 + xvseq.b xr1, xr1, xr2 + xvmsknz.b xr0, xr0 + + + xvmsknz.b xr1, xr1 + xvpickve.w xr3, xr0, 4 + xvpickve.w xr4, xr1, 4 + vilvl.h vr0, vr3, vr0 + + vilvl.h vr1, vr4, vr1 + vilvl.w vr0, vr1, vr0 + movfr2gr.d t0, fa0 + and t0, t0, t2 + + bltu a2, t1, L(end) + bnez t0, L(found) + bstrins.d a0, zero, 5, 0 +L(loop): + xvld xr0, a3, -64 + + xvld xr1, a3, -32 + addi.d a3, a3, -64 + xvseq.b xr0, xr0, xr2 + xvseq.b xr1, xr1, xr2 + + + beq a0, a3, L(out) + xvmax.bu xr3, xr0, xr1 + xvseteqz.v fcc0, xr3 + bcnez fcc0, L(loop) + + xvmsknz.b xr0, xr0 + xvmsknz.b xr1, xr1 + xvpickve.w xr3, xr0, 4 + xvpickve.w xr4, xr1, 4 + + vilvl.h vr0, vr3, vr0 + vilvl.h vr1, vr4, vr1 + vilvl.w vr0, vr1, vr0 + movfr2gr.d t0, fa0 + +L(found): + addi.d a0, a3, 63 + clz.d t1, t0 + sub.d a0, a0, t1 + jr ra + + +L(out): + xvmsknz.b xr0, xr0 + xvmsknz.b xr1, xr1 + xvpickve.w xr3, xr0, 4 + xvpickve.w xr4, xr1, 4 + + vilvl.h vr0, vr3, vr0 + vilvl.h vr1, vr4, vr1 + vilvl.w vr0, vr1, vr0 + movfr2gr.d t0, fa0 + +L(end): + sll.d t2, t3, t4 + and t0, t0, t2 + addi.d a0, a3, 63 + clz.d t1, t0 + + sub.d a0, a0, t1 + maskeqz a0, a0, t0 + jr ra +L(ret0): + move a0, zero + + + jr ra +END(MEMRCHR) + +libc_hidden_builtin_def (MEMRCHR) +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/memrchr-lsx.S b/sysdeps/loongarch/lp64/multiarch/memrchr-lsx.S new file mode 100644 index 0000000000..39a7c8b076 --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/memrchr-lsx.S @@ -0,0 +1,105 @@ +/* Optimized memrchr implementation using LoongArch LSX 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 + +# define MEMRCHR __memrchr_lsx + +LEAF(MEMRCHR, 6) + beqz a2, L(ret0) + addi.d a2, a2, -1 + add.d a3, a0, a2 + andi t1, a3, 0x1f + + bstrins.d a3, zero, 4, 0 + addi.d t1, t1, 1 + vld vr0, a3, 0 + vld vr1, a3, 16 + + sub.d t2, zero, t1 + li.d t3, -1 + vreplgr2vr.b vr2, a1 + andi t4, a0, 0x1f + + srl.d t2, t3, t2 + vseq.b vr0, vr0, vr2 + vseq.b vr1, vr1, vr2 + vmsknz.b vr0, vr0 + + + vmsknz.b vr1, vr1 + vilvl.h vr0, vr1, vr0 + movfr2gr.s t0, fa0 + and t0, t0, t2 + + bltu a2, t1, L(end) + bnez t0, L(found) + bstrins.d a0, zero, 4, 0 +L(loop): + vld vr0, a3, -32 + + vld vr1, a3, -16 + addi.d a3, a3, -32 + vseq.b vr0, vr0, vr2 + vseq.b vr1, vr1, vr2 + + beq a0, a3, L(out) + vmax.bu vr3, vr0, vr1 + vseteqz.v fcc0, vr3 + bcnez fcc0, L(loop) + + + vmsknz.b vr0, vr0 + vmsknz.b vr1, vr1 + vilvl.h vr0, vr1, vr0 + movfr2gr.s t0, fa0 + +L(found): + addi.d a0, a3, 31 + clz.w t1, t0 + sub.d a0, a0, t1 + jr ra + +L(out): + vmsknz.b vr0, vr0 + vmsknz.b vr1, vr1 + vilvl.h vr0, vr1, vr0 + movfr2gr.s t0, fa0 + +L(end): + sll.d t2, t3, t4 + and t0, t0, t2 + addi.d a0, a3, 31 + clz.w t1, t0 + + + sub.d a0, a0, t1 + maskeqz a0, a0, t0 + jr ra +L(ret0): + move a0, zero + + jr ra +END(MEMRCHR) + +libc_hidden_builtin_def (MEMRCHR) +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/memrchr.c b/sysdeps/loongarch/lp64/multiarch/memrchr.c new file mode 100644 index 0000000000..8baba9ab7e --- /dev/null +++ b/sysdeps/loongarch/lp64/multiarch/memrchr.c @@ -0,0 +1,33 @@ +/* Multiple versions of memrchr. + 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 memrchr __redirect_memrchr +# include +# undef memrchr + +# define SYMBOL_NAME memrchr +# include "ifunc-memrchr.h" + +libc_ifunc_redirected (__redirect_memrchr, __memrchr, IFUNC_SELECTOR ()); +libc_hidden_def (__memrchr) +weak_alias (__memrchr, memrchr) + +#endif