From patchwork Fri Apr 21 07:54:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hau Hsu X-Patchwork-Id: 68111 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 698C4385383B for ; Fri, 21 Apr 2023 07:55:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 698C4385383B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682063737; bh=qKkRybMvAZHb34vEduXFG/+Jq5aZhVo6NLnTe937+Xc=; 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=u0a0OMnJAW3TaCn7+/y6OOADyow9zdrioCgR9Wq5lhbrOf+7mTicSvvdxqeOFTe89 aNRG/uaKz91Ek1MFkcgBgmD85OOZnSbEU6GLVQZwY71Jg8wTSaZ2Dk4hRrvgCKS2A2 QOf5faKzZJyZp6r/AZYe+VTX3X0bZo65MhMcSvmQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 148D03857027 for ; Fri, 21 Apr 2023 07:54:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 148D03857027 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-63b51fd2972so1644996b3a.3 for ; Fri, 21 Apr 2023 00:54:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682063670; x=1684655670; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qKkRybMvAZHb34vEduXFG/+Jq5aZhVo6NLnTe937+Xc=; b=XoTQOzqHC0tPx9iEo1/WZDgwXtT98tz9WqkENX2AT42qMiFJrJiR8ulOapAU3yA8zi wi2vvxOcTZVjmFYB6BueMwYRBMzNCCdX6M8OZgfeWLgTlbhSpjLXWFaVuPKSlVOPEdxK Q78P/SMF9XkhEO5G5eBYDd/gurGU9/y3ibSk81Y5jp62mxEotPCI8r6wwgS/4Iun174S x+qDwNHzCZm8eNfoxPB2T2ojz/bYcIrgkoCTn30/iwM9sbOH1F1e0YxeHr6HxfNI89mt 982a3YHTCU4z4+ozI4xejanD4k5enTfIDwKgYsFQX2eLz6G7dw3RiBbuSXV2TGeVmr0J 4LYg== X-Gm-Message-State: AAQBX9eNsAdbiKQCNFeu3DQWchkFWGr/FxfPpZTwYdGyco40Iv8SpD4X 4EmO9vnLP9u0AKWejhdip8PUl+8Z67dIQoRZbU1raxOWxjNd0M5szYTWN6eea3zi4UuHD9YrYcW aGgm5Od45iUnOHo0rLVPs3DZc7cNJ99h8TlkWmm0dsOcz25ZqziMhKe5jtAktVjguLjLKn660Qh 5QO0kr X-Google-Smtp-Source: AKy350YLKRbwZQJG4wf2TedVp35sokdAHwQP0rGQdgMY62xbXQFKXGZwvzrWEItTOz0B6Ek1uRXBQg== X-Received: by 2002:a05:6a20:6a28:b0:ef:cd5b:a5c7 with SMTP id p40-20020a056a206a2800b000efcd5ba5c7mr6054019pzk.56.1682063669676; Fri, 21 Apr 2023 00:54:29 -0700 (PDT) Received: from localhost.localdomain (1-169-217-217.dynamic-ip.hinet.net. [1.169.217.217]) by smtp.gmail.com with ESMTPSA id fa23-20020a056a002d1700b006259e883ee9sm650992pfb.189.2023.04.21.00.54.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Apr 2023 00:54:29 -0700 (PDT) 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 4/5] riscv: vectorized strchr and strnlen functions Date: Fri, 21 Apr 2023 15:54:04 +0800 Message-Id: <20230421075405.14892-5-hau.hsu@sifive.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230421075405.14892-1-hau.hsu@sifive.com> References: <20230421075405.14892-1-hau.hsu@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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: 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: Nick Knight 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/strchr.S | 53 +++++++++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strnlen.S | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 sysdeps/riscv/rvv/strchr.S create mode 100644 sysdeps/riscv/rvv/strnlen.S diff --git a/sysdeps/riscv/rvv/strchr.S b/sysdeps/riscv/rvv/strchr.S new file mode 100644 index 0000000000..4a660200c3 --- /dev/null +++ b/sysdeps/riscv/rvv/strchr.S @@ -0,0 +1,53 @@ +/* RISC-V multiarch strchr, V-extension version. + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Nick Knight . + + 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 + + + + +ENTRY(strchr) +0: + vsetvli t0, zero, e8, m8, ta, ma + vle8ff.v v0, (a0) + vmseq.vi v8, v0, 0 + vmseq.vx v9, v0, a1 + vfirst.m a2, v8 /* first occurrence of \0 */ + vfirst.m a3, v9 /* first occurrence of ch */ + addi a4, a3, 1 + seqz a4, a4 + sltu a5, a2, a3 + or a4, a4, a5 + beqz a4, 1f /* Found ch, not preceded by \0? */ + li a6, -1 + csrr a5, vl + add a0, a0, a5 + beq a2, a6, 0b /* Didn't find \0? */ + li a0, 0 + ret +1: + add a0, a0, a3 + ret + +END(strchr) +weak_alias (strchr, index) +libc_hidden_builtin_def (strchr) + + diff --git a/sysdeps/riscv/rvv/strnlen.S b/sysdeps/riscv/rvv/strnlen.S new file mode 100644 index 0000000000..c1ce12baa5 --- /dev/null +++ b/sysdeps/riscv/rvv/strnlen.S @@ -0,0 +1,56 @@ +/* RVV versions strnlen. RISC-V version. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by: Nick Knight + + 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 pStr a0 +#define pCopyStr a2 +#define iRetValue a0 +#define iMaxlen a1 +#define iCurrentVL a3 +#define iEndOffset a4 + +#define ELEM_LMUL_SETTING m1 +#define vStr v0 +#define vMaskEnd v8 + +ENTRY(__strnlen) + + mv pCopyStr, pStr + mv iRetValue, iMaxlen +L(strnlen_loop): + beqz iMaxlen, L(end_strnlen_loop) + vsetvli zero, iMaxlen, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vStr, (pCopyStr) + vmseq.vi vMaskEnd, vStr, 0 + vfirst.m iEndOffset, vMaskEnd /* first occurence of \0 */ + csrr iCurrentVL, vl + add pCopyStr, pCopyStr, iCurrentVL + sub iMaxlen, iMaxlen, iCurrentVL + bltz iEndOffset, L(strnlen_loop) + add iMaxlen, iMaxlen, iCurrentVL + sub iRetValue, iRetValue, iMaxlen + add iRetValue, iRetValue, iEndOffset +L(end_strnlen_loop): + ret +END(__strnlen) +weak_alias (__strnlen, strnlen) +libc_hidden_builtin_def (strnlen) +libc_hidden_builtin_def (__strnlen)