From patchwork Wed Mar 1 15:32:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Chen X-Patchwork-Id: 65847 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 B70E7385B514 for ; Wed, 1 Mar 2023 15:35:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B70E7385B514 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677684907; bh=tAuRdMkWjWLLMsdukHidxDyLHM6ALUTbHiqDGqJUvEU=; 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=NDs8LKs8baJKRfSwefWfxAB4EdldEvX6RV9EH9YDc4Pz8GZNMwQ1nCqzVZ/65vLge OdUbF4vKKUECvhGrJ3XvgF5N5ILJAtPkl8BjfJ/M+S2rbxcwyni1gABmLzECs18/xF 7AfJ7YfGI3hz+nH3utpjtxOI/IqReP/mPt5Rn6t0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 73DD33858002 for ; Wed, 1 Mar 2023 15:33:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73DD33858002 Received: by mail-pl1-x62a.google.com with SMTP id ky4so14407569plb.3 for ; Wed, 01 Mar 2023 07:33:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677684798; 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=tAuRdMkWjWLLMsdukHidxDyLHM6ALUTbHiqDGqJUvEU=; b=aoR5Z9QIiJHJqEkD4259FpOBuGleK91ITTLbAA/VXyTJFBwSoBRtdumEqvenqBTwWR F1hrM3G7cK2hSfjCT/tNmbQVADMYWli1LAM1N3mu7zsY3/oa/kczhwM6Ne3xtyUhEEfW he/hsf4PEqU1gGpqbP/eTI2rPee16cAh113SSaNCp8ihjtlzFvpHIfZJktHgSJnZ5M7E s0bFd5JN6nG1RZDrGScW9+cf6GhcJfhAbh8UU6zZeS/UmhKzVSclRrsApJ6v7sHjJsWJ xS+icFlppxm7V4VBsnRhUcZUZx0YLpScki8Riy9TpWb4OA/hKnpBdqXLgkQYTzkbI5CM UESg== X-Gm-Message-State: AO0yUKU9ZebfGNHN1JqIw5ildKxnkhRfJwyQjPUTJcGWHBhdsXEudLL1 38bx1Rx0cbJutEfexqcHN6K4T3s0AiTCy8VfYuBtc+30T0hcSRWQw4Uz+jovfWx6OKh/AY3fVmU iKrv8uwsbqnRsG0+IZymwOQ44P4A9e1+kj+3RbzcaG5zlJQ2dCmZkHMwmYJzYqtNpf6qa66DowN 2PWWFJ9Q== X-Google-Smtp-Source: AK7set9VW6QouE3Qkz4yuFG931nOEhRLSBqy+YEKq2+9LGMGcU6F9Tq77EFvGsKfaWhhiABndSY7UQ== X-Received: by 2002:a17:90a:6acf:b0:237:1b6a:dbce with SMTP id b15-20020a17090a6acf00b002371b6adbcemr7965411pjm.2.1677684798056; Wed, 01 Mar 2023 07:33:18 -0800 (PST) Received: from localhost.localdomain (111-251-213-204.dynamic-ip.hinet.net. [111.251.213.204]) by smtp.gmail.com with ESMTPSA id a6-20020a17090a740600b002345ef591dasm8058025pjg.31.2023.03.01.07.33.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 07:33:17 -0800 (PST) To: libc-alpha@sourceware.org, palmer@dabbelt.com, darius@bluespec.com, andrew@sifive.com, dj@redhat.com Cc: jerry.shih@sifive.com, nick.knight@sifive.com, hongrong.hsu@sifive.com, hau.hsu@sifive.com, kito.cheng@sifive.com, vincent.chen@sifive.com, greentime.hu@sifive.com Subject: [PATCH 4/4] riscv: vectorized strchr and strnlen functions Date: Wed, 1 Mar 2023 23:32:47 +0800 Message-Id: <20230301153247.1499566-5-vincent.chen@sifive.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230301153247.1499566-1-vincent.chen@sifive.com> References: <20230301153247.1499566-1-vincent.chen@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 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 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: Vincent Chen via Libc-alpha From: Vincent Chen Reply-To: Vincent Chen 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)