public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Hau Hsu <hau.hsu@sifive.com>
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	[thread overview]
Message-ID: <20230504074851.38763-5-hau.hsu@sifive.com> (raw)
In-Reply-To: <20230504074851.38763-1-hau.hsu@sifive.com>

From: Nick Knight <nick.knight@sifive.com>

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
+   <http://www.gnu.org/licenses/>.  */
+
+
+#include <sysdep.h>
+#include <sys/asm.h>
+
+#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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#include <sys/asm.h>
+
+#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)
-- 
2.38.1


  parent reply	other threads:[~2023-05-04  7:49 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-04  7:48 [PATCH v3 0/5] riscv: Vectorized mem*/str* function Hau Hsu
2023-05-04  7:48 ` [PATCH v3 1/5] riscv: Enabling vectorized mem*/str* functions in build time Hau Hsu
2023-05-04  7:48 ` [PATCH v3 2/5] riscv: vectorized mem* functions Hau Hsu
2023-05-04  7:48 ` [PATCH v3 3/5] riscv: vectorized str* functions Hau Hsu
2023-05-04  7:48 ` Hau Hsu [this message]
2023-05-04  7:48 ` [PATCH v3 5/5] riscv: vectorized __memcmpeq function Hau Hsu
2023-05-08 14:06 ` [PATCH v3 0/5] riscv: Vectorized mem*/str* function Palmer Dabbelt
2023-05-10  9:01   ` Hau Hsu
2023-05-10 12:28     ` Sergei Lewis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230504074851.38763-5-hau.hsu@sifive.com \
    --to=hau.hsu@sifive.com \
    --cc=hongrong.hsu@sifive.com \
    --cc=jerry.shih@sifive.com \
    --cc=kito.cheng@sifive.com \
    --cc=libc-alpha@sourceware.org \
    --cc=nick.knight@sifive.com \
    --cc=vincent.chen@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).