From patchwork Fri Apr 21 07:27:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hau Hsu X-Patchwork-Id: 68097 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 AE4033858434 for ; Fri, 21 Apr 2023 07:28:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE4033858434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682062108; bh=M+8RsA44kDpp09yFbLkF/XoJuNTBva6C3apOpqubi+I=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=jml5peGqGImUloWLKEvAODgQ89JSdGwEOuc16o4//lOF6KTwqG0McHf/ERNKRM/1/ Z6cmQVuckrH+oqqqPdojIGpYZcDR3rHnXGL2cluC2t/sLlcG+fAvHUAtY5vC0LCx8D eL/Mo0kWEdeN9Tkbi8Tk10PkZSGD82VdyRcDj6uM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hhsu-imac.local (1-169-217-217.dynamic-ip.hinet.net [1.169.217.217]) by sourceware.org (Postfix) with ESMTP id 40CDF3858C50 for ; Fri, 21 Apr 2023 07:28:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 40CDF3858C50 Received: by hhsu-imac.local (Postfix, from userid 501) id 5462452C538A; Fri, 21 Apr 2023 15:27:58 +0800 (CST) To: libc-alpha@sourceware.org, hongrong.hsu@sifive.com, jerry.shih@sifive.com, nick.knight@sifive.com, kito.cheng@sifive.com Cc: greentime.hu@sifive.com, alice.chan@sifive.com, andrew@sifive.com, vincent.chen@sifive.com, hau.hsu@sifive.com Subject: [PATCH v2 1/5] riscv: Enabling vectorized mem*/str* functions in build time Date: Fri, 21 Apr 2023 15:27:29 +0800 Message-Id: <20230421072733.14047-2-hau.hsu@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230421072733.14047-1-hau.hsu@sifive.com> References: <20230421072733.14047-1-hau.hsu@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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: 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: , X-Patchwork-Original-From: Hau Hsu via Libc-alpha From: Hau Hsu Reply-To: Hau Hsu Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Vincent Chen Let the build selects the vectorized mem*/str* functions when it detects the compiler supports RISC-V V extension and enables it in this build. We agree that the these vectorized mem*/str* functions should be selected by IFUNC. Therefore, this patch is intended as a **temporary solution** to enable reviewers to evaluate the effectiveness of these vectorized mem*/str* functions. --- scripts/build-many-glibcs.py | 10 ++++++++++ sysdeps/riscv/preconfigure | 19 +++++++++++++++++++ sysdeps/riscv/preconfigure.ac | 18 ++++++++++++++++++ sysdeps/riscv/rv32/rvv/Implies | 2 ++ sysdeps/riscv/rv64/rvv/Implies | 2 ++ 5 files changed, 51 insertions(+) create mode 100644 sysdeps/riscv/rv32/rvv/Implies create mode 100644 sysdeps/riscv/rv64/rvv/Implies diff --git a/scripts/build-many-glibcs.py b/scripts/build-many-glibcs.py index 82f8d97281..2fbb91a028 100755 --- a/scripts/build-many-glibcs.py +++ b/scripts/build-many-glibcs.py @@ -381,6 +381,11 @@ class Context(object): variant='rv32imafdc-ilp32d', gcc_cfg=['--with-arch=rv32imafdc', '--with-abi=ilp32d', '--disable-multilib']) + self.add_config(arch='riscv32', + os_name='linux-gnu', + variant='rv32imafdcv-ilp32d', + gcc_cfg=['--with-arch=rv32imafdcv', '--with-abi=ilp32d', + '--disable-multilib']) self.add_config(arch='riscv64', os_name='linux-gnu', variant='rv64imac-lp64', @@ -396,6 +401,11 @@ class Context(object): variant='rv64imafdc-lp64d', gcc_cfg=['--with-arch=rv64imafdc', '--with-abi=lp64d', '--disable-multilib']) + self.add_config(arch='riscv64', + os_name='linux-gnu', + variant='rv64imafdcv-lp64d', + gcc_cfg=['--with-arch=rv64imafdcv', '--with-abi=lp64d', + '--disable-multilib']) self.add_config(arch='s390x', os_name='linux-gnu', glibcs=[{}, diff --git a/sysdeps/riscv/preconfigure b/sysdeps/riscv/preconfigure index 4dedf4b0bb..5ddc195b46 100644 --- a/sysdeps/riscv/preconfigure +++ b/sysdeps/riscv/preconfigure @@ -7,6 +7,7 @@ riscv*) flen=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __riscv_flen \(.*\)/\1/p'` float_abi=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __riscv_float_abi_\([^ ]*\) .*/\1/p'` atomic=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | grep '#define __riscv_atomic' | cut -d' ' -f2` + vector=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | grep '#define __riscv_vector' | cut -d' ' -f2` case "$xlen" in 64 | 32) @@ -32,6 +33,24 @@ riscv*) ;; esac + case "$vector" in + __riscv_vector) + case "$flen" in + 64) + float_machine=rvv + ;; + *) + # V 1.0 spec requires both F and D extensions, but this may be an older version. Degrade to scalar only. + ;; + esac + ;; + *) + ;; + esac + + { $as_echo "$as_me:${as_lineno-$LINENO}: vector $vector flen $flen float_machine $float_machine" >&5 +$as_echo "$as_me: vector $vector flen $flen float_machine $float_machine" >&6;} + case "$float_abi" in soft) abi_flen=0 diff --git a/sysdeps/riscv/preconfigure.ac b/sysdeps/riscv/preconfigure.ac index a5c30e0dbf..b6b8bb46e4 100644 --- a/sysdeps/riscv/preconfigure.ac +++ b/sysdeps/riscv/preconfigure.ac @@ -7,6 +7,7 @@ riscv*) flen=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __riscv_flen \(.*\)/\1/p'` float_abi=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __riscv_float_abi_\([^ ]*\) .*/\1/p'` atomic=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | grep '#define __riscv_atomic' | cut -d' ' -f2` + vector=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | grep '#define __riscv_vector' | cut -d' ' -f2` case "$xlen" in 64 | 32) @@ -32,6 +33,23 @@ riscv*) ;; esac + case "$vector" in + __riscv_vector) + case "$flen" in + 64) + float_machine=rvv + ;; + *) + # V 1.0 spec requires both F and D extensions, but this may be an older version. Degrade to scalar only. + ;; + esac + ;; + *) + ;; + esac + + AC_MSG_NOTICE([vector $vector flen $flen float_machine $float_machine]) + case "$float_abi" in soft) abi_flen=0 diff --git a/sysdeps/riscv/rv32/rvv/Implies b/sysdeps/riscv/rv32/rvv/Implies new file mode 100644 index 0000000000..25ce1df222 --- /dev/null +++ b/sysdeps/riscv/rv32/rvv/Implies @@ -0,0 +1,2 @@ +riscv/rv32/rvd +riscv/rvv diff --git a/sysdeps/riscv/rv64/rvv/Implies b/sysdeps/riscv/rv64/rvv/Implies new file mode 100644 index 0000000000..9993bb30e3 --- /dev/null +++ b/sysdeps/riscv/rv64/rvv/Implies @@ -0,0 +1,2 @@ +riscv/rv64/rvd +riscv/rvv From patchwork Fri Apr 21 07:31:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hau Hsu X-Patchwork-Id: 68101 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 80C9B385701C for ; Fri, 21 Apr 2023 07:32:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 80C9B385701C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682062334; bh=Hm/7cUa1/bLGlUij6cYL0nWvl7GDXs3H6f8oOwjui9Q=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=lsqfGnMBgAPh+ZLuTBtwt1PwhGee/etZ0QClZ1/3jvZZVe6IVEJvZsl37lu7VXoUG DR41gwqqLA2ZY2J0SOd4x8S/mwbcYviLrd1UmnHe6BSWDXxHRO9xrsoj9IUO0mWSdW rgufYSyXi677oTKHVJgzotk8WAQIdRsFloknCsCs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hhsu-imac.local (1-169-217-217.dynamic-ip.hinet.net [1.169.217.217]) by sourceware.org (Postfix) with ESMTP id E6DF73857739 for ; Fri, 21 Apr 2023 07:31:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6DF73857739 Received: by hhsu-imac.local (Postfix, from userid 501) id E35B152C5B17; Fri, 21 Apr 2023 15:31:41 +0800 (CST) To: libc-alpha@sourceware.org, hongrong.hsu@sifive.com, jerry.shih@sifive.com, nick.knight@sifive.com, kito.cheng@sifive.com Cc: greentime.hu@sifive.com, alice.chan@sifive.com, andrew@sifive.com, vincent.chen@sifive.com, hau.hsu@sifive.com Subject: [PATCH v2 3/5] riscv: vectorized str* functions Date: Fri, 21 Apr 2023 15:31:32 +0800 Message-Id: <20230421073132.14241-1-hau.hsu@sifive.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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: 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: , X-Patchwork-Original-From: Hau Hsu via Libc-alpha From: Hau Hsu Reply-To: Hau Hsu Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Jerry Shih This patch proposes implementations of strcat, strcmp, strcpy, strlen, strncat, strncmp and strncpy that leverage the RISC-V V extension (RVV), version 1.0. These routines assumes VLEN is at least 32 bits, as is required by all currently defined vector extensions, and they support arbitrarily large VLEN. All implementations work for both RV32 and RV64 platforms, and make no assumptions about page size. --- sysdeps/riscv/rvv/strcat.S | 72 ++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strcmp.S | 93 +++++++++++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strcpy.S | 56 ++++++++++++++++++++++ sysdeps/riscv/rvv/strlen.S | 54 +++++++++++++++++++++ sysdeps/riscv/rvv/strncat.S | 83 +++++++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strncmp.S | 85 +++++++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strncpy.S | 86 ++++++++++++++++++++++++++++++++++ 7 files changed, 529 insertions(+) create mode 100644 sysdeps/riscv/rvv/strcat.S create mode 100644 sysdeps/riscv/rvv/strcmp.S create mode 100644 sysdeps/riscv/rvv/strcpy.S create mode 100644 sysdeps/riscv/rvv/strlen.S create mode 100644 sysdeps/riscv/rvv/strncat.S create mode 100644 sysdeps/riscv/rvv/strncmp.S create mode 100644 sysdeps/riscv/rvv/strncpy.S diff --git a/sysdeps/riscv/rvv/strcat.S b/sysdeps/riscv/rvv/strcat.S new file mode 100644 index 0000000000..8a7779fd3c --- /dev/null +++ b/sysdeps/riscv/rvv/strcat.S @@ -0,0 +1,72 @@ +/* RVV versions strcat. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define pDst a0 +#define pSrc a1 +#define pDstPtr a2 + +#define iVL a3 +#define iCurrentVL a4 +#define iActiveElemPos a5 + +#define ELEM_LMUL_SETTING m1 +#define vMask1 v0 +#define vMask2 v1 +#define vStr1 v8 +#define vStr2 v16 + +ENTRY(strcat) + + mv pDstPtr, pDst + + /* Perform `strlen(dst)`. */ +L(strlen_loop): + vsetvli iVL, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vStr1, (pDstPtr) + vmseq.vx vMask1, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask1 + add pDstPtr, pDstPtr, iCurrentVL + bltz iActiveElemPos, L(strlen_loop) + + sub pDstPtr, pDstPtr, iCurrentVL + add pDstPtr, pDstPtr, iActiveElemPos + + /* Perform `strcpy(dst, src)`. */ +L(strcpy_loop): + vsetvli iVL, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vStr1, (pSrc) + vmseq.vx vMask2, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask2 + vmsif.m vMask1, vMask2 + add pSrc, pSrc, iCurrentVL + vse8.v vStr1, (pDstPtr), vMask1.t + add pDstPtr, pDstPtr, iCurrentVL + bltz iActiveElemPos, L(strcpy_loop) + + ret + +END(strcat) +libc_hidden_builtin_def (strcat) diff --git a/sysdeps/riscv/rvv/strcmp.S b/sysdeps/riscv/rvv/strcmp.S new file mode 100644 index 0000000000..c5f525bbe9 --- /dev/null +++ b/sysdeps/riscv/rvv/strcmp.S @@ -0,0 +1,93 @@ +// Copyright (c) 2023 SiFive, Inc. -- Proprietary and Confidential All Rights +// Reserved. +// +// NOTICE: All information contained herein is, and remains the property of +// SiFive, Inc. The intellectual and technical concepts contained herein are +// proprietary to SiFive, Inc. and may be covered by U.S. and Foreign Patents, +// patents in process, and are protected by trade secret or copyright law. +// +// This work may not be copied, modified, re-published, uploaded, executed, or +// distributed in any way, in any medium, whether in whole or in part, without +// prior written permission from SiFive, Inc. +// +// The copyright notice above does not evidence any actual or intended +// publication or disclosure of this source code, which includes information +// that is confidential and/or proprietary, and is a trade secret, of SiFive, +// Inc. +//===----------------------------------------------------------------------===// + +// Contributed by: Jerry Shih + +// Prototype: +// int strcmp(const char *lhs, const char *rhs) + +#include +#include + +#define iResult a0 + +#define pStr1 a0 +#define pStr2 a1 + +#define iVL a2 +#define iTemp1 a3 +#define iTemp2 a4 + +#define vStr1 v0 +#define vStr2 v8 +#define vMask1 v16 +#define vMask2 v17 + +ENTRY(strcmp) + // lmul=1 + +L(Loop): + vsetvli iVL, zero, e8, m1, ta, ma + vle8ff.v vStr1, (pStr1) + // check if vStr1[i] == 0 + vmseq.vx vMask1, vStr1, zero + + vle8ff.v vStr2, (pStr2) + // check if vStr1[i] != vStr2[i] + vmsne.vv vMask2, vStr1, vStr2 + + // find the index x for vStr1[x]==0 + vfirst.m iTemp1, vMask1 + // find the index x for vStr1[x]!=vStr2[x] + vfirst.m iTemp2, vMask2 + + bgez iTemp1, L(check1) + bgez iTemp2, L(check2) + + // get the current vl updated by vle8ff. + csrr iVL, vl + add pStr1, pStr1, iVL + add pStr2, pStr2, iVL + j L(Loop) + + // iTemp1>=0 +L(check1): + bltz iTemp2, 1f + blt iTemp2, iTemp1, L(check2) +1: + // iTemp2<0 + // iTemp2>=0 && iTemp1=0 +L(check2): + add pStr1, pStr1, iTemp2 + add pStr2, pStr2, iTemp2 + lbu iTemp1, 0(pStr1) + lbu iTemp2, 0(pStr2) + sub iResult, iTemp1, iTemp2 + ret + +END(strcmp) +libc_hidden_builtin_def (strcmp) diff --git a/sysdeps/riscv/rvv/strcpy.S b/sysdeps/riscv/rvv/strcpy.S new file mode 100644 index 0000000000..8fb754ee23 --- /dev/null +++ b/sysdeps/riscv/rvv/strcpy.S @@ -0,0 +1,56 @@ +/* RVV versions strcpy. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define pDst a0 +#define pSrc a1 +#define pDstPtr a2 + +#define iVL a3 +#define iCurrentVL a4 +#define iActiveElemPos a5 + +#define ELEM_LMUL_SETTING m1 +#define vMask1 v0 +#define vMask2 v1 +#define vStr1 v8 +#define vStr2 v16 + +ENTRY(strcpy) + + mv pDstPtr, pDst + +L(strcpy_loop): + vsetvli iVL, zero, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vStr1, (pSrc) + vmseq.vx vMask2, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask2 + vmsif.m vMask1, vMask2 + add pSrc, pSrc, iCurrentVL + vse8.v vStr1, (pDstPtr), vMask1.t + add pDstPtr, pDstPtr, iCurrentVL + bltz iActiveElemPos, L(strcpy_loop) + + ret + +END(strcpy) +libc_hidden_builtin_def (strcpy) diff --git a/sysdeps/riscv/rvv/strlen.S b/sysdeps/riscv/rvv/strlen.S new file mode 100644 index 0000000000..eb456b094b --- /dev/null +++ b/sysdeps/riscv/rvv/strlen.S @@ -0,0 +1,54 @@ +/* RVV versions strlen. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define iResult a0 +#define pStr a0 +#define pCopyStr a1 +#define iVL a2 +#define iCurrentVL a2 +#define iEndOffset a3 + +#define ELEM_LMUL_SETTING m2 +#define vStr v0 +#define vMaskEnd v2 + +ENTRY(strlen) + + mv pCopyStr, pStr +L(loop): + vsetvli iVL, zero, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vStr, (pCopyStr) + csrr iCurrentVL, vl + vmseq.vi vMaskEnd, vStr, 0 + vfirst.m iEndOffset, vMaskEnd + add pCopyStr, pCopyStr, iCurrentVL + bltz iEndOffset, L(loop) + + add pStr, pStr, iCurrentVL + add pCopyStr, pCopyStr, iEndOffset + sub iResult, pCopyStr, iResult + + ret + +END(strlen) + +libc_hidden_builtin_def (strlen) diff --git a/sysdeps/riscv/rvv/strncat.S b/sysdeps/riscv/rvv/strncat.S new file mode 100644 index 0000000000..7847c4f008 --- /dev/null +++ b/sysdeps/riscv/rvv/strncat.S @@ -0,0 +1,83 @@ +/* RVV versions strncat. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define pDst a0 +#define pSrc a1 +#define iLength a2 +#define pDstPtr a3 + +#define iVL a4 +#define iCurrentVL a5 +#define iActiveElemPos a6 + +#define ELEM_LMUL_SETTING m1 +#define vMask1 v0 +#define vMask2 v1 +#define vStr1 v8 +#define vStr2 v16 + +ENTRY(strncat) + + mv pDstPtr, pDst + + /* the strlen of dst. */ +L(strlen_loop): + vsetvli iVL, zero, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vStr1, (pDstPtr) + /* find the '\0'. */ + vmseq.vx vMask1, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask1 + add pDstPtr, pDstPtr, iCurrentVL + bltz iActiveElemPos, L(strlen_loop) + + sub pDstPtr, pDstPtr, iCurrentVL + add pDstPtr, pDstPtr, iActiveElemPos + + /* copy pSrc to pDstPtr. */ +L(strcpy_loop): + vsetvli zero, iLength, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vStr1, (pSrc) + vmseq.vx vMask2, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask2 + vmsif.m vMask1, vMask2 + add pSrc, pSrc, iCurrentVL + sub iLength, iLength, iCurrentVL + vse8.v vStr1, (pDstPtr), vMask1.t + add pDstPtr, pDstPtr, iCurrentVL + beqz iLength, L(fill_zero) + bltz iActiveElemPos, L(strcpy_loop) + + ret + +L(fill_zero): + bgez iActiveElemPos, L(fill_zero_end) + sb zero, (pDstPtr) + +L(fill_zero_end): + ret + +END(strncat) +libc_hidden_builtin_def (strncat) diff --git a/sysdeps/riscv/rvv/strncmp.S b/sysdeps/riscv/rvv/strncmp.S new file mode 100644 index 0000000000..168dbb07ce --- /dev/null +++ b/sysdeps/riscv/rvv/strncmp.S @@ -0,0 +1,85 @@ +/* RVV versions strncmp. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define iResult a0 + +#define pStr1 a0 +#define pStr2 a1 +#define iLength a2 + +#define iVL a3 +#define iTemp1 a4 +#define iTemp2 a5 + +#define ELEM_LMUL_SETTING m1 +#define vStr1 v0 +#define vStr2 v4 +#define vMask1 v8 +#define vMask2 v9 + +ENTRY(strncmp) + + beqz iLength, L(zero_length) + +L(loop): + vsetvli zero, iLength, e8, ELEM_LMUL_SETTING, ta, ma + + vle8ff.v vStr1, (pStr1) + /* vStr1[i] == 0. */ + vmseq.vx vMask1, vStr1, zero + + vle8ff.v vStr2, (pStr2) + /* vStr1[i] != vStr2[i]. */ + vmsne.vv vMask2, vStr1, vStr2 + + csrr iVL, vl + + /* r = mask1 | mask2 + We could use vfirst.m to get the first zero char or the + first different char between str1 and str2. */ + vmor.mm vMask1, vMask1, vMask2 + + sub iLength, iLength, iVL + + vfirst.m iTemp1, vMask1 + + bgez iTemp1, L(end_loop) + + add pStr1, pStr1, iVL + add pStr2, pStr2, iVL + bnez iLength, L(loop) +L(end_loop): + + add pStr1, pStr1, iTemp1 + add pStr2, pStr2, iTemp1 + lbu iTemp1, 0(pStr1) + lbu iTemp2, 0(pStr2) + + sub iResult, iTemp1, iTemp2 + ret + +L(zero_length): + li iResult, 0 + ret + +END(strncmp) +libc_hidden_builtin_def (strncmp) diff --git a/sysdeps/riscv/rvv/strncpy.S b/sysdeps/riscv/rvv/strncpy.S new file mode 100644 index 0000000000..e8d9450448 --- /dev/null +++ b/sysdeps/riscv/rvv/strncpy.S @@ -0,0 +1,86 @@ +/* RVV versions strncpy. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih . + + 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 + +#define pDst a0 +#define pSrc a1 +#define iLength a2 +#define pDstPtr a3 + +#define iVL a4 +#define iCurrentVL a5 +#define iActiveElemPos a6 +#define iTemp a7 + +#define ELEM_LMUL_SETTING m1 +#define vMask1 v0 +#define vMask2 v1 +#define ZERO_FILL_ELEM_LMUL_SETTING m8 +#define vStr1 v8 +#define vStr2 v16 + +ENTRY(strncpy) + + mv pDstPtr, pDst + + /* Copy pSrc to pDstPtr. */ +L(strcpy_loop): + vsetvli zero, iLength, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vStr1, (pSrc) + vmseq.vx vMask2, vStr1, zero + csrr iCurrentVL, vl + vfirst.m iActiveElemPos, vMask2 + vmsif.m vMask1, vMask2 + add pSrc, pSrc, iCurrentVL + sub iLength, iLength, iCurrentVL + vse8.v vStr1, (pDstPtr), vMask1.t + add pDstPtr, pDstPtr, iCurrentVL + bgez iActiveElemPos, L(fill_zero) + bnez iLength, L(strcpy_loop) + ret + + /* Fill the tail zero. */ +L(fill_zero): + /* We already copy the `\0` to dst. But we use `vfirst.m` to + get the `index` of `\0` position. We need to adjust `-1` + to get the correct remaining iLength for zero filling. */ + sub iTemp, iCurrentVL, iActiveElemPos + addi iTemp, iTemp, -1 + add iLength, iLength, iTemp + /* Have an earily return for `strlen(src) + 1 == count` case. */ + bnez iLength, 1f + ret +1: + sub pDstPtr, pDstPtr, iTemp + vsetvli zero, iLength, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma + vmv.v.x vStr2, zero + +L(fill_zero_loop): + vsetvli iVL, iLength, e8, ZERO_FILL_ELEM_LMUL_SETTING, ta, ma + vse8.v vStr2, (pDstPtr) + sub iLength, iLength, iVL + add pDstPtr, pDstPtr, iVL + bnez iLength, L(fill_zero_loop) + + ret + +END(strncpy) +libc_hidden_builtin_def (strncpy) From patchwork Fri Apr 21 07:33:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hau Hsu X-Patchwork-Id: 68104 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 D0A9F3857720 for ; Fri, 21 Apr 2023 07:33:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0A9F3857720 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682062412; bh=iXYYco1kbXOODD2dFTtsWq6yKtRa6g72VD/nuRu/hoo=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=My5oKX6YCfU7aQa/2jcoxC+pcRDIVwUZCBujHkscdGepMTmYzsbAeaTWuuM/69C9b Xdjn9nys0BJrn95gvoOk5JaONguPmI5GeQFjqGeJIbCLvrcMyp6IgN/YkscDKpetFs 8DPMl5OARTAFEVwaNMf3wLwJbtZ/2vsTPuNKSsgQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hhsu-imac.local (1-169-217-217.dynamic-ip.hinet.net [1.169.217.217]) by sourceware.org (Postfix) with ESMTP id 2D0F43856DC6 for ; Fri, 21 Apr 2023 07:33:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D0F43856DC6 Received: by hhsu-imac.local (Postfix, from userid 501) id 2866352C5C07; Fri, 21 Apr 2023 15:33:02 +0800 (CST) To: libc-alpha@sourceware.org, hongrong.hsu@sifive.com, jerry.shih@sifive.com, nick.knight@sifive.com, kito.cheng@sifive.com Cc: greentime.hu@sifive.com, alice.chan@sifive.com, andrew@sifive.com, vincent.chen@sifive.com, hau.hsu@sifive.com, Yun Hsiang Subject: [PATCH v2 5/5] riscv: add vectorized __memcmpeq Date: Fri, 21 Apr 2023 15:33:00 +0800 Message-Id: <20230421073300.14312-1-hau.hsu@sifive.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, KHOP_HELO_FCRDNS, NO_DNS_FOR_FROM, RCVD_IN_PBL, RCVD_IN_SORBS_DUL, RDNS_DYNAMIC, SPF_HELO_NONE, SPF_NONE, 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: 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: , X-Patchwork-Original-From: Hau Hsu via Libc-alpha From: Hau Hsu Reply-To: Hau Hsu Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" From: Yun Hsiang This patch proposes implementations of __memcmpeq that leverage the RISC-V V extension (RVV), version 1.0. These routines assumes VLEN is at least 32 bits, as is required by all currently defined vector extensions, and they support arbitrarily large VLEN. All implementations work for both RV32 and RV64 platforms, and make no assumptions about page size. --- sysdeps/riscv/rvv/memcmp.S | 4 --- sysdeps/riscv/rvv/memcmpeq.S | 69 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 sysdeps/riscv/rvv/memcmpeq.S diff --git a/sysdeps/riscv/rvv/memcmp.S b/sysdeps/riscv/rvv/memcmp.S index b156ec524c..74d8361293 100644 --- a/sysdeps/riscv/rvv/memcmp.S +++ b/sysdeps/riscv/rvv/memcmp.S @@ -69,7 +69,3 @@ L(found): END(memcmp) libc_hidden_builtin_def (memcmp) -weak_alias (memcmp,bcmp) -strong_alias (memcmp, __memcmpeq) -libc_hidden_def (__memcmpeq) - diff --git a/sysdeps/riscv/rvv/memcmpeq.S b/sysdeps/riscv/rvv/memcmpeq.S new file mode 100644 index 0000000000..302bca6992 --- /dev/null +++ b/sysdeps/riscv/rvv/memcmpeq.S @@ -0,0 +1,69 @@ +/* RVV versions memcmp. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Jerry Shih , + Yun Hsiang . + + 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 + + +#define iResult a0 + +#define pSrc1 a0 +#define pSrc2 a1 +#define iNum a2 + +#define iVL a3 +#define iTemp a4 + +#define ELEM_LMUL_SETTING m1 +#define vData1 v0 +#define vData2 v8 +#define vMask v16 + +ENTRY(__memcmpeq) + +L(loop): + vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma + + vle8.v vData1, (pSrc1) + vle8.v vData2, (pSrc2) + + vmsne.vv vMask, vData1, vData2 + sub iNum, iNum, iVL + vfirst.m iTemp, vMask + + // Skip the loop if we find the different value between pSrc1 and pSrc2. + bgez iTemp, L(found) + + add pSrc1, pSrc1, iVL + add pSrc2, pSrc2, iVL + + bnez iNum, L(loop) + + li iResult, 0 + ret + +L(found): + mv iResult, iVL + ret + +END(__memcmpeq) + +weak_alias (__memcmpeq, bcmp) +libc_hidden_def (__memcmpeq)