[v3,4/5] riscv: vectorized strchr and strnlen functions

Message ID 20230504074851.38763-5-hau.hsu@sifive.com
State Changes Requested, archived
Delegated to: Palmer Dabbelt
Headers
Series riscv: Vectorized mem*/str* function |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Hau Hsu May 4, 2023, 7:48 a.m. UTC
  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
  

Patch

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)