From patchwork Thu May 4 07:48:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hau Hsu X-Patchwork-Id: 68736 X-Patchwork-Delegate: palmer@dabbelt.com 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 2A684385293A for ; Thu, 4 May 2023 07:50:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A684385293A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683186631; bh=jse2fHFH9fc3qdt9mCb8n03g+nxcsHPRJrpueR7/mlg=; 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=Ul77m88suQB3PR++MqF41jckqmu0KFTbDFGJlcPH1i58TQxZWJj0D0UT//4m4yD4B snjU8JVqT+yu9mQLOIzJWVX+BHsjJk6zw0/StVZBZDSF5RqzfmQVEuceFWRCTR51BS jQHnM/s6vhkTmWxYmXXNLL9g/Iwycvey5Mqb1Edg= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 6B8F63857005 for ; Thu, 4 May 2023 07:49:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6B8F63857005 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-64359d9c531so165847b3a.3 for ; Thu, 04 May 2023 00:49:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683186564; x=1685778564; 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=jse2fHFH9fc3qdt9mCb8n03g+nxcsHPRJrpueR7/mlg=; b=AJw/4guB7xlrEBKXZnPaae+yAxWRiJ7np5482OoIp84ASMm7MUushH9QtHa8OwmL2d fzLWgcyJhETcSqK2KhYGo77chpXkFhl84gaX7MCMFQcEgh8yWfLFCcbpUGAFj5tuCsAp S4SqJjjUpMTk9Q8QnpDOdKYMwQPWyRzj+oTVA3Bz3ZOq0peOH+FGu0yAHqDwxwXwlHzX wS/UhmVZ30VsxhdqJfJgktKhBdFicjJaoCrz+DOdXnLFPq8Oh6G4CwmoNNtLAcRhQJo0 +FnpHQA/ZaxPfN+oYF36N5AnbDhUFH+UaYpcFlL16r7bqhHAtq7rjhQqdqoNEMQjTnRV r3zA== X-Gm-Message-State: AC+VfDwN/YknRvs/7yWuwTuIBQb3RQ3x8+vejtH4akfAB2hvG5OGJj2m unwaElT7n1Gn/hoJPskARuOrk/l3w90OClWyUsOQJmooSEmpT1fdpdCjabK08mBz0P7uNNZ61tF 7TvxjL7uestpJYeeJNavQUv4IoEXlUCE3KXyZAUkqJKmnMvG1Q7q6XEo25GevHNCkNZMAHrKIvz pEKg== X-Google-Smtp-Source: ACHHUZ7//r5I0CMGLWh6RCZWxu8ajq8yqQJqZXwiYuSQYluHZ0NMnfu2zRqkolDqQH7FngYgyLZ6Mg== X-Received: by 2002:a17:902:ea95:b0:1a6:6fe3:df91 with SMTP id x21-20020a170902ea9500b001a66fe3df91mr2744470plb.50.1683186563982; Thu, 04 May 2023 00:49:23 -0700 (PDT) Received: from localhost.localdomain (36-238-22-214.dynamic-ip.hinet.net. [36.238.22.214]) by smtp.gmail.com with ESMTPSA id y18-20020a17090322d200b001ab06958770sm4875294plg.161.2023.05.04.00.49.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 May 2023 00:49:23 -0700 (PDT) To: libc-alpha@sourceware.org Cc: hau.hsu@sifive.com, kito.cheng@sifive.com, nick.knight@sifive.com, jerry.shih@sifive.com, vincent.chen@sifive.com, hongrong.hsu@sifive.com Subject: [PATCH v3 4/5] riscv: vectorized strchr and strnlen functions Date: Thu, 4 May 2023 15:48:50 +0800 Message-Id: <20230504074851.38763-5-hau.hsu@sifive.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230504074851.38763-1-hau.hsu@sifive.com> References: <20230504074851.38763-1-hau.hsu@sifive.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, 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 | 62 +++++++++++++++++++++++++++++++++++++ sysdeps/riscv/rvv/strnlen.S | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 117 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..053923d3d7 --- /dev/null +++ b/sysdeps/riscv/rvv/strchr.S @@ -0,0 +1,62 @@ +/* RVV versions strchr. RISC-V version. + 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 + +#define str a0 +#define ch a1 +#define end_offset a2 +#define ch_offset a3 +#define temp1 a4 +#define temp2 a5 +#define cur_vl a6 +#define ivl t0 + +#define ELEM_LMUL_SETTING m1 +#define vstr v0 +#define vmask_end v8 +#define vmask_ch v9 + +ENTRY(strchr) + +L(strchr_loop): + vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vstr, (str) + vmseq.vi vmask_end, vstr, 0 + vmseq.vx vmask_ch, vstr, ch + vfirst.m end_offset, vmask_end /* first occurrence of \0 */ + vfirst.m ch_offset, vmask_ch /* first occurrence of ch */ + sltz temp1, ch_offset + sltu temp2, end_offset, ch_offset + or temp1, temp1, temp2 + beqz temp1, L(found_ch) /* Found ch, not preceded by \0? */ + csrr cur_vl, vl + add str, str, cur_vl + bltz end_offset, L(strchr_loop) /* Didn't find \0? */ + li str, 0 + ret +L(found_ch): + add str, str, ch_offset + 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..b902ae0fd4 --- /dev/null +++ b/sysdeps/riscv/rvv/strnlen.S @@ -0,0 +1,55 @@ +/* RVV versions strnlen. RISC-V version. + 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 + +#define str a0 +#define copy_str a2 +#define ret_value a0 +#define max_len a1 +#define cur_vl a3 +#define end_offset a4 + +#define ELEM_LMUL_SETTING m1 +#define vstr v0 +#define vmask_end v8 + +ENTRY(__strnlen) + + mv copy_str, str + mv ret_value, max_len +L(strnlen_loop): + beqz max_len, L(end_strnlen_loop) + vsetvli zero, max_len, e8, ELEM_LMUL_SETTING, ta, ma + vle8ff.v vstr, (copy_str) + vmseq.vi vmask_end, vstr, 0 + vfirst.m end_offset, vmask_end /* first occurence of \0 */ + csrr cur_vl, vl + add copy_str, copy_str, cur_vl + sub max_len, max_len, cur_vl + bltz end_offset, L(strnlen_loop) + add max_len, max_len, cur_vl + sub ret_value, ret_value, max_len + add ret_value, ret_value, end_offset +L(end_strnlen_loop): + ret +END(__strnlen) +weak_alias (__strnlen, strnlen) +libc_hidden_builtin_def (strnlen) +libc_hidden_builtin_def (__strnlen)