public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v12 00/31] Improve generic string routines
@ 2023-02-02 18:11 Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 01/31] Parameterize op_t from memcopy.h Adhemerval Zanella
                   ` (31 more replies)
  0 siblings, 32 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It is done by:

  1. Parametrizing the internal routines (for instance the find zero
     in a word) so each architecture can reimplement without the need
     to reimplement the whole routine.

  2. Vectorizing more string implementations (for instance strcpy
     and strcmp).

  3. Change some implementations to use already possible optimized
     ones (strnlen and strchr).  It makes new ports to focus on
     only provide optimized implementation of a hardful symbols
     (for instance memchr) and make its improvement to be used in
     a larger set of routines.

I checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multiarch (it covers both LE and BE
for 64 and 32 bits). I also checked the string routines on alpha, hppa,
and sh.

Changes since v11:
  * Use index_first_zero_ne strcmp/strncmp, and fixed it on LE.
  * Added strrchr optimization based on strlen/memrchr.
  * Reorder the patches so composed optimizations (such as strrchr) as
    ordered later.

Changes since v10:
  * Added strcpy and stpcpy optimization.
  * Added RISCV __riscv_zbb support to lower ctz/clz/orc.b.
  * Fixed test-strnlen name.

Changes since v9:
  * Added strncmp optimization.
  * Fixed wcsmbs regressions.

Changes since v8:
  * Change memrchr to use vectorized load on final string, instead of
    byte per byte reads.
  * Remove string-maskoff.h header.
  * Add string-repeat_bytes.h and string-shift.h.
  * Hook up the generic implementation on string tests.

Changes since v7:
  * Split string-fzc.h out of string-fzi.h, with all of the
    routines that are combinations of fza and fzi routines.
  * Fix missing find_t and shift_find() from alpha, arm, powerpc.
  * Use compiler builtins for arm and powerpc.
  * Define sh4 has_zero() via has_eq(), rather than reverse.

Changes since v6:
  * Add find_t to handle alpha way of comapring bytes (which returns
    a bit-mask instead of byte-mask).
  * Fixed alpha string-fzi.h and added string-fza.h.
  * Renamed check_mask to shift_find.

Changes since v5:
  * Replace 'inline' with '__always_inline' macros.
  * Replace strchr implementation with a simpler one that call
    strchrnul.
  * Add strchrnul suggested changes.
  * Add memchr suggested changes.
  * Added check_mask on string-maskoff.h.
  * Rebase and update Copyright years.

Changes since v4:
  * Removed __clz and __ctz in favor of count_leading_zero and
    count_trailing_zeros from longlong.h.
  * Use repeat_bytes more often.
  * Added a comment on strcmp final_cmp on why index_first_zero_ne can
    not be used.

Changes since v3:
  * Rebased against master.
  * Dropped strcpy optimization.
  * Refactor strcmp implementation.
  * Some minor changes in comments.

Changes since v2:
  * Move string-fz{a,b,i} to its own patch.
  * Add a inline implementation for __builtin_c{l,t}z to avoid using
    compiler provided symbols.
  * Add a new header, string-maskoff.h, to handle unaligned accesses
    on some implementation.
  * Fixed strcmp on LE machines.
  * Added a unaligned strcpy variant for architecture that define
    _STRING_ARCH_unaligned.
  * Add SH string-fzb.h (which uses cmp/str instruction to find
    a zero in word).

Changes since v1:
  * Marked ChangeLog entries with [BZ #5806], as appropriate.
  * Reorganized the headers, so that armv6t2 and power6 need override
    as little as possible to use their (integer) zero detection insns.
  * Hopefully fixed all of the coding style issues.
  * Adjusted the memrchr algorithm as discussed.
  * Replaced the #ifdef STRRCHR etc that are used by the multiarch
  * files.
  * Tested on i386, i686, x86_64 (verified this is unused), ppc64,
    ppc64le --with-cpu=power8 (to use power6 in multiarch), armv7,
    aarch64, alpha (qemu) and hppa (qemu).

Adhemerval Zanella (25):
  Parameterize op_t from memcopy.h
  Add string vectorized find and detection functions
  string: Improve generic strlen
  string: Improve generic strchrnul
  string: Improve generic strchr
  string: Improve generic strcmp
  string: Improve generic strncmp
  string: Improve generic stpcpy
  string: Improve generic strcpy
  string: Improve generic memchr
  string: Improve generic strnlen with memchr
  string: Improve generic memrchr
  string: Improve generic strrchr with memrchr and strlen
  sh: Add string-fzb.h
  riscv: Add string-fza.h and string-fzi.h
  string: Hook up the default implementation on test-strlen
  string: Hook up the default implementation on test-strnlen
  string: Hook up the default implementation on test-strchr
  string: Hook up the default implementation on test-strcmp
  string: Hook up the default implementation on test-strncmp
  string: Hook up the default implementation on test-stpcpy
  string: Hook up the default implementation on test-strcpy
  string: Hook up the default implementation on test-memchr
  string: Hook up the default implementation on test-memrchr
  string: Hook up the default implementation on test-strrchr

Richard Henderson (6):
  Parameterize OP_T_THRES from memcopy.h
  hppa: Add memcopy.h
  hppa: Add string-fza.h, string-fzc.h, and string-fzi.h
  alpha: Add string-fza, string-fzb.h, string-fzi.h, and string-shift.h
  arm: Add string-fza.h
  powerpc: Add string-fza.h

 string/memchr.c                               | 176 +++++-----------
 string/memcmp.c                               |   4 -
 string/memrchr.c                              | 196 ++++--------------
 string/stpcpy.c                               |  92 +++++++-
 string/strchr.c                               | 164 +--------------
 string/strchrnul.c                            | 155 ++------------
 string/strcmp.c                               | 110 ++++++++--
 string/strcpy.c                               |   6 +-
 string/strlen.c                               |  92 ++------
 string/strncmp.c                              | 138 ++++++++----
 string/strnlen.c                              | 137 +-----------
 string/strrchr.c                              |  18 +-
 string/test-memchr.c                          |  31 ++-
 string/test-memrchr.c                         |   7 +
 string/test-stpcpy.c                          |  32 ++-
 string/test-strchr.c                          |  53 +++--
 string/test-strcmp.c                          |  22 ++
 string/test-strcpy.c                          |  34 ++-
 string/test-strlen.c                          |  31 ++-
 string/test-strncmp.c                         |  16 ++
 string/test-strnlen.c                         |  35 +++-
 string/test-strrchr.c                         |  38 ++--
 sysdeps/alpha/string-fza.h                    |  61 ++++++
 sysdeps/alpha/string-fzb.h                    |  52 +++++
 sysdeps/alpha/string-fzi.h                    |  62 ++++++
 sysdeps/alpha/string-shift.h                  |  44 ++++
 sysdeps/arm/armv6t2/string-fza.h              |  68 ++++++
 sysdeps/generic/memcopy.h                     |  10 +-
 sysdeps/generic/string-fza.h                  | 104 ++++++++++
 sysdeps/generic/string-fzb.h                  |  49 +++++
 sysdeps/generic/string-fzc.h                  |  83 ++++++++
 sysdeps/generic/string-fzi.h                  |  71 +++++++
 sysdeps/generic/string-misc.h                 |  45 ++++
 sysdeps/generic/string-opthr.h                |  25 +++
 sysdeps/generic/string-optype.h               |  24 +++
 sysdeps/generic/string-shift.h                |  52 +++++
 sysdeps/hppa/memcopy.h                        |  42 ++++
 sysdeps/hppa/string-fzb.h                     |  63 ++++++
 sysdeps/hppa/string-fzc.h                     | 124 +++++++++++
 sysdeps/hppa/string-fzi.h                     |  63 ++++++
 sysdeps/i386/i686/multiarch/strnlen-c.c       |  14 +-
 sysdeps/i386/memcopy.h                        |   3 -
 sysdeps/i386/string-opthr.h                   |  25 +++
 sysdeps/m68k/memcopy.h                        |   3 -
 sysdeps/powerpc/powerpc32/power4/memcopy.h    |   5 -
 .../powerpc32/power4/multiarch/memchr-ppc32.c |  14 +-
 .../power4/multiarch/strchrnul-ppc32.c        |   4 -
 .../power4/multiarch/strnlen-ppc32.c          |  14 +-
 .../powerpc64/multiarch/memchr-ppc64.c        |   9 +-
 sysdeps/powerpc/string-fza.h                  |  72 +++++++
 sysdeps/riscv/string-fza.h                    |  70 +++++++
 sysdeps/riscv/string-fzi.h                    |  77 +++++++
 sysdeps/s390/strchr-c.c                       |  11 +-
 sysdeps/s390/strchrnul-c.c                    |   2 -
 sysdeps/s390/strlen-c.c                       |  10 +-
 sysdeps/s390/strnlen-c.c                      |  14 +-
 sysdeps/sh/string-fzb.h                       |  55 +++++
 sysdeps/x86_64/x32/string-optype.h            |  24 +++
 58 files changed, 2041 insertions(+), 1014 deletions(-)
 create mode 100644 sysdeps/alpha/string-fza.h
 create mode 100644 sysdeps/alpha/string-fzb.h
 create mode 100644 sysdeps/alpha/string-fzi.h
 create mode 100644 sysdeps/alpha/string-shift.h
 create mode 100644 sysdeps/arm/armv6t2/string-fza.h
 create mode 100644 sysdeps/generic/string-fza.h
 create mode 100644 sysdeps/generic/string-fzb.h
 create mode 100644 sysdeps/generic/string-fzc.h
 create mode 100644 sysdeps/generic/string-fzi.h
 create mode 100644 sysdeps/generic/string-misc.h
 create mode 100644 sysdeps/generic/string-opthr.h
 create mode 100644 sysdeps/generic/string-optype.h
 create mode 100644 sysdeps/generic/string-shift.h
 create mode 100644 sysdeps/hppa/memcopy.h
 create mode 100644 sysdeps/hppa/string-fzb.h
 create mode 100644 sysdeps/hppa/string-fzc.h
 create mode 100644 sysdeps/hppa/string-fzi.h
 create mode 100644 sysdeps/i386/string-opthr.h
 create mode 100644 sysdeps/powerpc/string-fza.h
 create mode 100644 sysdeps/riscv/string-fza.h
 create mode 100644 sysdeps/riscv/string-fzi.h
 create mode 100644 sysdeps/sh/string-fzb.h
 create mode 100644 sysdeps/x86_64/x32/string-optype.h

-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 01/31] Parameterize op_t from memcopy.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 02/31] Parameterize OP_T_THRES " Adhemerval Zanella
                   ` (30 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It moves the op_t definition out to an specific header, adds
the attribute 'may-alias', and cleanup its duplicated definitions.

Checked with a build and check with run-built-tests=no for all major
Linux ABIs.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/memcmp.c                    |  1 -
 sysdeps/generic/memcopy.h          |  6 ++----
 sysdeps/generic/string-optype.h    | 24 ++++++++++++++++++++++++
 sysdeps/x86_64/x32/string-optype.h | 24 ++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 5 deletions(-)
 create mode 100644 sysdeps/generic/string-optype.h
 create mode 100644 sysdeps/x86_64/x32/string-optype.h

diff --git a/string/memcmp.c b/string/memcmp.c
index 067b2e6a42..ea0fa03e1c 100644
--- a/string/memcmp.c
+++ b/string/memcmp.c
@@ -46,7 +46,6 @@
 /* Type to use for aligned memory operations.
    This should normally be the biggest type supported by a single load
    and store.  Must be an unsigned type.  */
-# define op_t	unsigned long int
 # define OPSIZ	(sizeof (op_t))
 
 /* Threshold value for when to enter the unrolled loops.  */
diff --git a/sysdeps/generic/memcopy.h b/sysdeps/generic/memcopy.h
index 9f3ffb5d30..b5ffa4d114 100644
--- a/sysdeps/generic/memcopy.h
+++ b/sysdeps/generic/memcopy.h
@@ -55,10 +55,8 @@
      [I fail to understand.  I feel stupid.  --roland]
 */
 
-/* Type to use for aligned memory operations.
-   This should normally be the biggest type supported by a single load
-   and store.  */
-#define	op_t	unsigned long int
+/* Type to use for aligned memory operations.  */
+#include <string-optype.h>
 #define OPSIZ	(sizeof (op_t))
 
 /* Type to use for unaligned operations.  */
diff --git a/sysdeps/generic/string-optype.h b/sysdeps/generic/string-optype.h
new file mode 100644
index 0000000000..42bdd2a145
--- /dev/null
+++ b/sysdeps/generic/string-optype.h
@@ -0,0 +1,24 @@
+/* Define a type to use for word access.  Generic 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/>.  */
+
+#ifndef _STRING_OPTYPE_H
+#define _STRING_OPTYPE_H 1
+
+typedef unsigned long int __attribute__ ((__may_alias__)) op_t;
+
+#endif /* string-optype.h */
diff --git a/sysdeps/x86_64/x32/string-optype.h b/sysdeps/x86_64/x32/string-optype.h
new file mode 100644
index 0000000000..e7679f934f
--- /dev/null
+++ b/sysdeps/x86_64/x32/string-optype.h
@@ -0,0 +1,24 @@
+/* Define a type to use for word access.  Generic 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/>.  */
+
+#ifndef _STRING_OPTYPE_H
+#define _STRING_OPTYPE_H 1
+
+typedef unsigned long long int __attribute__ ((__may_alias__)) op_t;
+
+#endif /* string-optype.h */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 02/31] Parameterize OP_T_THRES from memcopy.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 01/31] Parameterize op_t from memcopy.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 03/31] Add string vectorized find and detection functions Adhemerval Zanella
                   ` (29 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein
  Cc: Carlos O'Donell

From: Richard Henderson <richard.henderson@linaro.org>

It moves OP_T_THRES out of memcopy.h to its own header and adjust
each architecture that redefines it.

Checked with a build and check with run-built-tests=no for all major
Linux ABIs.

Co-authored-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/memcmp.c                            |  3 ---
 sysdeps/generic/memcopy.h                  |  4 +---
 sysdeps/generic/string-opthr.h             | 25 ++++++++++++++++++++++
 sysdeps/i386/memcopy.h                     |  3 ---
 sysdeps/i386/string-opthr.h                | 25 ++++++++++++++++++++++
 sysdeps/m68k/memcopy.h                     |  3 ---
 sysdeps/powerpc/powerpc32/power4/memcopy.h |  5 -----
 7 files changed, 51 insertions(+), 17 deletions(-)
 create mode 100644 sysdeps/generic/string-opthr.h
 create mode 100644 sysdeps/i386/string-opthr.h

diff --git a/string/memcmp.c b/string/memcmp.c
index ea0fa03e1c..047ca4f98e 100644
--- a/string/memcmp.c
+++ b/string/memcmp.c
@@ -48,9 +48,6 @@
    and store.  Must be an unsigned type.  */
 # define OPSIZ	(sizeof (op_t))
 
-/* Threshold value for when to enter the unrolled loops.  */
-# define OP_T_THRES	16
-
 /* Type to use for unaligned operations.  */
 typedef unsigned char byte;
 
diff --git a/sysdeps/generic/memcopy.h b/sysdeps/generic/memcopy.h
index b5ffa4d114..e9b3f227b2 100644
--- a/sysdeps/generic/memcopy.h
+++ b/sysdeps/generic/memcopy.h
@@ -57,6 +57,7 @@
 
 /* Type to use for aligned memory operations.  */
 #include <string-optype.h>
+#include <string-opthr.h>
 #define OPSIZ	(sizeof (op_t))
 
 /* Type to use for unaligned operations.  */
@@ -188,9 +189,6 @@ extern void _wordcopy_bwd_dest_aligned (long int, long int, size_t)
 
 #endif
 
-/* Threshold value for when to enter the unrolled loops.  */
-#define	OP_T_THRES	16
-
 /* Set to 1 if memcpy is safe to use for forward-copying memmove with
    overlapping addresses.  This is 0 by default because memcpy implementations
    are generally not safe for overlapping addresses.  */
diff --git a/sysdeps/generic/string-opthr.h b/sysdeps/generic/string-opthr.h
new file mode 100644
index 0000000000..6f10a98edd
--- /dev/null
+++ b/sysdeps/generic/string-opthr.h
@@ -0,0 +1,25 @@
+/* Define a threshold for word access.  Generic 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/>.  */
+
+#ifndef _STRING_OPTHR_H
+#define _STRING_OPTHR_H 1
+
+/* Threshold value for when to enter the unrolled loops.  */
+#define OP_T_THRES	16
+
+#endif /* string-opthr.h */
diff --git a/sysdeps/i386/memcopy.h b/sysdeps/i386/memcopy.h
index 4f82689b84..1aa7c3a850 100644
--- a/sysdeps/i386/memcopy.h
+++ b/sysdeps/i386/memcopy.h
@@ -18,9 +18,6 @@
 
 #include <sysdeps/generic/memcopy.h>
 
-#undef	OP_T_THRES
-#define	OP_T_THRES	8
-
 #undef	BYTE_COPY_FWD
 #define BYTE_COPY_FWD(dst_bp, src_bp, nbytes)				      \
   do {									      \
diff --git a/sysdeps/i386/string-opthr.h b/sysdeps/i386/string-opthr.h
new file mode 100644
index 0000000000..ed3e4b2ddb
--- /dev/null
+++ b/sysdeps/i386/string-opthr.h
@@ -0,0 +1,25 @@
+/* Define a threshold for word access.  i386 version.
+   Copyright (C) 2018 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/>.  */
+
+#ifndef I386_STRING_OPTHR_H
+#define I386_STRING_OPTHR_H 1
+
+/* Threshold value for when to enter the unrolled loops.  */
+#define OP_T_THRES	8
+
+#endif /* I386_STRING_OPTHR_H */
diff --git a/sysdeps/m68k/memcopy.h b/sysdeps/m68k/memcopy.h
index accd81c1c3..610577071d 100644
--- a/sysdeps/m68k/memcopy.h
+++ b/sysdeps/m68k/memcopy.h
@@ -20,9 +20,6 @@
 
 #if	defined(__mc68020__) || defined(mc68020)
 
-#undef	OP_T_THRES
-#define	OP_T_THRES	16
-
 /* WORD_COPY_FWD and WORD_COPY_BWD are not symmetric on the 68020,
    because of its weird instruction overlap characteristics.  */
 
diff --git a/sysdeps/powerpc/powerpc32/power4/memcopy.h b/sysdeps/powerpc/powerpc32/power4/memcopy.h
index 384f33b029..872157e485 100644
--- a/sysdeps/powerpc/powerpc32/power4/memcopy.h
+++ b/sysdeps/powerpc/powerpc32/power4/memcopy.h
@@ -50,11 +50,6 @@
      [I fail to understand.  I feel stupid.  --roland]
 */
 
-
-/* Threshold value for when to enter the unrolled loops.  */
-#undef	OP_T_THRES
-#define OP_T_THRES 16
-
 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
    without any assumptions about alignment of the pointers.  */
 #undef BYTE_COPY_FWD
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 03/31] Add string vectorized find and detection functions
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 01/31] Parameterize op_t from memcopy.h Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 02/31] Parameterize OP_T_THRES " Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03  0:24   ` Richard Henderson
  2023-02-02 18:11 ` [PATCH v12 04/31] string: Improve generic strlen Adhemerval Zanella
                   ` (28 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

This patch adds generic string find and detection meant to be used in
generic vectorized string implementation.  The idea is to decompose the
basic string operation so each architecture can reimplement if it
provides any specialized hardware instruction.

The 'string-misc.h' provides miscellaneous functions:

  - extractbyte: extracts the byte from an specific index.
  - repeat_bytes: setup an word by replicate the argument on each byte.

The 'string-fza.h' provides zero byte detection functions:

  - find_zero_low, find_zero_all, find_eq_low, find_eq_all,
    find_zero_eq_low, find_zero_eq_all, find_zero_ne_low,
    and find_zero_ne_all

The 'string-fzb.h' provides boolean zero byte detection functions:

  - has_zero: determine if any byte within a word is zero.
  - has_eq: determine byte equality between two words.
  - has_zero_eq: determine if any byte within a word is zero along with
    byte equality between two words.

The 'string-fzi.h' provides positions for string-fza.h results:

  - index_first: return index of first zero byte within a word.
  - index_last: return index of first byte different between two words.

The 'string-fzc.h' provides a combined version of fza and fzi:

  - index_first_zero_eq: return index of first zero byte within a word or
    first byte different between two words.
  - index_first_zero_ne: return index of first zero byte within a word or
    first byte equal between two words.
  - index_last_zero: return index of last zero byte within a word.
  - index_last_eq: return index of last byte different between two words.

The 'string-shift.h' provides a way to mask off parts of a work based on
some alignmnet (to handle unaligned arguments):

  - shift_find, shift_find_last.

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
---
 sysdeps/generic/string-fza.h   | 104 +++++++++++++++++++++++++++++++++
 sysdeps/generic/string-fzb.h   |  49 ++++++++++++++++
 sysdeps/generic/string-fzc.h   |  83 ++++++++++++++++++++++++++
 sysdeps/generic/string-fzi.h   |  71 ++++++++++++++++++++++
 sysdeps/generic/string-misc.h  |  45 ++++++++++++++
 sysdeps/generic/string-shift.h |  52 +++++++++++++++++
 6 files changed, 404 insertions(+)
 create mode 100644 sysdeps/generic/string-fza.h
 create mode 100644 sysdeps/generic/string-fzb.h
 create mode 100644 sysdeps/generic/string-fzc.h
 create mode 100644 sysdeps/generic/string-fzi.h
 create mode 100644 sysdeps/generic/string-misc.h
 create mode 100644 sysdeps/generic/string-shift.h

diff --git a/sysdeps/generic/string-fza.h b/sysdeps/generic/string-fza.h
new file mode 100644
index 0000000000..b5a5536f73
--- /dev/null
+++ b/sysdeps/generic/string-fza.h
@@ -0,0 +1,104 @@
+/* Basic zero byte detection.  Generic C 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/>.  */
+
+#ifndef _STRING_FZA_H
+#define _STRING_FZA_H 1
+
+#include <string-misc.h>
+#include <string-optype.h>
+
+/* The function return a byte mask.  */
+typedef op_t find_t;
+
+/* This function returns non-zero if any byte in X is zero.
+   More specifically, at least one bit set within the least significant
+   byte that was zero; other bytes within the word are indeterminate.  */
+static __always_inline find_t
+find_zero_low (op_t x)
+{
+  /* This expression comes from
+       https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
+     Subtracting 1 sets 0x80 in a byte that was 0; anding ~x clears
+     0x80 in a byte that was >= 128; anding 0x80 isolates that test bit.  */
+  op_t lsb = repeat_bytes (0x01);
+  op_t msb = repeat_bytes (0x80);
+  return (x - lsb) & ~x & msb;
+}
+
+/* This function returns at least one bit set within every byte of X that
+   is zero.  The result is exact in that, unlike find_zero_low, all bytes
+   are determinate.  This is usually used for finding the index of the
+   most significant byte that was zero.  */
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  /* For each byte, find not-zero by
+     (0) And 0x7f so that we cannot carry between bytes,
+     (1) Add 0x7f so that non-zero carries into 0x80,
+     (2) Or in the original byte (which might have had 0x80 set).
+     Then invert and mask such that 0x80 is set iff that byte was zero.  */
+  op_t m = repeat_bytes (0x7f);
+  return ~(((x & m) + m) | x | m);
+}
+
+/* With similar caveats, identify bytes that are equal between X1 and X2.  */
+static __always_inline find_t
+find_eq_low (op_t x1, op_t x2)
+{
+  return find_zero_low (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1 ^ x2);
+}
+
+/* With similar caveats, identify zero bytes in X1 and bytes that are
+   equal between in X1 and X2.  */
+static __always_inline find_t
+find_zero_eq_low (op_t x1, op_t x2)
+{
+  return find_zero_low (x1) | find_zero_low (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_zero_all (x1 ^ x2);
+}
+
+/* With similar caveats, identify zero bytes in X1 and bytes that are
+   not equal between in X1 and X2.  */
+static __always_inline find_t
+find_zero_ne_low (op_t x1, op_t x2)
+{
+  return (~find_zero_eq_low (x1, x2)) + 1;
+}
+
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  op_t m = repeat_bytes (0x7f);
+  op_t eq = x1 ^ x2;
+  op_t nz1 = ((x1 & m) + m) | x1;
+  op_t ne2 = ((eq & m) + m) | eq;
+  return (ne2 | ~nz1) & ~m;
+}
+
+#endif /* _STRING_FZA_H */
diff --git a/sysdeps/generic/string-fzb.h b/sysdeps/generic/string-fzb.h
new file mode 100644
index 0000000000..42de500d67
--- /dev/null
+++ b/sysdeps/generic/string-fzb.h
@@ -0,0 +1,49 @@
+/* Zero byte detection, boolean.  Generic C 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/>.  */
+
+#ifndef _STRING_FZB_H
+#define _STRING_FZB_H 1
+
+#include <endian.h>
+#include <string-fza.h>
+
+/* Determine if any byte within X is zero.  This is a pure boolean test.  */
+
+static __always_inline _Bool
+has_zero (op_t x)
+{
+  return find_zero_low (x) != 0;
+}
+
+/* Likewise, but for byte equality between X1 and X2.  */
+
+static __always_inline _Bool
+has_eq (op_t x1, op_t x2)
+{
+  return find_eq_low (x1, x2) != 0;
+}
+
+/* Likewise, but for zeros in X1 and equal bytes between X1 and X2.  */
+
+static __always_inline _Bool
+has_zero_eq (op_t x1, op_t x2)
+{
+  return find_zero_eq_low (x1, x2);
+}
+
+#endif /* _STRING_FZB_H */
diff --git a/sysdeps/generic/string-fzc.h b/sysdeps/generic/string-fzc.h
new file mode 100644
index 0000000000..75b2bf884e
--- /dev/null
+++ b/sysdeps/generic/string-fzc.h
@@ -0,0 +1,83 @@
+/* Zero byte detection; indexes.  Generic C 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/>.  */
+
+#ifndef _STRING_FZC_H
+#define _STRING_FZC_H 1
+
+#include <endian.h>
+#include <string-fza.h>
+#include <string-fzi.h>
+
+
+/* Given a word X that is known to contain a zero byte, return the index of
+   the first such within the word in memory order.  */
+static __always_inline unsigned int
+index_first_zero (op_t x)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x = find_zero_low (x);
+  else
+    x = find_zero_all (x);
+  return index_first (x);
+}
+
+/* Similarly, but perform the search for byte equality between X1 and X2.  */
+static __always_inline unsigned int
+index_first_eq (op_t x1, op_t x2)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x1 = find_eq_low (x1, x2);
+  else
+    x1 = find_eq_all (x1, x2);
+  return index_first (x1);
+}
+
+/* Similarly, but perform the search for zero within X1 or equality between
+   X1 and X2.  */
+static __always_inline unsigned int
+index_first_zero_eq (op_t x1, op_t x2)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x1 = find_zero_eq_low (x1, x2);
+  else
+    x1 = find_zero_eq_all (x1, x2);
+  return index_first (x1);
+}
+
+/* Similarly, but perform the search for zero within X1 or inequality between
+   X1 and X2.  */
+static __always_inline unsigned int
+index_first_zero_ne (op_t x1, op_t x2)
+{
+  return index_first (find_zero_ne_all (x1, x2));
+}
+
+/* Similarly, but search for the last zero within X.  */
+static __always_inline unsigned int
+index_last_zero (op_t x)
+{
+  return index_last (find_zero_all (x));
+}
+
+static __always_inline unsigned int
+index_last_eq (op_t x1, op_t x2)
+{
+  return index_last_zero (x1 ^ x2);
+}
+
+#endif /* STRING_FZC_H */
diff --git a/sysdeps/generic/string-fzi.h b/sysdeps/generic/string-fzi.h
new file mode 100644
index 0000000000..2deecefc23
--- /dev/null
+++ b/sysdeps/generic/string-fzi.h
@@ -0,0 +1,71 @@
+/* Zero byte detection; indexes.  Generic C 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/>.  */
+
+#ifndef _STRING_FZI_H
+#define _STRING_FZI_H 1
+
+#include <limits.h>
+#include <endian.h>
+#include <string-fza.h>
+
+static __always_inline int
+clz (find_t c)
+{
+  if (sizeof (find_t) == sizeof (unsigned long))
+    return __builtin_clzl (c);
+  else
+    return __builtin_clzll (c);
+}
+
+static __always_inline int
+ctz (find_t c)
+{
+  if (sizeof (find_t) == sizeof (unsigned long))
+    return __builtin_ctzl (c);
+  else
+    return __builtin_ctzll (c);
+}
+
+/* A subroutine for the index_zero functions.  Given a test word C, return
+   the (memory order) index of the first byte (in memory order) that is
+   non-zero.  */
+static __always_inline unsigned int
+index_first (find_t c)
+{
+  int r;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    r = ctz (c);
+  else
+    r = clz (c);
+  return r / CHAR_BIT;
+}
+
+/* Similarly, but return the (memory order) index of the last byte that is
+   non-zero.  */
+static __always_inline unsigned int
+index_last (find_t c)
+{
+  int r;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    r = clz (c);
+  else
+    r = ctz (c);
+  return sizeof (find_t) - 1 - (r / CHAR_BIT);
+}
+
+#endif /* STRING_FZI_H */
diff --git a/sysdeps/generic/string-misc.h b/sysdeps/generic/string-misc.h
new file mode 100644
index 0000000000..a119646957
--- /dev/null
+++ b/sysdeps/generic/string-misc.h
@@ -0,0 +1,45 @@
+/* Miscellaneous functions used in string implementations.  Generic C 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/>.  */
+
+#ifndef _STRING_MISC_H
+#define _STRING_MISC_H 1
+
+#include <limits.h>
+#include <endian.h>
+#include <string-optype.h>
+
+/* Extract the byte at index IDX from word X, with index 0 being the
+   least significant byte.  */
+static __always_inline unsigned char
+extractbyte (op_t x, unsigned int idx)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return x >> (idx * CHAR_BIT);
+  else
+    return x >> (sizeof (x) - 1 - idx) * CHAR_BIT;
+}
+
+/* Setup an word with each byte being c_in.  For instance, on a 64 bits
+   machine with input as 0xce the functions returns 0xcececececececece.  */
+static __always_inline op_t
+repeat_bytes (unsigned char c_in)
+{
+  return ((op_t)-1 / 0xff) * c_in;
+}
+
+#endif /* _STRING_MISC_H */
diff --git a/sysdeps/generic/string-shift.h b/sysdeps/generic/string-shift.h
new file mode 100644
index 0000000000..9491e8b580
--- /dev/null
+++ b/sysdeps/generic/string-shift.h
@@ -0,0 +1,52 @@
+/* Shift unaligned word read  Generic C 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/>.  */
+
+#ifndef _STRING_SHIFT_H
+#define _STRING_SHIFT_H 1
+
+#include <endian.h>
+#include <limits.h>
+#include <stdint.h>
+#include <string-fza.h>
+
+/* Return the mask WORD shifted based on S_INT address value, to ignore
+   values not presented in the aligned word read.  */
+static __always_inline find_t
+shift_find (find_t word, uintptr_t s)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return word >> (CHAR_BIT * (s % sizeof (op_t)));
+  else
+    return word << (CHAR_BIT * (s % sizeof (op_t)));
+}
+
+/* Mask off the bits defined for the S alignment value, or return WORD if
+   S is 0.  */
+static __always_inline find_t
+shift_find_last (find_t word, uintptr_t s)
+{
+  s = s % sizeof (op_t);
+  if (s == 0)
+    return word;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return word & ~(((op_t)-1) << (s * CHAR_BIT));
+  else
+    return word & ~(((op_t)-1) >> (s * CHAR_BIT));
+}
+
+#endif /* _STRING_SHIFT_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 04/31] string: Improve generic strlen
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (2 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 03/31] Add string vectorized find and detection functions Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:23   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 05/31] string: Improve generic strchrnul Adhemerval Zanella
                   ` (27 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

New algorithm read the first aligned address and mask off the
unwanted bytes (this strategy is similar to arch-specific
implementations used on powerpc, sparc, and sh).

The loop now read word-aligned address and check using the has_zero
macro.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powercp64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
---
 string/strlen.c         | 92 ++++++++++-------------------------------
 sysdeps/s390/strlen-c.c | 10 +++--
 2 files changed, 28 insertions(+), 74 deletions(-)

diff --git a/string/strlen.c b/string/strlen.c
index ee1aae0fff..5a4424f9a5 100644
--- a/string/strlen.c
+++ b/string/strlen.c
@@ -15,86 +15,38 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <libc-pointer-arith.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
+#include <string-shift.h>
 #include <string.h>
-#include <stdlib.h>
 
-#undef strlen
-
-#ifndef STRLEN
-# define STRLEN strlen
+#ifdef STRLEN
+# define __strlen STRLEN
 #endif
 
 /* Return the length of the null-terminated string STR.  Scan for
    the null terminator quickly by testing four bytes at a time.  */
 size_t
-STRLEN (const char *str)
+__strlen (const char *str)
 {
-  const char *char_ptr;
-  const unsigned long int *longword_ptr;
-  unsigned long int longword, himagic, lomagic;
-
-  /* Handle the first few characters by reading one character at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = str; ((unsigned long int) char_ptr
-			& (sizeof (longword) - 1)) != 0;
-       ++char_ptr)
-    if (*char_ptr == '\0')
-      return char_ptr - str;
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to 8-byte longwords.  */
-
-  longword_ptr = (unsigned long int *) char_ptr;
+  /* Align pointer to sizeof op_t.  */
+  const uintptr_t s_int = (uintptr_t) str;
+  const op_t *word_ptr = (const op_t*) PTR_ALIGN_DOWN (str, sizeof (op_t));
 
-  /* Computing (longword - lomagic) sets the high bit of any corresponding
-     byte that is either zero or greater than 0x80.  The latter case can be
-     filtered out by computing (~longword & himagic).  The final result
-     will always be non-zero if one of the bytes of longword is zero.  */
-  himagic = 0x80808080L;
-  lomagic = 0x01010101L;
-  if (sizeof (longword) > 4)
-    {
-      /* 64-bit version of the magic.  */
-      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
-      himagic = ((himagic << 16) << 16) | himagic;
-      lomagic = ((lomagic << 16) << 16) | lomagic;
-    }
-  if (sizeof (longword) > 8)
-    abort ();
+  op_t word = *word_ptr;
+  find_t mask = shift_find (find_zero_all (word), s_int);
+  if (mask != 0)
+    return index_first (mask);
 
-  /* Instead of the traditional loop which tests each character,
-     we will test a longword at a time.  The tricky part is testing
-     if *any of the four* bytes in the longword in question are zero.  */
-  for (;;)
-    {
-      longword = *longword_ptr++;
+  do
+    word = *++word_ptr;
+  while (! has_zero (word));
 
-      if (((longword - lomagic) & ~longword & himagic) != 0)
-	{
-	  /* Which of the bytes was the zero?  */
-
-	  const char *cp = (const char *) (longword_ptr - 1);
-
-	  if (cp[0] == 0)
-	    return cp - str;
-	  if (cp[1] == 0)
-	    return cp - str + 1;
-	  if (cp[2] == 0)
-	    return cp - str + 2;
-	  if (cp[3] == 0)
-	    return cp - str + 3;
-	  if (sizeof (longword) > 4)
-	    {
-	      if (cp[4] == 0)
-		return cp - str + 4;
-	      if (cp[5] == 0)
-		return cp - str + 5;
-	      if (cp[6] == 0)
-		return cp - str + 6;
-	      if (cp[7] == 0)
-		return cp - str + 7;
-	    }
-	}
-    }
+  return ((const char *) word_ptr) + index_first_zero (word) - str;
 }
+#ifndef STRLEN
+weak_alias (__strlen, strlen)
 libc_hidden_builtin_def (strlen)
+#endif
diff --git a/sysdeps/s390/strlen-c.c b/sysdeps/s390/strlen-c.c
index b829ef2452..0a33a6f8e5 100644
--- a/sysdeps/s390/strlen-c.c
+++ b/sysdeps/s390/strlen-c.c
@@ -21,12 +21,14 @@
 #if HAVE_STRLEN_C
 # if HAVE_STRLEN_IFUNC
 #  define STRLEN STRLEN_C
+# endif
+
+# include <string/strlen.c>
+
+# if HAVE_STRLEN_IFUNC
 #  if defined SHARED && IS_IN (libc)
-#   undef libc_hidden_builtin_def
-#   define libc_hidden_builtin_def(name)		\
-  __hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c);
+__hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c);
 #  endif
 # endif
 
-# include <string/strlen.c>
 #endif
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 05/31] string: Improve generic strchrnul
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (3 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 04/31] string: Improve generic strlen Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:23   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 06/31] string: Improve generic strchr Adhemerval Zanella
                   ` (26 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

New algorithm read the first aligned address and mask off the unwanted
bytes (this strategy is similar to arch-specific  implementations used
on powerpc, sparc, and sh).

The loop now read word-aligned address and check using the has_zero_eq
function.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
and powerpc-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
---
 string/strchrnul.c                            | 155 +++---------------
 .../power4/multiarch/strchrnul-ppc32.c        |   4 -
 sysdeps/s390/strchrnul-c.c                    |   2 -
 3 files changed, 22 insertions(+), 139 deletions(-)

diff --git a/string/strchrnul.c b/string/strchrnul.c
index fa2db4b417..e7887fa285 100644
--- a/string/strchrnul.c
+++ b/string/strchrnul.c
@@ -1,10 +1,5 @@
 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
-   with help from Dan Sahlin (dan@sics.se) and
-   bug fix and commentary by Jim Blandy (jimb@ai.mit.edu);
-   adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
-   and implemented by Roland McGrath (roland@ai.mit.edu).
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -20,147 +15,41 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <libc-pointer-arith.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
+#include <string-shift.h>
 #include <string.h>
-#include <memcopy.h>
-#include <stdlib.h>
 
 #undef __strchrnul
 #undef strchrnul
 
-#ifndef STRCHRNUL
-# define STRCHRNUL __strchrnul
+#ifdef STRCHRNUL
+# define __strchrnul STRCHRNUL
 #endif
 
 /* Find the first occurrence of C in S or the final NUL byte.  */
 char *
-STRCHRNUL (const char *s, int c_in)
+__strchrnul (const char *str, int c_in)
 {
-  const unsigned char *char_ptr;
-  const unsigned long int *longword_ptr;
-  unsigned long int longword, magic_bits, charmask;
-  unsigned char c;
-
-  c = (unsigned char) c_in;
-
-  /* Handle the first few characters by reading one character at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = (const unsigned char *) s;
-       ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0;
-       ++char_ptr)
-    if (*char_ptr == c || *char_ptr == '\0')
-      return (void *) char_ptr;
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to 8-byte longwords.  */
-
-  longword_ptr = (unsigned long int *) char_ptr;
-
-  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
-     the "holes."  Note that there is a hole just to the left of
-     each byte, with an extra at the end:
-
-     bits:  01111110 11111110 11111110 11111111
-     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
-
-     The 1-bits make sure that carries propagate to the next 0-bit.
-     The 0-bits provide holes for carries to fall into.  */
-  magic_bits = -1;
-  magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
-
-  /* Set up a longword, each of whose bytes is C.  */
-  charmask = c | (c << 8);
-  charmask |= charmask << 16;
-  if (sizeof (longword) > 4)
-    /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
-    charmask |= (charmask << 16) << 16;
-  if (sizeof (longword) > 8)
-    abort ();
-
-  /* Instead of the traditional loop which tests each character,
-     we will test a longword at a time.  The tricky part is testing
-     if *any of the four* bytes in the longword in question are zero.  */
-  for (;;)
-    {
-      /* We tentatively exit the loop if adding MAGIC_BITS to
-	 LONGWORD fails to change any of the hole bits of LONGWORD.
-
-	 1) Is this safe?  Will it catch all the zero bytes?
-	 Suppose there is a byte with all zeros.  Any carry bits
-	 propagating from its left will fall into the hole at its
-	 least significant bit and stop.  Since there will be no
-	 carry from its most significant bit, the LSB of the
-	 byte to the left will be unchanged, and the zero will be
-	 detected.
+  /* Align pointer to sizeof op_t.  */
+  uintptr_t s_int = (uintptr_t) str;
+  const op_t *word_ptr = (const op_t *) PTR_ALIGN_DOWN (str, sizeof (op_t));
 
-	 2) Is this worthwhile?  Will it ignore everything except
-	 zero bytes?  Suppose every byte of LONGWORD has a bit set
-	 somewhere.  There will be a carry into bit 8.  If bit 8
-	 is set, this will carry into bit 16.  If bit 8 is clear,
-	 one of bits 9-15 must be set, so there will be a carry
-	 into bit 16.  Similarly, there will be a carry into bit
-	 24.  If one of bits 24-30 is set, there will be a carry
-	 into bit 31, so all of the hole bits will be changed.
+  op_t repeated_c = repeat_bytes (c_in);
 
-	 The one misfire occurs when bits 24-30 are clear and bit
-	 31 is set; in this case, the hole at bit 31 is not
-	 changed.  If we had access to the processor carry flag,
-	 we could close this loophole by putting the fourth hole
-	 at bit 32!
+  op_t word = *word_ptr;
+  find_t mask = shift_find (find_zero_eq_all (word, repeated_c), s_int);
+  if (mask != 0)
+    return (char *) str + index_first (mask);
 
-	 So it ignores everything except 128's, when they're aligned
-	 properly.
+  do
+    word = *++word_ptr;
+  while (! has_zero_eq (word, repeated_c));
 
-	 3) But wait!  Aren't we looking for C as well as zero?
-	 Good point.  So what we do is XOR LONGWORD with a longword,
-	 each of whose bytes is C.  This turns each byte that is C
-	 into a zero.  */
-
-      longword = *longword_ptr++;
-
-      /* Add MAGIC_BITS to LONGWORD.  */
-      if ((((longword + magic_bits)
-
-	    /* Set those bits that were unchanged by the addition.  */
-	    ^ ~longword)
-
-	   /* Look at only the hole bits.  If any of the hole bits
-	      are unchanged, most likely one of the bytes was a
-	      zero.  */
-	   & ~magic_bits) != 0
-
-	  /* That caught zeroes.  Now test for C.  */
-	  || ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
-	      & ~magic_bits) != 0)
-	{
-	  /* Which of the bytes was C or zero?
-	     If none of them were, it was a misfire; continue the search.  */
-
-	  const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
-
-	  if (*cp == c || *cp == '\0')
-	    return (char *) cp;
-	  if (*++cp == c || *cp == '\0')
-	    return (char *) cp;
-	  if (*++cp == c || *cp == '\0')
-	    return (char *) cp;
-	  if (*++cp == c || *cp == '\0')
-	    return (char *) cp;
-	  if (sizeof (longword) > 4)
-	    {
-	      if (*++cp == c || *cp == '\0')
-		return (char *) cp;
-	      if (*++cp == c || *cp == '\0')
-		return (char *) cp;
-	      if (*++cp == c || *cp == '\0')
-		return (char *) cp;
-	      if (*++cp == c || *cp == '\0')
-		return (char *) cp;
-	    }
-	}
-    }
-
-  /* This should never happen.  */
-  return NULL;
+  return (char *) word_ptr + index_first_zero_eq (word, repeated_c);
 }
-
+#ifndef STRCHRNUL
 weak_alias (__strchrnul, strchrnul)
+#endif
diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
index 88ce5dfffa..da03ac7c04 100644
--- a/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
+++ b/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
@@ -19,10 +19,6 @@
 #include <string.h>
 
 #define STRCHRNUL  __strchrnul_ppc
-
-#undef weak_alias
-#define weak_alias(a,b )
-
 extern __typeof (strchrnul) __strchrnul_ppc attribute_hidden;
 
 #include <string/strchrnul.c>
diff --git a/sysdeps/s390/strchrnul-c.c b/sysdeps/s390/strchrnul-c.c
index e1248d1dbf..ff6aa38d4f 100644
--- a/sysdeps/s390/strchrnul-c.c
+++ b/sysdeps/s390/strchrnul-c.c
@@ -22,8 +22,6 @@
 # if HAVE_STRCHRNUL_IFUNC
 #  define STRCHRNUL STRCHRNUL_C
 #  define __strchrnul STRCHRNUL
-#  undef weak_alias
-#  define weak_alias(name, alias)
 # endif
 
 # include <string/strchrnul.c>
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 06/31] string: Improve generic strchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (4 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 05/31] string: Improve generic strchrnul Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:24   ` Noah Goldstein
  2023-02-04  2:58   ` Richard Henderson
  2023-02-02 18:11 ` [PATCH v12 07/31] string: Improve generic strcmp Adhemerval Zanella
                   ` (25 subsequent siblings)
  31 siblings, 2 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

New algorithm now calls strchrnul.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/strchr.c         | 164 ++--------------------------------------
 sysdeps/s390/strchr-c.c |  11 +--
 2 files changed, 14 insertions(+), 161 deletions(-)

diff --git a/string/strchr.c b/string/strchr.c
index 1572b8b42e..90ae0b69fc 100644
--- a/string/strchr.c
+++ b/string/strchr.c
@@ -1,10 +1,5 @@
 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
-   with help from Dan Sahlin (dan@sics.se) and
-   bug fix and commentary by Jim Blandy (jimb@ai.mit.edu);
-   adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
-   and implemented by Roland McGrath (roland@ai.mit.edu).
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -21,165 +16,22 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <string.h>
-#include <stdlib.h>
 
 #undef strchr
+#undef index
 
-#ifndef STRCHR
-# define STRCHR strchr
+#ifdef STRCHR
+# define strchr STRCHR
 #endif
 
 /* Find the first occurrence of C in S.  */
 char *
-STRCHR (const char *s, int c_in)
+strchr (const char *s, int c_in)
 {
-  const unsigned char *char_ptr;
-  const unsigned long int *longword_ptr;
-  unsigned long int longword, magic_bits, charmask;
-  unsigned char c;
-
-  c = (unsigned char) c_in;
-
-  /* Handle the first few characters by reading one character at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = (const unsigned char *) s;
-       ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0;
-       ++char_ptr)
-    if (*char_ptr == c)
-      return (void *) char_ptr;
-    else if (*char_ptr == '\0')
-      return NULL;
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to 8-byte longwords.  */
-
-  longword_ptr = (unsigned long int *) char_ptr;
-
-  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
-     the "holes."  Note that there is a hole just to the left of
-     each byte, with an extra at the end:
-
-     bits:  01111110 11111110 11111110 11111111
-     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
-
-     The 1-bits make sure that carries propagate to the next 0-bit.
-     The 0-bits provide holes for carries to fall into.  */
-  magic_bits = -1;
-  magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
-
-  /* Set up a longword, each of whose bytes is C.  */
-  charmask = c | (c << 8);
-  charmask |= charmask << 16;
-  if (sizeof (longword) > 4)
-    /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
-    charmask |= (charmask << 16) << 16;
-  if (sizeof (longword) > 8)
-    abort ();
-
-  /* Instead of the traditional loop which tests each character,
-     we will test a longword at a time.  The tricky part is testing
-     if *any of the four* bytes in the longword in question are zero.  */
-  for (;;)
-    {
-      /* We tentatively exit the loop if adding MAGIC_BITS to
-	 LONGWORD fails to change any of the hole bits of LONGWORD.
-
-	 1) Is this safe?  Will it catch all the zero bytes?
-	 Suppose there is a byte with all zeros.  Any carry bits
-	 propagating from its left will fall into the hole at its
-	 least significant bit and stop.  Since there will be no
-	 carry from its most significant bit, the LSB of the
-	 byte to the left will be unchanged, and the zero will be
-	 detected.
-
-	 2) Is this worthwhile?  Will it ignore everything except
-	 zero bytes?  Suppose every byte of LONGWORD has a bit set
-	 somewhere.  There will be a carry into bit 8.  If bit 8
-	 is set, this will carry into bit 16.  If bit 8 is clear,
-	 one of bits 9-15 must be set, so there will be a carry
-	 into bit 16.  Similarly, there will be a carry into bit
-	 24.  If one of bits 24-30 is set, there will be a carry
-	 into bit 31, so all of the hole bits will be changed.
-
-	 The one misfire occurs when bits 24-30 are clear and bit
-	 31 is set; in this case, the hole at bit 31 is not
-	 changed.  If we had access to the processor carry flag,
-	 we could close this loophole by putting the fourth hole
-	 at bit 32!
-
-	 So it ignores everything except 128's, when they're aligned
-	 properly.
-
-	 3) But wait!  Aren't we looking for C as well as zero?
-	 Good point.  So what we do is XOR LONGWORD with a longword,
-	 each of whose bytes is C.  This turns each byte that is C
-	 into a zero.  */
-
-      longword = *longword_ptr++;
-
-      /* Add MAGIC_BITS to LONGWORD.  */
-      if ((((longword + magic_bits)
-
-	    /* Set those bits that were unchanged by the addition.  */
-	    ^ ~longword)
-
-	   /* Look at only the hole bits.  If any of the hole bits
-	      are unchanged, most likely one of the bytes was a
-	      zero.  */
-	   & ~magic_bits) != 0
-
-	  /* That caught zeroes.  Now test for C.  */
-	  || ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
-	      & ~magic_bits) != 0)
-	{
-	  /* Which of the bytes was C or zero?
-	     If none of them were, it was a misfire; continue the search.  */
-
-	  const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
-
-	  if (*cp == c)
-	    return (char *) cp;
-	  else if (*cp == '\0')
-	    return NULL;
-	  if (*++cp == c)
-	    return (char *) cp;
-	  else if (*cp == '\0')
-	    return NULL;
-	  if (*++cp == c)
-	    return (char *) cp;
-	  else if (*cp == '\0')
-	    return NULL;
-	  if (*++cp == c)
-	    return (char *) cp;
-	  else if (*cp == '\0')
-	    return NULL;
-	  if (sizeof (longword) > 4)
-	    {
-	      if (*++cp == c)
-		return (char *) cp;
-	      else if (*cp == '\0')
-		return NULL;
-	      if (*++cp == c)
-		return (char *) cp;
-	      else if (*cp == '\0')
-		return NULL;
-	      if (*++cp == c)
-		return (char *) cp;
-	      else if (*cp == '\0')
-		return NULL;
-	      if (*++cp == c)
-		return (char *) cp;
-	      else if (*cp == '\0')
-		return NULL;
-	    }
-	}
-    }
-
-  return NULL;
+  char *r = __strchrnul (s, c_in);
+  return (*(unsigned char *)r == (unsigned char)c_in) ? r : NULL;
 }
-
-#ifdef weak_alias
-# undef index
+#ifndef STRCHR
 weak_alias (strchr, index)
-#endif
 libc_hidden_builtin_def (strchr)
+#endif
diff --git a/sysdeps/s390/strchr-c.c b/sysdeps/s390/strchr-c.c
index c00f2cceea..90822ae0f4 100644
--- a/sysdeps/s390/strchr-c.c
+++ b/sysdeps/s390/strchr-c.c
@@ -21,13 +21,14 @@
 #if HAVE_STRCHR_C
 # if HAVE_STRCHR_IFUNC
 #  define STRCHR STRCHR_C
-#  undef weak_alias
+# endif
+
+# include <string/strchr.c>
+
+# if HAVE_STRCHR_IFUNC
 #  if defined SHARED && IS_IN (libc)
-#   undef libc_hidden_builtin_def
-#   define libc_hidden_builtin_def(name)			\
-     __hidden_ver1 (__strchr_c, __GI_strchr, __strchr_c);
+__hidden_ver1 (__strchr_c, __GI_strchr, __strchr_c);
 #  endif
 # endif
 
-# include <string/strchr.c>
 #endif
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 07/31] string: Improve generic strcmp
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (5 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 06/31] string: Improve generic strchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:25   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 08/31] string: Improve generic strncmp Adhemerval Zanella
                   ` (24 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It follows the strategy:

  - Align the first input to word boundary using byte operations.

  - If second input is also word aligned, read a word per time, check for
    null (using has_zero), and check final words using byte operation.

  - If second input is not word aligned, loop by aligning the source, and
    merging the result of two reads.  Similar to aligned case, check for
    null with has_zero, and check final words using byte operation.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
and powerpc-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/strcmp.c | 110 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 94 insertions(+), 16 deletions(-)

diff --git a/string/strcmp.c b/string/strcmp.c
index 053f5a8d2b..11ec8bac81 100644
--- a/string/strcmp.c
+++ b/string/strcmp.c
@@ -15,33 +15,111 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <stdint.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
 #include <string.h>
+#include <memcopy.h>
 
-#undef strcmp
-
-#ifndef STRCMP
-# define STRCMP strcmp
+#ifdef STRCMP
+# define strcmp STRCMP
 #endif
 
+static inline int
+final_cmp (const op_t w1, const op_t w2)
+{
+  unsigned int idx = index_first_zero_ne (w1, w2);
+  return extractbyte (w1, idx) - extractbyte (w2, idx);
+}
+
+/* Aligned loop: if a difference is found, exit to compare the bytes.  Else
+   if a zero is found we have equal strings.  */
+static inline int
+strcmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1)
+{
+  op_t w2 = *x2++;
+
+  while (w1 == w2)
+    {
+      if (has_zero (w1))
+	return 0;
+      w1 = *x1++;
+      w2 = *x2++;
+    }
+
+  return final_cmp (w1, w2);
+}
+
+/* Unaligned loop: align the first partial of P2, with 0xff for the rest of
+   the bytes so that we can also apply the has_zero test to see if we have
+   already reached EOS.  If we have, then we can simply fall through to the
+   final comparison.  */
+static inline int
+strcmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs)
+{
+  op_t w2a = *x2++;
+  uintptr_t sh_1 = ofs * CHAR_BIT;
+  uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
+
+  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
+  if (!has_zero (w2))
+    {
+      op_t w2b;
+
+      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
+	 does not contain end-of-string.  Therefore it is safe (and necessary)
+	 to read another word from each while we do not have a difference.  */
+      while (1)
+	{
+	  w2b = *x2++;
+	  w2 = MERGE (w2a, sh_1, w2b, sh_2);
+	  if (w1 != w2)
+	    return final_cmp (w1, w2);
+	  if (has_zero (w2b))
+	    break;
+	  w1 = *x1++;
+	  w2a = w2b;
+	}
+
+      /* Zero found in the second partial of P2.  If we had EOS in the aligned
+	 word, we have equality.  */
+      if (has_zero (w1))
+	return 0;
+
+      /* Load the final word of P1 and align the final partial of P2.  */
+      w1 = *x1++;
+      w2 = MERGE (w2b, sh_1, 0, sh_2);
+    }
+
+  return final_cmp (w1, w2);
+}
+
 /* Compare S1 and S2, returning less than, equal to or
    greater than zero if S1 is lexicographically less than,
    equal to or greater than S2.  */
 int
-STRCMP (const char *p1, const char *p2)
+strcmp (const char *p1, const char *p2)
 {
-  const unsigned char *s1 = (const unsigned char *) p1;
-  const unsigned char *s2 = (const unsigned char *) p2;
-  unsigned char c1, c2;
-
-  do
+  /* Handle the unaligned bytes of p1 first.  */
+  uintptr_t n = -(uintptr_t)p1 % sizeof(op_t);
+  for (int i = 0; i < n; ++i)
     {
-      c1 = (unsigned char) *s1++;
-      c2 = (unsigned char) *s2++;
-      if (c1 == '\0')
-	return c1 - c2;
+      unsigned char c1 = *p1++;
+      unsigned char c2 = *p2++;
+      int diff = c1 - c2;
+      if (c1 == '\0' || diff != 0)
+	return diff;
     }
-  while (c1 == c2);
 
-  return c1 - c2;
+  /* P1 is now aligned to op_t.  P2 may or may not be.  */
+  const op_t *x1 = (const op_t *) p1;
+  op_t w1 = *x1++;
+  uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
+  return ofs == 0
+    ? strcmp_aligned_loop (x1, (const op_t *)p2, w1)
+    : strcmp_unaligned_loop (x1, (const op_t *)(p2 - ofs), w1, ofs);
 }
+#ifndef STRCMP
 libc_hidden_builtin_def (strcmp)
+#endif
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 08/31] string: Improve generic strncmp
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (6 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 07/31] string: Improve generic strcmp Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03  0:34   ` Richard Henderson
  2023-02-21  9:28   ` Szabolcs Nagy
  2023-02-02 18:11 ` [PATCH v12 09/31] string: Improve generic stpcpy Adhemerval Zanella
                   ` (23 subsequent siblings)
  31 siblings, 2 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It follows the strategy:

   - Align the first input to word boundary using byte operations.

   - If second input is also word aligned, read a word per time, check
     for  null (using has_zero), and check final words using byte
     operation.

   - If second input is not word aligned, loop by aligning the source,
     and merge the result of two reads.  Similar to aligned case, check
     for null with has_zero, and check final words using byte operation.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
and powerpc-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).
---
 string/strncmp.c | 138 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 101 insertions(+), 37 deletions(-)

diff --git a/string/strncmp.c b/string/strncmp.c
index fd7cee09b6..4c8bf36bb9 100644
--- a/string/strncmp.c
+++ b/string/strncmp.c
@@ -15,7 +15,12 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <stdint.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
 #include <string.h>
+#include <sys/param.h>
 #include <memcopy.h>
 
 #undef strncmp
@@ -24,51 +29,110 @@
 #define STRNCMP strncmp
 #endif
 
-/* Compare no more than N characters of S1 and S2,
-   returning less than, equal to or greater than zero
-   if S1 is lexicographically less than, equal to or
-   greater than S2.  */
-int
-STRNCMP (const char *s1, const char *s2, size_t n)
+static inline int
+final_cmp (const op_t w1, const op_t w2, size_t n)
+{
+  unsigned int idx = index_first_zero_ne (w1, w2);
+  if (n <= idx)
+    return 0;
+  return extractbyte (w1, idx) - extractbyte (w2, idx);
+}
+
+/* Aligned loop: if a difference is found, exit to compare the bytes.  Else
+   if a zero is found we have equal strings.  */
+static inline int
+strncmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1, size_t n)
 {
-  unsigned char c1 = '\0';
-  unsigned char c2 = '\0';
+  op_t w2 = *x2++;
 
-  if (n >= 4)
+  while (w1 == w2)
     {
-      size_t n4 = n >> 2;
-      do
-	{
-	  c1 = (unsigned char) *s1++;
-	  c2 = (unsigned char) *s2++;
-	  if (c1 == '\0' || c1 != c2)
-	    return c1 - c2;
-	  c1 = (unsigned char) *s1++;
-	  c2 = (unsigned char) *s2++;
-	  if (c1 == '\0' || c1 != c2)
-	    return c1 - c2;
-	  c1 = (unsigned char) *s1++;
-	  c2 = (unsigned char) *s2++;
-	  if (c1 == '\0' || c1 != c2)
-	    return c1 - c2;
-	  c1 = (unsigned char) *s1++;
-	  c2 = (unsigned char) *s2++;
-	  if (c1 == '\0' || c1 != c2)
-	    return c1 - c2;
-	} while (--n4 > 0);
-      n &= 3;
+      if (n <= sizeof (op_t))
+	break;
+      n -= sizeof (op_t);
+
+      if (has_zero (w1))
+	return 0;
+      w1 = *x1++;
+      w2 = *x2++;
     }
 
-  while (n > 0)
+  return final_cmp (w1, w2, n);
+}
+
+/* Unaligned loop: align the first partial of P2, with 0xff for the rest of
+   the bytes so that we can also apply the has_zero test to see if we have
+   already reached EOS.  If we have, then we can simply fall through to the
+   final comparison.  */
+static inline int
+strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
+			size_t n)
+{
+  op_t w2a = *x2++;
+  uintptr_t sh_1 = ofs * CHAR_BIT;
+  uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
+
+  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
+  if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
     {
-      c1 = (unsigned char) *s1++;
-      c2 = (unsigned char) *s2++;
-      if (c1 == '\0' || c1 != c2)
-	return c1 - c2;
-      n--;
+      op_t w2b;
+
+      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
+	 does not contain end-of-string.  Therefore it is safe (and necessary)
+	 to read another word from each while we do not have a difference.  */
+      while (1)
+	{
+	  w2b = *x2++;
+	  w2 = MERGE (w2a, sh_1, w2b, sh_2);
+	  if (n <= sizeof (op_t) || w1 != w2)
+	    return final_cmp (w1, w2, n);
+	  n -= sizeof(op_t);
+	  if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
+	    break;
+	  w1 = *x1++;
+	  w2a = w2b;
+	}
+
+      /* Zero found in the second partial of P2.  If we had EOS in the aligned
+	 word, we have equality.  */
+      if (has_zero (w1))
+	return 0;
+
+      /* Load the final word of P1 and align the final partial of P2.  */
+      w1 = *x1++;
+      w2 = MERGE (w2b, sh_1, 0, sh_2);
     }
 
-  return c1 - c2;
+  return final_cmp (w1, w2, n);
 }
 
+/* Compare no more than N characters of S1 and S2,
+   returning less than, equal to or greater than zero
+   if S1 is lexicographically less than, equal to or
+   greater than S2.  */
+int
+STRNCMP (const char *p1, const char *p2, size_t n)
+{
+  /* Handle the unaligned bytes of p1 first.  */
+  uintptr_t a = MIN (-(uintptr_t)p1 % sizeof(op_t), n);
+  int diff = 0;
+  for (int i = 0; i < a; ++i)
+    {
+      unsigned char c1 = *p1++;
+      unsigned char c2 = *p2++;
+      diff = c1 - c2;
+      if (c1 == '\0' || diff != 0)
+	return diff;
+    }
+  if (a == n)
+    return 0;
+
+  /* P1 is now aligned to op_t.  P2 may or may not be.  */
+  const op_t *x1 = (const op_t *) p1;
+  op_t w1 = *x1++;
+  uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
+  return ofs == 0
+    ? strncmp_aligned_loop (x1, (const op_t *) p2, w1, n - a)
+    : strncmp_unaligned_loop (x1, (const op_t *) (p2 - ofs), w1, ofs, n - a);
+}
 libc_hidden_builtin_def (STRNCMP)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 09/31] string: Improve generic stpcpy
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (7 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 08/31] string: Improve generic strncmp Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:26   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 10/31] string: Improve generic strcpy Adhemerval Zanella
                   ` (22 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It follows the strategy:

  - Align the destination on word boundary using byte operations.

  - If source is also word aligned, read a word per time, check for
    null (using has_zero from string-fzb.h), and write the remaining
    bytes.

  - If source is not word aligned, loop by aligning the source, and
    merging the result of two reads.  Similar to aligned case,
    check for null with has_zero, and write the remaining bytes if
    null is found.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
and powerpc-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/stpcpy.c | 92 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 86 insertions(+), 6 deletions(-)

diff --git a/string/stpcpy.c b/string/stpcpy.c
index 8df5065cfe..dd0fef12ef 100644
--- a/string/stpcpy.c
+++ b/string/stpcpy.c
@@ -15,12 +15,12 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
 #define NO_MEMPCPY_STPCPY_REDIRECT
 #include <string.h>
+#include <stdint.h>
+#include <memcopy.h>
+#include <string-fzb.h>
+#include <string-misc.h>
 
 #undef __stpcpy
 #undef stpcpy
@@ -29,12 +29,92 @@
 # define STPCPY __stpcpy
 #endif
 
+static __always_inline char *
+write_byte_from_word (op_t *dest, op_t word)
+{
+  char *d = (char *) dest;
+  for (size_t i = 0; i < OPSIZ; i++, ++d)
+    {
+      char c = extractbyte (word, i);
+      *d = c;
+      if (c == '\0')
+	break;
+    }
+  return d;
+}
+
+static __always_inline char *
+stpcpy_aligned_loop (op_t *restrict dst, const op_t *restrict src)
+{
+  op_t word;
+  while (1)
+    {
+      word = *src++;
+      if (has_zero (word))
+	break;
+      *dst++ = word;
+    }
+
+  return write_byte_from_word (dst, word);
+}
+
+static __always_inline char *
+stpcpy_unaligned_loop (op_t *restrict dst, const op_t *restrict src,
+		       uintptr_t ofs)
+{
+  op_t w2a = *src++;
+  uintptr_t sh_1 = ofs * CHAR_BIT;
+  uintptr_t sh_2 = OPSIZ * CHAR_BIT - sh_1;
+
+  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
+  if (!has_zero (w2))
+    {
+      op_t w2b;
+
+      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
+	 does not contain end-of-string.  Therefore it is safe (and necessary)
+	 to read another word from each while we do not have a difference.  */
+      while (1)
+	{
+	  w2b = *src++;
+	  w2 = MERGE (w2a, sh_1, w2b, sh_2);
+	  /* Check if there is zero on w2a.  */
+	  if (has_zero (w2))
+	    goto out;
+	  *dst++ = w2;
+	  if (has_zero (w2b))
+	    break;
+	  w2a = w2b;
+	}
+
+      /* Align the final partial of P2.  */
+      w2 = MERGE (w2b, sh_1, 0, sh_2);
+    }
+
+out:
+  return write_byte_from_word (dst, w2);
+}
+
+
 /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST.  */
 char *
 STPCPY (char *dest, const char *src)
 {
-  size_t len = strlen (src);
-  return memcpy (dest, src, len + 1) + len;
+  /* Copy just a few bytes to make DEST aligned.  */
+  size_t len = (-(uintptr_t) dest) % OPSIZ;
+  for (; len != 0; len--, ++dest)
+    {
+      char c = *src++;
+      *dest = c;
+      if (c == '\0')
+	return dest;
+    }
+
+  /* DEST is now aligned to op_t, SRC may or may not be.  */
+  uintptr_t ofs = (uintptr_t) src % OPSIZ;
+  return ofs == 0 ? stpcpy_aligned_loop ((op_t*) dest, (const op_t *) src)
+		  : stpcpy_unaligned_loop ((op_t*) dest,
+					   (const op_t *) (src - ofs) , ofs);
 }
 weak_alias (__stpcpy, stpcpy)
 libc_hidden_def (__stpcpy)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 10/31] string: Improve generic strcpy
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (8 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 09/31] string: Improve generic stpcpy Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:26   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 11/31] string: Improve generic memchr Adhemerval Zanella
                   ` (21 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Now that stpcpy is vectorized based on op_t, it should be better to
call it instead of strlen plus memcpy.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
and powerpc-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/strcpy.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/string/strcpy.c b/string/strcpy.c
index 0345c71d15..d945d8fdf0 100644
--- a/string/strcpy.c
+++ b/string/strcpy.c
@@ -19,6 +19,9 @@
 #include <string.h>
 
 #undef strcpy
+/* Disable internal stpcpy optimization, otherwise the __stpcpy might it
+   generate a strcpy call.  */
+#undef __stpcpy
 
 #ifndef STRCPY
 # define STRCPY strcpy
@@ -28,6 +31,7 @@
 char *
 STRCPY (char *dest, const char *src)
 {
-  return memcpy (dest, src, strlen (src) + 1);
+  __stpcpy (dest, src);
+  return dest;
 }
 libc_hidden_builtin_def (strcpy)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 11/31] string: Improve generic memchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (9 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 10/31] string: Improve generic strcpy Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:26   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 12/31] string: Improve generic strnlen with memchr Adhemerval Zanella
                   ` (20 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

New algorithm read the first aligned address and mask off the
unwanted bytes (this strategy is similar to arch-specific
implementations used on powerpc, sparc, and sh).

The loop now read word-aligned address and check using the has_eq
macro.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
---
 string/memchr.c                               | 176 +++++-------------
 .../powerpc32/power4/multiarch/memchr-ppc32.c |  14 +-
 .../powerpc64/multiarch/memchr-ppc64.c        |   9 +-
 3 files changed, 56 insertions(+), 143 deletions(-)

diff --git a/string/memchr.c b/string/memchr.c
index f800d47dce..4f497d6166 100644
--- a/string/memchr.c
+++ b/string/memchr.c
@@ -1,10 +1,6 @@
-/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
+/* Scan memory for a character.  Generic version
+   Copyright (C) 1991-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
-   with help from Dan Sahlin (dan@sics.se) and
-   commentary by Jim Blandy (jimb@ai.mit.edu);
-   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
-   and implemented by Roland McGrath (roland@ai.mit.edu).
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -20,143 +16,73 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#ifndef _LIBC
-# include <config.h>
-#endif
-
+#include <libc-pointer-arith.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
+#include <string-shift.h>
 #include <string.h>
 
-#include <stddef.h>
+#undef memchr
 
-#include <limits.h>
-
-#undef __memchr
-#ifdef _LIBC
-# undef memchr
+#ifdef MEMCHR
+# define __memchr MEMCHR
 #endif
 
-#ifndef weak_alias
-# define __memchr memchr
-#endif
-
-#ifndef MEMCHR
-# define MEMCHR __memchr
-#endif
+static __always_inline const char *
+sadd (uintptr_t x, uintptr_t y)
+{
+  return (const char *)(y > UINTPTR_MAX - x ? UINTPTR_MAX : x + y);
+}
 
 /* Search no more than N bytes of S for C.  */
 void *
-MEMCHR (void const *s, int c_in, size_t n)
+__memchr (void const *s, int c_in, size_t n)
 {
-  /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
-     long instead of a 64-bit uintmax_t tends to give better
-     performance.  On 64-bit hardware, unsigned long is generally 64
-     bits already.  Change this typedef to experiment with
-     performance.  */
-  typedef unsigned long int longword;
-
-  const unsigned char *char_ptr;
-  const longword *longword_ptr;
-  longword repeated_one;
-  longword repeated_c;
-  unsigned char c;
-
-  c = (unsigned char) c_in;
-
-  /* Handle the first few bytes by reading one byte at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = (const unsigned char *) s;
-       n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
-       --n, ++char_ptr)
-    if (*char_ptr == c)
-      return (void *) char_ptr;
-
-  longword_ptr = (const longword *) char_ptr;
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to any size longwords.  */
-
-  /* Compute auxiliary longword values:
-     repeated_one is a value which has a 1 in every byte.
-     repeated_c has c in every byte.  */
-  repeated_one = 0x01010101;
-  repeated_c = c | (c << 8);
-  repeated_c |= repeated_c << 16;
-  if (0xffffffffU < (longword) -1)
+  if (__glibc_unlikely (n == 0))
+    return NULL;
+
+  /* Read the first word, but munge it so that bytes before the array
+     will not match goal.  */
+  const op_t *word_ptr = PTR_ALIGN_DOWN (s, sizeof (op_t));
+  uintptr_t s_int = (uintptr_t) s;
+
+  op_t word = *word_ptr;
+  op_t repeated_c = repeat_bytes (c_in);
+  /* Compute the address of the last byte taking in consideration possible
+     overflow.  */
+  const char *lbyte = sadd (s_int, n - 1);
+  /* And also the address of the word containing the last byte. */
+  const op_t *lword = (const op_t *) PTR_ALIGN_DOWN (lbyte, sizeof (op_t));
+
+  find_t mask = shift_find (find_eq_all (word, repeated_c), s_int);
+  if (mask != 0)
     {
-      repeated_one |= repeated_one << 31 << 1;
-      repeated_c |= repeated_c << 31 << 1;
-      if (8 < sizeof (longword))
-	{
-	  size_t i;
-
-	  for (i = 64; i < sizeof (longword) * 8; i *= 2)
-	    {
-	      repeated_one |= repeated_one << i;
-	      repeated_c |= repeated_c << i;
-	    }
-	}
+      char *ret = (char *) s + index_first (mask);
+      return (ret <= lbyte) ? ret : NULL;
     }
+  if (word_ptr == lword)
+    return NULL;
 
-  /* Instead of the traditional loop which tests each byte, we will test a
-     longword at a time.  The tricky part is testing if *any of the four*
-     bytes in the longword in question are equal to c.  We first use an xor
-     with repeated_c.  This reduces the task to testing whether *any of the
-     four* bytes in longword1 is zero.
-
-     We compute tmp =
-       ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
-     That is, we perform the following operations:
-       1. Subtract repeated_one.
-       2. & ~longword1.
-       3. & a mask consisting of 0x80 in every byte.
-     Consider what happens in each byte:
-       - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
-	 and step 3 transforms it into 0x80.  A carry can also be propagated
-	 to more significant bytes.
-       - If a byte of longword1 is nonzero, let its lowest 1 bit be at
-	 position k (0 <= k <= 7); so the lowest k bits are 0.  After step 1,
-	 the byte ends in a single bit of value 0 and k bits of value 1.
-	 After step 2, the result is just k bits of value 1: 2^k - 1.  After
-	 step 3, the result is 0.  And no carry is produced.
-     So, if longword1 has only non-zero bytes, tmp is zero.
-     Whereas if longword1 has a zero byte, call j the position of the least
-     significant zero byte.  Then the result has a zero at positions 0, ...,
-     j-1 and a 0x80 at position j.  We cannot predict the result at the more
-     significant bytes (positions j+1..3), but it does not matter since we
-     already have a non-zero bit at position 8*j+7.
-
-     So, the test whether any byte in longword1 is zero is equivalent to
-     testing whether tmp is nonzero.  */
-
-  while (n >= sizeof (longword))
+  word = *++word_ptr;
+  while (word_ptr != lword)
     {
-      longword longword1 = *longword_ptr ^ repeated_c;
-
-      if ((((longword1 - repeated_one) & ~longword1)
-	   & (repeated_one << 7)) != 0)
-	break;
-      longword_ptr++;
-      n -= sizeof (longword);
+      if (has_eq (word, repeated_c))
+	return (char *) word_ptr + index_first_eq (word, repeated_c);
+      word = *++word_ptr;
     }
 
-  char_ptr = (const unsigned char *) longword_ptr;
-
-  /* At this point, we know that either n < sizeof (longword), or one of the
-     sizeof (longword) bytes starting at char_ptr is == c.  On little-endian
-     machines, we could determine the first such byte without any further
-     memory accesses, just by looking at the tmp result from the last loop
-     iteration.  But this does not work on big-endian machines.  Choose code
-     that works in both cases.  */
-
-  for (; n > 0; --n, ++char_ptr)
+  if (has_eq (word, repeated_c))
     {
-      if (*char_ptr == c)
-	return (void *) char_ptr;
+      /* We found a match, but it might be in a byte past the end of the
+	 array.  */
+      char *ret = (char *) word_ptr + index_first_eq (word, repeated_c);
+      if (ret <= lbyte)
+	return ret;
     }
-
   return NULL;
 }
-#ifdef weak_alias
+#ifndef MEMCHR
 weak_alias (__memchr, memchr)
-#endif
 libc_hidden_builtin_def (memchr)
+#endif
diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
index 39ff84f3f3..a78585650f 100644
--- a/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
+++ b/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
@@ -18,17 +18,11 @@
 
 #include <string.h>
 
-#define MEMCHR  __memchr_ppc
+extern __typeof (memchr) __memchr_ppc attribute_hidden;
 
-#undef weak_alias
-#define weak_alias(a, b)
+#define MEMCHR  __memchr_ppc
+#include <string/memchr.c>
 
 #ifdef SHARED
-# undef libc_hidden_builtin_def
-# define libc_hidden_builtin_def(name) \
-  __hidden_ver1(__memchr_ppc, __GI_memchr, __memchr_ppc);
+__hidden_ver1(__memchr_ppc, __GI_memchr, __memchr_ppc);
 #endif
-
-extern __typeof (memchr) __memchr_ppc attribute_hidden;
-
-#include <string/memchr.c>
diff --git a/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
index 8097df709c..49ba5521fe 100644
--- a/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
+++ b/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
@@ -18,14 +18,7 @@
 
 #include <string.h>
 
-#define MEMCHR  __memchr_ppc
-
-#undef weak_alias
-#define weak_alias(a, b)
-
-# undef libc_hidden_builtin_def
-# define libc_hidden_builtin_def(name)
-
 extern __typeof (memchr) __memchr_ppc attribute_hidden;
 
+#define MEMCHR  __memchr_ppc
 #include <string/memchr.c>
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 12/31] string: Improve generic strnlen with memchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (10 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 11/31] string: Improve generic memchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:27   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 13/31] string: Improve generic memrchr Adhemerval Zanella
                   ` (19 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It also cleanups the multiple inclusion by leaving the ifunc
implementation to undef the weak_alias and libc_hidden_def.

Co-authored-by: Richard Henderson  <richard.henderson@linaro.org>
---
 string/strnlen.c                              | 137 +-----------------
 sysdeps/i386/i686/multiarch/strnlen-c.c       |  14 +-
 .../power4/multiarch/strnlen-ppc32.c          |  14 +-
 sysdeps/s390/strnlen-c.c                      |  14 +-
 4 files changed, 27 insertions(+), 152 deletions(-)

diff --git a/string/strnlen.c b/string/strnlen.c
index 6ff294eab1..dc23354ec8 100644
--- a/string/strnlen.c
+++ b/string/strnlen.c
@@ -1,10 +1,6 @@
 /* Find the length of STRING, but scan at most MAXLEN characters.
    Copyright (C) 1991-2023 Free Software Foundation, Inc.
 
-   Based on strlen written by Torbjorn Granlund (tege@sics.se),
-   with help from Dan Sahlin (dan@sics.se);
-   commentary by Jim Blandy (jimb@ai.mit.edu).
-
    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
@@ -20,7 +16,6 @@
    not, see <https://www.gnu.org/licenses/>.  */
 
 #include <string.h>
-#include <stdlib.h>
 
 /* Find the length of S, but scan at most MAXLEN characters.  If no
    '\0' terminator is found in that many characters, return MAXLEN.  */
@@ -32,134 +27,12 @@
 size_t
 __strnlen (const char *str, size_t maxlen)
 {
-  const char *char_ptr, *end_ptr = str + maxlen;
-  const unsigned long int *longword_ptr;
-  unsigned long int longword, himagic, lomagic;
-
-  if (maxlen == 0)
-    return 0;
-
-  if (__glibc_unlikely (end_ptr < str))
-    end_ptr = (const char *) ~0UL;
-
-  /* Handle the first few characters by reading one character at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = str; ((unsigned long int) char_ptr
-			& (sizeof (longword) - 1)) != 0;
-       ++char_ptr)
-    if (*char_ptr == '\0')
-      {
-	if (char_ptr > end_ptr)
-	  char_ptr = end_ptr;
-	return char_ptr - str;
-      }
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to 8-byte longwords.  */
-
-  longword_ptr = (unsigned long int *) char_ptr;
-
-  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
-     the "holes."  Note that there is a hole just to the left of
-     each byte, with an extra at the end:
-
-     bits:  01111110 11111110 11111110 11111111
-     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
-
-     The 1-bits make sure that carries propagate to the next 0-bit.
-     The 0-bits provide holes for carries to fall into.  */
-  himagic = 0x80808080L;
-  lomagic = 0x01010101L;
-  if (sizeof (longword) > 4)
-    {
-      /* 64-bit version of the magic.  */
-      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
-      himagic = ((himagic << 16) << 16) | himagic;
-      lomagic = ((lomagic << 16) << 16) | lomagic;
-    }
-  if (sizeof (longword) > 8)
-    abort ();
-
-  /* Instead of the traditional loop which tests each character,
-     we will test a longword at a time.  The tricky part is testing
-     if *any of the four* bytes in the longword in question are zero.  */
-  while (longword_ptr < (unsigned long int *) end_ptr)
-    {
-      /* We tentatively exit the loop if adding MAGIC_BITS to
-	 LONGWORD fails to change any of the hole bits of LONGWORD.
-
-	 1) Is this safe?  Will it catch all the zero bytes?
-	 Suppose there is a byte with all zeros.  Any carry bits
-	 propagating from its left will fall into the hole at its
-	 least significant bit and stop.  Since there will be no
-	 carry from its most significant bit, the LSB of the
-	 byte to the left will be unchanged, and the zero will be
-	 detected.
-
-	 2) Is this worthwhile?  Will it ignore everything except
-	 zero bytes?  Suppose every byte of LONGWORD has a bit set
-	 somewhere.  There will be a carry into bit 8.  If bit 8
-	 is set, this will carry into bit 16.  If bit 8 is clear,
-	 one of bits 9-15 must be set, so there will be a carry
-	 into bit 16.  Similarly, there will be a carry into bit
-	 24.  If one of bits 24-30 is set, there will be a carry
-	 into bit 31, so all of the hole bits will be changed.
-
-	 The one misfire occurs when bits 24-30 are clear and bit
-	 31 is set; in this case, the hole at bit 31 is not
-	 changed.  If we had access to the processor carry flag,
-	 we could close this loophole by putting the fourth hole
-	 at bit 32!
-
-	 So it ignores everything except 128's, when they're aligned
-	 properly.  */
-
-      longword = *longword_ptr++;
-
-      if ((longword - lomagic) & himagic)
-	{
-	  /* Which of the bytes was the zero?  If none of them were, it was
-	     a misfire; continue the search.  */
-
-	  const char *cp = (const char *) (longword_ptr - 1);
-
-	  char_ptr = cp;
-	  if (cp[0] == 0)
-	    break;
-	  char_ptr = cp + 1;
-	  if (cp[1] == 0)
-	    break;
-	  char_ptr = cp + 2;
-	  if (cp[2] == 0)
-	    break;
-	  char_ptr = cp + 3;
-	  if (cp[3] == 0)
-	    break;
-	  if (sizeof (longword) > 4)
-	    {
-	      char_ptr = cp + 4;
-	      if (cp[4] == 0)
-		break;
-	      char_ptr = cp + 5;
-	      if (cp[5] == 0)
-		break;
-	      char_ptr = cp + 6;
-	      if (cp[6] == 0)
-		break;
-	      char_ptr = cp + 7;
-	      if (cp[7] == 0)
-		break;
-	    }
-	}
-      char_ptr = end_ptr;
-    }
-
-  if (char_ptr > end_ptr)
-    char_ptr = end_ptr;
-  return char_ptr - str;
+  const char *found = memchr (str, '\0', maxlen);
+  return found ? found - str : maxlen;
 }
+
 #ifndef STRNLEN
-libc_hidden_def (__strnlen)
 weak_alias (__strnlen, strnlen)
-#endif
+libc_hidden_def (__strnlen)
 libc_hidden_def (strnlen)
+#endif
diff --git a/sysdeps/i386/i686/multiarch/strnlen-c.c b/sysdeps/i386/i686/multiarch/strnlen-c.c
index 351e939a93..beb0350d53 100644
--- a/sysdeps/i386/i686/multiarch/strnlen-c.c
+++ b/sysdeps/i386/i686/multiarch/strnlen-c.c
@@ -1,10 +1,10 @@
 #define STRNLEN  __strnlen_ia32
+#include <string/strnlen.c>
+
 #ifdef SHARED
-# undef libc_hidden_def
-# define libc_hidden_def(name)  \
-    __hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32); \
-    strong_alias (__strnlen_ia32, __strnlen_ia32_1); \
-    __hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
+/* Alias for internal symbol to avoid PLT generation, it redirects the
+   libc_hidden_def (__strnlen/strlen) to default implementation.  */
+__hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32);
+strong_alias (__strnlen_ia32, __strnlen_ia32_1);
+__hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
 #endif
-
-#include "string/strnlen.c"
diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
index 957b9b99e8..2ca1cd7181 100644
--- a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
+++ b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
@@ -17,12 +17,12 @@
    <https://www.gnu.org/licenses/>.  */
 
 #define STRNLEN  __strnlen_ppc
+#include <string/strnlen.c>
+
 #ifdef SHARED
-# undef libc_hidden_def
-# define libc_hidden_def(name)  \
-    __hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
-    strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
-    __hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
+/* Alias for internal symbol to avoid PLT generation, it redirects the
+   libc_hidden_def (__strnlen/strlen) to default implementation.  */
+__hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
+strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
+__hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
 #endif
-
-#include <string/strnlen.c>
diff --git a/sysdeps/s390/strnlen-c.c b/sysdeps/s390/strnlen-c.c
index 172fcc7caa..95156a0ff5 100644
--- a/sysdeps/s390/strnlen-c.c
+++ b/sysdeps/s390/strnlen-c.c
@@ -21,14 +21,16 @@
 #if HAVE_STRNLEN_C
 # if HAVE_STRNLEN_IFUNC
 #  define STRNLEN STRNLEN_C
+# endif
+
+# include <string/strnlen.c>
+
+# if HAVE_STRNLEN_IFUNC
 #  if defined SHARED && IS_IN (libc)
-#   undef libc_hidden_def
-#   define libc_hidden_def(name)					\
-  __hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);	\
-  strong_alias (__strnlen_c, __strnlen_c_1);			\
-  __hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
+__hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);
+strong_alias (__strnlen_c, __strnlen_c_1);
+__hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
 #  endif
 # endif
 
-# include <string/strnlen.c>
 #endif
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 13/31] string: Improve generic memrchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (11 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 12/31] string: Improve generic strnlen with memchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen Adhemerval Zanella
                   ` (18 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

New algorithm read the lastaligned address and mask off the unwanted
bytes.  The loop now read word-aligned address and check using the
has_eq macro.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
---
 string/memrchr.c | 196 ++++++++++-------------------------------------
 1 file changed, 39 insertions(+), 157 deletions(-)

diff --git a/string/memrchr.c b/string/memrchr.c
index 18b20ff76a..b37f2a68c8 100644
--- a/string/memrchr.c
+++ b/string/memrchr.c
@@ -1,11 +1,6 @@
 /* memrchr -- find the last occurrence of a byte in a memory block
    Copyright (C) 1991-2023 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
-   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
-   with help from Dan Sahlin (dan@sics.se) and
-   commentary by Jim Blandy (jimb@ai.mit.edu);
-   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
-   and implemented by Roland McGrath (roland@ai.mit.edu).
 
    The GNU C Library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
@@ -21,177 +16,64 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#include <stdlib.h>
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#if defined _LIBC
-# include <string.h>
-# include <memcopy.h>
-#endif
-
-#if defined HAVE_LIMITS_H || defined _LIBC
-# include <limits.h>
-#endif
-
-#define LONG_MAX_32_BITS 2147483647
-
-#ifndef LONG_MAX
-# define LONG_MAX LONG_MAX_32_BITS
-#endif
-
-#include <sys/types.h>
+#include <string-fzb.h>
+#include <string-fzc.h>
+#include <string-fzi.h>
+#include <string-shift.h>
+#include <string.h>
+#include <libc-pointer-arith.h>
 
 #undef __memrchr
 #undef memrchr
 
-#ifndef weak_alias
-# define __memrchr memrchr
+#ifdef MEMRCHR
+# define __memrchr MEMRCHR
 #endif
 
-/* Search no more than N bytes of S for C.  */
 void *
-#ifndef MEMRCHR
-__memrchr
-#else
-MEMRCHR
-#endif
-     (const void *s, int c_in, size_t n)
+__memrchr (const void *s, int c_in, size_t n)
 {
-  const unsigned char *char_ptr;
-  const unsigned long int *longword_ptr;
-  unsigned long int longword, magic_bits, charmask;
-  unsigned char c;
-
-  c = (unsigned char) c_in;
-
-  /* Handle the last few characters by reading one character at a time.
-     Do this until CHAR_PTR is aligned on a longword boundary.  */
-  for (char_ptr = (const unsigned char *) s + n;
-       n > 0 && ((unsigned long int) char_ptr
-		 & (sizeof (longword) - 1)) != 0;
-       --n)
-    if (*--char_ptr == c)
-      return (void *) char_ptr;
-
-  /* All these elucidatory comments refer to 4-byte longwords,
-     but the theory applies equally well to 8-byte longwords.  */
-
-  longword_ptr = (const unsigned long int *) char_ptr;
-
-  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
-     the "holes."  Note that there is a hole just to the left of
-     each byte, with an extra at the end:
+  if (__glibc_unlikely (n == 0))
+    return NULL;
 
-     bits:  01111110 11111110 11111110 11111111
-     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
+  const op_t *word_ptr = (const op_t *) PTR_ALIGN_UP (s + n, sizeof (op_t));
+  uintptr_t s_int = (uintptr_t) s + n;
 
-     The 1-bits make sure that carries propagate to the next 0-bit.
-     The 0-bits provide holes for carries to fall into.  */
-  magic_bits = -1;
-  magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
+  op_t word = *--word_ptr;
+  op_t repeated_c = repeat_bytes (c_in);
 
-  /* Set up a longword, each of whose bytes is C.  */
-  charmask = c | (c << 8);
-  charmask |= charmask << 16;
-#if LONG_MAX > LONG_MAX_32_BITS
-  charmask |= charmask << 32;
-#endif
+  /* Compute the address of the word containing the initial byte. */
+  const op_t *sword = (const op_t *) PTR_ALIGN_DOWN (s, sizeof (op_t));
 
-  /* Instead of the traditional loop which tests each character,
-     we will test a longword at a time.  The tricky part is testing
-     if *any of the four* bytes in the longword in question are zero.  */
-  while (n >= sizeof (longword))
+  /* If the end of buffer is not op_t aligned, mask off the undesirable bits
+     before find the last byte position.  */
+  find_t mask = shift_find_last (find_eq_all (word, repeated_c), s_int);
+  if (mask != 0)
     {
-      /* We tentatively exit the loop if adding MAGIC_BITS to
-	 LONGWORD fails to change any of the hole bits of LONGWORD.
-
-	 1) Is this safe?  Will it catch all the zero bytes?
-	 Suppose there is a byte with all zeros.  Any carry bits
-	 propagating from its left will fall into the hole at its
-	 least significant bit and stop.  Since there will be no
-	 carry from its most significant bit, the LSB of the
-	 byte to the left will be unchanged, and the zero will be
-	 detected.
-
-	 2) Is this worthwhile?  Will it ignore everything except
-	 zero bytes?  Suppose every byte of LONGWORD has a bit set
-	 somewhere.  There will be a carry into bit 8.  If bit 8
-	 is set, this will carry into bit 16.  If bit 8 is clear,
-	 one of bits 9-15 must be set, so there will be a carry
-	 into bit 16.  Similarly, there will be a carry into bit
-	 24.  If one of bits 24-30 is set, there will be a carry
-	 into bit 31, so all of the hole bits will be changed.
-
-	 The one misfire occurs when bits 24-30 are clear and bit
-	 31 is set; in this case, the hole at bit 31 is not
-	 changed.  If we had access to the processor carry flag,
-	 we could close this loophole by putting the fourth hole
-	 at bit 32!
-
-	 So it ignores everything except 128's, when they're aligned
-	 properly.
-
-	 3) But wait!  Aren't we looking for C, not zero?
-	 Good point.  So what we do is XOR LONGWORD with a longword,
-	 each of whose bytes is C.  This turns each byte that is C
-	 into a zero.  */
-
-      longword = *--longword_ptr ^ charmask;
-
-      /* Add MAGIC_BITS to LONGWORD.  */
-      if ((((longword + magic_bits)
-
-	    /* Set those bits that were unchanged by the addition.  */
-	    ^ ~longword)
-
-	   /* Look at only the hole bits.  If any of the hole bits
-	      are unchanged, most likely one of the bytes was a
-	      zero.  */
-	   & ~magic_bits) != 0)
-	{
-	  /* Which of the bytes was C?  If none of them were, it was
-	     a misfire; continue the search.  */
-
-	  const unsigned char *cp = (const unsigned char *) longword_ptr;
-
-#if LONG_MAX > 2147483647
-	  if (cp[7] == c)
-	    return (void *) &cp[7];
-	  if (cp[6] == c)
-	    return (void *) &cp[6];
-	  if (cp[5] == c)
-	    return (void *) &cp[5];
-	  if (cp[4] == c)
-	    return (void *) &cp[4];
-#endif
-	  if (cp[3] == c)
-	    return (void *) &cp[3];
-	  if (cp[2] == c)
-	    return (void *) &cp[2];
-	  if (cp[1] == c)
-	    return (void *) &cp[1];
-	  if (cp[0] == c)
-	    return (void *) cp;
-	}
-
-      n -= sizeof (longword);
+      char *ret = (char *) word_ptr + index_last (mask);
+      return ret >= (char *) s ? ret : NULL;
     }
+  if (word_ptr == sword)
+    return NULL;
+  word = *--word_ptr;
 
-  char_ptr = (const unsigned char *) longword_ptr;
-
-  while (n-- > 0)
+  while (word_ptr != sword)
     {
-      if (*--char_ptr == c)
-	return (void *) char_ptr;
+      if (has_eq (word, repeated_c))
+	return (char *) word_ptr + index_last_eq (word, repeated_c);
+      word = *--word_ptr;
     }
 
-  return 0;
+  if (has_eq (word, repeated_c))
+    {
+      /* We found a match, but it might be in a byte past the end of the
+	 array.  */
+      char *ret = (char *) word_ptr + index_last_eq (word, repeated_c);
+      if (ret >= (char *) s)
+	return ret;
+    }
+  return NULL;
 }
 #ifndef MEMRCHR
-# ifdef weak_alias
 weak_alias (__memrchr, memrchr)
-# endif
 #endif
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (12 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 13/31] string: Improve generic memrchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-04  3:06   ` Richard Henderson
  2023-02-02 18:11 ` [PATCH v12 15/31] hppa: Add memcopy.h Adhemerval Zanella
                   ` (17 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Now that both strlen and memrchr have word vectorized implementation,
it should be faster to implement strrchr based on memrchr over the
string length instead of calling strchr on a loop.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).
---
 string/strrchr.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)

diff --git a/string/strrchr.c b/string/strrchr.c
index 3c6e715d3b..7b76dea4e0 100644
--- a/string/strrchr.c
+++ b/string/strrchr.c
@@ -27,23 +27,7 @@
 char *
 STRRCHR (const char *s, int c)
 {
-  const char *found, *p;
-
-  c = (unsigned char) c;
-
-  /* Since strchr is fast, we use it rather than the obvious loop.  */
-
-  if (c == '\0')
-    return strchr (s, '\0');
-
-  found = NULL;
-  while ((p = strchr (s, c)) != NULL)
-    {
-      found = p;
-      s = p + 1;
-    }
-
-  return (char *) found;
+  return __memrchr (s, c, strlen (s) + 1);
 }
 
 #ifdef weak_alias
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 15/31] hppa: Add memcopy.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (13 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 16/31] hppa: Add string-fza.h, string-fzc.h, and string-fzi.h Adhemerval Zanella
                   ` (16 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

From: Richard Henderson <richard.henderson@linaro.org>

GCC's combine pass cannot merge (x >> c | y << (32 - c)) into a
double-word shift unless (1) the subtract is in the same basic block
and (2) the result of the subtract is used exactly once.  Neither
condition is true for any use of MERGE.

By forcing the use of a double-word shift, we not only reduce
contention on SAR, but also allow the setting of SAR to be hoisted
outside of a loop.

Checked on hppa-linux-gnu.

Reviewewd-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/hppa/memcopy.h | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 sysdeps/hppa/memcopy.h

diff --git a/sysdeps/hppa/memcopy.h b/sysdeps/hppa/memcopy.h
new file mode 100644
index 0000000000..0d4b4ac435
--- /dev/null
+++ b/sysdeps/hppa/memcopy.h
@@ -0,0 +1,42 @@
+/* Definitions for memory copy functions, PA-RISC 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 <sysdeps/generic/memcopy.h>
+
+/* Use a single double-word shift instead of two shifts and an ior.
+   If the uses of MERGE were close to the computation of shl/shr,
+   the compiler might have been able to create this itself.
+   But instead that computation is well separated.
+
+   Using an inline function instead of a macro is the easiest way
+   to ensure that the types are correct.  */
+
+#undef MERGE
+
+static __always_inline op_t
+MERGE (op_t w0, int shl, op_t w1, int shr)
+{
+  _Static_assert (OPSIZ == 4 || OPSIZ == 8, "Invalid OPSIZE");
+
+  op_t res;
+  if (OPSIZ == 4)
+    asm ("shrpw %1,%2,%%sar,%0" : "=r"(res) : "r"(w0), "r"(w1), "q"(shr));
+  else if (OPSIZ == 8)
+    asm ("shrpd %1,%2,%%sar,%0" : "=r"(res) : "r"(w0), "r"(w1), "q"(shr));
+  return res;
+}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 16/31] hppa: Add string-fza.h, string-fzc.h, and string-fzi.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (14 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 15/31] hppa: Add memcopy.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 17/31] alpha: Add string-fza, string-fzb.h, string-fzi.h, and string-shift.h Adhemerval Zanella
                   ` (15 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

From: Richard Henderson <richard.henderson@linaro.org>

Use UXOR,SBZ to test for a zero byte within a word.  While we can
get semi-decent code out of asm-goto, we would do slightly better
with a compiler builtin.

For index_zero et al, sequential testing of bytes is less expensive than
any tricks that involve a count-leading-zeros insn that we don't have.

Checked on hppa-linux-gnu.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/hppa/string-fzb.h |  63 +++++++++++++++++++
 sysdeps/hppa/string-fzc.h | 124 ++++++++++++++++++++++++++++++++++++++
 sysdeps/hppa/string-fzi.h |  63 +++++++++++++++++++
 3 files changed, 250 insertions(+)
 create mode 100644 sysdeps/hppa/string-fzb.h
 create mode 100644 sysdeps/hppa/string-fzc.h
 create mode 100644 sysdeps/hppa/string-fzi.h

diff --git a/sysdeps/hppa/string-fzb.h b/sysdeps/hppa/string-fzb.h
new file mode 100644
index 0000000000..d6b234a271
--- /dev/null
+++ b/sysdeps/hppa/string-fzb.h
@@ -0,0 +1,63 @@
+/* Zero byte detection, boolean.  HPPA 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/>.  */
+
+#ifndef _STRING_FZB_H
+#define _STRING_FZB_H 1
+
+#include <sys/cdefs.h>
+#include <string-optype.h>
+
+_Static_assert (sizeof (op_t) == 4, "64-bit not supported");
+
+/* Determine if any byte within X is zero.  This is a pure boolean test.  */
+static __always_inline _Bool
+has_zero (op_t x)
+{
+  /* It's more useful to expose a control transfer to the compiler
+     than to expose a proper boolean result.  */
+  asm goto ("uxor,sbz %%r0,%0,%%r0\n\t"
+	    "b,n %l1" : : "r"(x) : : nbz);
+  return 1;
+ nbz:
+  return 0;
+}
+
+/* Likewise, but for byte equality between X1 and X2.  */
+static __always_inline _Bool
+has_eq (op_t x1, op_t x2)
+{
+  asm goto ("uxor,sbz %0,%1,%%r0\n\t"
+	    "b,n %l2" : : "r"(x1), "r"(x2) : : nbz);
+  return 1;
+ nbz:
+  return 0;
+}
+
+/* Likewise, but for zeros in X1 and equal bytes between X1 and X2.  */
+static __always_inline _Bool
+has_zero_eq (op_t x1, op_t x2)
+{
+  asm goto ("uxor,sbz %%r0,%0,%%r0\n\t"
+	    "uxor,nbz %0,%1,%%r0\n\t"
+	    "b,n %l2" : : "r"(x1), "r"(x2) : : sbz);
+  return 0;
+ sbz:
+  return 1;
+}
+
+#endif /* _STRING_FZB_H */
diff --git a/sysdeps/hppa/string-fzc.h b/sysdeps/hppa/string-fzc.h
new file mode 100644
index 0000000000..c4e2e11550
--- /dev/null
+++ b/sysdeps/hppa/string-fzc.h
@@ -0,0 +1,124 @@
+/* string-fzc.h -- zero byte detection with indexes.  HPPA 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/>.  */
+
+#ifndef _STRING_FZC_H
+#define _STRING_FZC_H 1
+
+#include <string-optype.h>
+
+_Static_assert (sizeof (op_t) == 4, "64-bit not supported");
+
+/* Given a word X that is known to contain a zero byte, return the
+   index of the first such within the long in memory order.  */
+static __always_inline unsigned int
+index_first_zero (op_t x)
+{
+  unsigned int ret;
+
+  /* Since we have no clz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,<> %1,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,<> %1,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,<> %1,7,8,%%r0\n\t"
+       "ldi 0,%0"
+       : "=r"(ret) : "r"(x), "0"(3));
+
+  return ret;
+}
+
+/* Similarly, but perform the search for byte equality between X1 and X2.  */
+static __always_inline unsigned int
+index_first_eq (op_t x1, op_t x2)
+{
+  return index_first_zero (x1 ^ x2);
+}
+
+/* Similarly, but perform the search for zero within X1 or
+   equality between X1 and X2.  */
+static __always_inline unsigned int
+index_first_zero_eq (op_t x1, op_t x2)
+{
+  unsigned int ret;
+
+  /* Since we have no clz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,= %1,23,8,%%r0\n\t"
+       "extrw,u,<> %2,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,= %1,15,8,%%r0\n\t"
+       "extrw,u,<> %2,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,= %1,7,8,%%r0\n\t"
+       "extrw,u,<> %2,7,8,%%r0\n\t"
+       "ldi 0,%0"
+       : "=r"(ret) : "r"(x1), "r"(x1 ^ x2), "0"(3));
+
+  return ret;
+}
+
+/* Similarly, but perform the search for zero within X1 or
+   inequality between X1 and X2. */
+static __always_inline unsigned int
+index_first_zero_ne (op_t x1, op_t x2)
+{
+  unsigned int ret;
+
+  /* Since we have no clz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,<> %2,23,8,%%r0\n\t"
+       "extrw,u,<> %1,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,<> %2,15,8,%%r0\n\t"
+       "extrw,u,<> %1,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,<> %2,7,8,%%r0\n\t"
+       "extrw,u,<> %1,7,8,%%r0\n\t"
+       "ldi 0,%0"
+       : "=r"(ret) : "r"(x1), "r"(x1 ^ x2), "0"(3));
+
+  return ret;
+}
+
+/* Similarly, but search for the last zero within X.  */
+static __always_inline unsigned int
+index_last_zero (op_t x)
+{
+  unsigned int ret;
+
+  /* Since we have no ctz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,<> %1,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,<> %1,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,<> %1,31,8,%%r0\n\t"
+       "ldi 3,%0"
+       : "=r"(ret) : "r"(x), "0"(0));
+
+  return ret;
+}
+
+static __always_inline unsigned int
+index_last_eq (op_t x1, op_t x2)
+{
+  return index_last_zero (x1 ^ x2);
+}
+
+#endif /* _STRING_FZC_H */
diff --git a/sysdeps/hppa/string-fzi.h b/sysdeps/hppa/string-fzi.h
new file mode 100644
index 0000000000..08cdace416
--- /dev/null
+++ b/sysdeps/hppa/string-fzi.h
@@ -0,0 +1,63 @@
+/* string-fzi.h -- zero byte indexes.  HPPA 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/>.  */
+
+#ifndef _STRING_FZI_H
+#define _STRING_FZI_H 1
+
+#include <string-optype.h>
+#include <string-fza.h>
+
+_Static_assert (sizeof (op_t) == 4, "64-bit not supported");
+
+static __always_inline unsigned int
+index_first (find_t c)
+{
+  unsigned int ret;
+
+  /* Since we have no clz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,= %1,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,= %1,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,= %1,7,8,%%r0\n\t"
+       "ldi 0,%0"
+       : "=r"(ret) : "r"(c), "0"(3));
+
+  return ret;
+}
+
+static __always_inline unsigned int
+index_last (find_t c)
+{
+  unsigned int ret;
+
+  /* Since we have no ctz insn, direct tests of the bytes is faster
+     than loading up the constants to do the masking.  */
+  asm ("extrw,u,= %1,15,8,%%r0\n\t"
+       "ldi 1,%0\n\t"
+       "extrw,u,= %1,23,8,%%r0\n\t"
+       "ldi 2,%0\n\t"
+       "extrw,u,= %1,31,8,%%r0\n\t"
+       "ldi 3,%0"
+       : "=r"(ret) : "r"(c), "0"(0));
+
+  return ret;
+}
+
+#endif /* _STRING_FZI_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 17/31] alpha: Add string-fza, string-fzb.h, string-fzi.h, and string-shift.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (15 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 16/31] hppa: Add string-fza.h, string-fzc.h, and string-fzi.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 18/31] arm: Add string-fza.h Adhemerval Zanella
                   ` (14 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

From: Richard Henderson <richard.henderson@linaro.org>

While alpha has the more important string functions in assembly,
there are still a few for find the generic routines are used.

Use the CMPBGE insn, via the builtin, for testing of zeros.  Use a
simplified expansion of __builtin_ctz when the insn isn't available.

Checked on alpha-linux-gnu.

Co-authored-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/alpha/string-fza.h   | 61 +++++++++++++++++++++++++++++++++++
 sysdeps/alpha/string-fzb.h   | 52 ++++++++++++++++++++++++++++++
 sysdeps/alpha/string-fzi.h   | 62 ++++++++++++++++++++++++++++++++++++
 sysdeps/alpha/string-shift.h | 44 +++++++++++++++++++++++++
 4 files changed, 219 insertions(+)
 create mode 100644 sysdeps/alpha/string-fza.h
 create mode 100644 sysdeps/alpha/string-fzb.h
 create mode 100644 sysdeps/alpha/string-fzi.h
 create mode 100644 sysdeps/alpha/string-shift.h

diff --git a/sysdeps/alpha/string-fza.h b/sysdeps/alpha/string-fza.h
new file mode 100644
index 0000000000..b4a7080c44
--- /dev/null
+++ b/sysdeps/alpha/string-fza.h
@@ -0,0 +1,61 @@
+/* Basic zero byte detection.  Generic C 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/>.  */
+
+#ifndef _STRING_FZA_H
+#define _STRING_FZA_H 1
+
+#include <string-misc.h>
+#include <string-optype.h>
+
+/* The CMPBGE instruction creates a bit mask rather than a byte mask.
+   However, if we narrow find_t to either 'int' or 'uint8_t', we get
+   unnecessary truncation instructions from the 'unsigned long' type
+   returned by __builtin_alpha_cmpbge.  */
+typedef op_t find_t;
+
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  return __builtin_alpha_cmpbge (0, x);
+}
+
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_zero_all (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | (find_zero_all (x1 ^ x2) ^ 0xff);
+}
+
+/* Define the "inexact" versions in terms of the exact versions.  */
+#define find_zero_low		find_zero_all
+#define find_eq_low		find_eq_all
+#define find_zero_eq_low	find_zero_eq_all
+#define find_zero_ne_low	find_zero_ne_all
+
+#endif /* _STRING_FZA_H */
diff --git a/sysdeps/alpha/string-fzb.h b/sysdeps/alpha/string-fzb.h
new file mode 100644
index 0000000000..e3934ba413
--- /dev/null
+++ b/sysdeps/alpha/string-fzb.h
@@ -0,0 +1,52 @@
+/* Zero byte detection; boolean.  Alpha 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/>.  */
+
+#ifndef _STRING_FZB_H
+#define _STRING_FZB_H 1
+
+#include <sys/cdefs.h>
+#include <string-optype.h>
+
+/* Note that since CMPBGE creates a bit mask rather than a byte mask,
+   we cannot simply provide a target-specific string-fza.h.  */
+
+/* Determine if any byte within X is zero.  This is a pure boolean test.  */
+
+static __always_inline _Bool
+has_zero (op_t x)
+{
+  return __builtin_alpha_cmpbge (0, x) != 0;
+}
+
+/* Likewise, but for byte equality between X1 and X2.  */
+
+static __always_inline _Bool
+has_eq (op_t x1, op_t x2)
+{
+  return has_zero (x1 ^ x2);
+}
+
+/* Likewise, but for zeros in X1 and equal bytes between X1 and X2.  */
+
+static __always_inline _Bool
+has_zero_eq (op_t x1, op_t x2)
+{
+  return has_zero (x1) | has_eq (x1, x2);
+}
+
+#endif /* _STRING_FZB_H */
diff --git a/sysdeps/alpha/string-fzi.h b/sysdeps/alpha/string-fzi.h
new file mode 100644
index 0000000000..aba7ca4f3d
--- /dev/null
+++ b/sysdeps/alpha/string-fzi.h
@@ -0,0 +1,62 @@
+/* string-fzi.h -- zero byte detection; indices.  Alpha 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/>.  */
+
+#ifndef _STRING_FZI_H
+#define _STRING_FZI_H
+
+#include <stdint.h>
+#include <string-optype.h>
+#include <string-fza.h>
+
+/* Note that since CMPBGE creates a bit mask rather than a byte mask,
+   we cannot simply provide a target-specific string-fza.h.  */
+
+/* A subroutine for the index_zero functions.  Given a bitmask C,
+   return the index of the first bit set in memory order.  */
+static __always_inline unsigned int
+index_first (find_t c)
+{
+#ifdef __alpha_cix__
+  return __builtin_ctzl (c);
+#else
+  c = c & -c;
+  return (c & 0xf0 ? 4 : 0) + (c & 0xcc ? 2 : 0) + (c & 0xaa ? 1 : 0);
+#endif
+}
+
+/* Similarly, but return the (memory order) index of the last bit
+   that is non-zero.  Note that only the least 8 bits may be nonzero.  */
+
+static __always_inline unsigned int
+index_last (find_t x)
+{
+#ifdef __alpha_cix__
+  return __builtin_clzl (x) ^ 63;
+#else
+  unsigned r = 0;
+  if (x & 0xf0)
+    r += 4;
+  if (x & (0xc << r))
+    r += 2;
+  if (x & (0x2 << r))
+    r += 1;
+  return r;
+#endif
+}
+
+#endif /* _STRING_FZI_H */
diff --git a/sysdeps/alpha/string-shift.h b/sysdeps/alpha/string-shift.h
new file mode 100644
index 0000000000..4e2cb2fea1
--- /dev/null
+++ b/sysdeps/alpha/string-shift.h
@@ -0,0 +1,44 @@
+/* Shift unaligned word read.  Alpha 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/>.  */
+
+#ifndef _STRING_SHIFT_H
+#define _STRING_SHIFT_H 1
+
+#include <limits.h>
+#include <stdint.h>
+#include <string-fza.h>
+
+/* Return the mask WORD shifted based on S_INT address value, to ignore
+   values not presented in the aligned word read.  */
+static __always_inline find_t
+shift_find (find_t word, uintptr_t s)
+{
+  return word >> (s % sizeof (op_t));
+}
+
+/* Mask off the bits defined the the S alignment value.  */
+static __always_inline find_t
+shift_find_last (find_t word, uintptr_t s)
+{
+  s = s % sizeof (op_t);
+  if (s == 0)
+    return word;
+  return word & ~((op_t)-1 << s);
+}
+
+#endif /* _STRING_SHIFT_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 18/31] arm: Add string-fza.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (16 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 17/31] alpha: Add string-fza, string-fzb.h, string-fzi.h, and string-shift.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-20 13:24   ` Szabolcs Nagy
  2023-02-02 18:11 ` [PATCH v12 19/31] powerpc: " Adhemerval Zanella
                   ` (13 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

From: Richard Henderson <richard.henderson@linaro.org>

While arm has the more important string functions in assembly,
there are still a few generic routines used.

Use the UQSUB8 insn for testing of zeros.

Checked on armv7-linux-gnueabihf
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/arm/armv6t2/string-fza.h | 68 ++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 sysdeps/arm/armv6t2/string-fza.h

diff --git a/sysdeps/arm/armv6t2/string-fza.h b/sysdeps/arm/armv6t2/string-fza.h
new file mode 100644
index 0000000000..50afaba038
--- /dev/null
+++ b/sysdeps/arm/armv6t2/string-fza.h
@@ -0,0 +1,68 @@
+/* Zero byte detection; basics.  ARM 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/>.  */
+
+#ifndef _STRING_FZA_H
+#define _STRING_FZA_H 1
+
+#include <string-misc.h>
+#include <string-optype.h>
+
+/* The functions return a byte mask.  */
+typedef op_t find_t;
+
+/* This function returns at least one bit set within every byte
+   of X that is zero.  */
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  /* Use unsigned saturated subtraction from 1 in each byte.
+     That leaves 1 for every byte that was zero.  */
+  op_t ones = repeat_bytes (0x01);
+  return __builtin_arm_uqsub8 (ones, x);
+}
+
+/* Identify bytes that are equal between X1 and X2.  */
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1 ^ x2);
+}
+
+/* Identify zero bytes in X1 or equality between X1 and X2.  */
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_zero_all (x1 ^ x2);
+}
+
+/* Identify zero bytes in X1 or inequality between X1 and X2.  */
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  /* Make use of the fact that we'll already have ONES in a register.  */
+  op_t ones = repeat_bytes (0x01);
+  return find_zero_all (x1) | (find_zero_all (x1 ^ x2) ^ ones);
+}
+
+/* Define the "inexact" versions in terms of the exact versions.  */
+#define find_zero_low		find_zero_all
+#define find_eq_low		find_eq_all
+#define find_zero_eq_low	find_zero_eq_all
+#define find_zero_ne_low	find_zero_ne_all
+
+#endif /* _STRING_FZA_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 19/31] powerpc: Add string-fza.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (17 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 18/31] arm: Add string-fza.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 20/31] sh: Add string-fzb.h Adhemerval Zanella
                   ` (12 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

From: Richard Henderson <richard.henderson@linaro.org>

While ppc has the more important string functions in assembly,
there are still a few generic routines used.

Use the Power 6 CMPB insn for testing of zeros.

Checked on powerpc64le-linux-gnu.
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
---
 sysdeps/powerpc/string-fza.h | 72 ++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 sysdeps/powerpc/string-fza.h

diff --git a/sysdeps/powerpc/string-fza.h b/sysdeps/powerpc/string-fza.h
new file mode 100644
index 0000000000..5dec740041
--- /dev/null
+++ b/sysdeps/powerpc/string-fza.h
@@ -0,0 +1,72 @@
+/* Zero byte detection; basics.  PowerPC 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/>.  */
+
+#ifndef _POWERPC_STRING_FZA_H
+#define _POWERPC_STRING_FZA_H 1
+
+/* PowerISA 2.05 (POWER6) provides cmpb instruction.  */
+#ifdef _ARCH_PWR6
+# include <string-misc.h>
+# include <string-optype.h>
+
+/* The functions return a byte mask.  */
+typedef op_t find_t;
+
+/* This function returns 0xff for each byte that is
+   equal between X1 and X2.  */
+
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return __builtin_cmpb (x1, x2);
+}
+
+/* This function returns 0xff for each byte that is zero in X.  */
+
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  return find_eq_all (x, 0);
+}
+
+/* Identify zero bytes in X1 or equality between X1 and X2.  */
+
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_eq_all (x1, x2);
+}
+
+/* Identify zero bytes in X1 or inequality between X1 and X2.  */
+
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | ~find_eq_all (x1, x2);
+}
+
+/* Define the "inexact" versions in terms of the exact versions.  */
+# define find_zero_low		find_zero_all
+# define find_eq_low		find_eq_all
+# define find_zero_eq_low	find_zero_eq_all
+# define find_zero_ne_low	find_zero_ne_all
+#else
+# include <sysdeps/generic/string-fza.h>
+#endif /* _ARCH_PWR6  */
+
+#endif /* _POWERPC_STRING_FZA_H  */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 20/31] sh: Add string-fzb.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (18 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 19/31] powerpc: " Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h Adhemerval Zanella
                   ` (11 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Use the SH cmp/str on has_{zero,eq,zero_eq}.

Checked on sh4-linux-gnu.
---
 sysdeps/sh/string-fzb.h | 55 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 sysdeps/sh/string-fzb.h

diff --git a/sysdeps/sh/string-fzb.h b/sysdeps/sh/string-fzb.h
new file mode 100644
index 0000000000..c8dd176362
--- /dev/null
+++ b/sysdeps/sh/string-fzb.h
@@ -0,0 +1,55 @@
+/* Zero byte detection; boolean.  SH4 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/>.  */
+
+#ifndef STRING_FZB_H
+#define STRING_FZB_H 1
+
+#include <sys/cdefs.h>
+#include <string-optype.h>
+
+/* Determine if any bytes within X1 and X2 are equal.  */
+static __always_inline _Bool
+has_eq (op_t x1, op_t x2)
+{
+  int ret;
+
+  /* TODO: A compiler builtin for cmp/str would be much better.  It is
+     difficult to use asm goto here, because the range of bt/bf are
+     quite small.  */
+  asm("cmp/str %1,%2\n\t"
+      "movt %0"
+      : "=r" (ret) : "r" (x1), "r" (x2) : "t");
+
+  return ret;
+}
+
+/* Determine if any byte within X is zero.  */
+static __always_inline _Bool
+has_zero (op_t x)
+{
+  return has_eq (x, 0);
+}
+
+/* Likewise, but for zeros in X1 and equal bytes between X1 and X2.  */
+static __always_inline _Bool
+has_zero_eq (op_t x1, op_t x2)
+{
+  return has_zero (x1) | has_eq (x1, x2);
+}
+
+#endif /* STRING_FZB_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (19 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 20/31] sh: Add string-fzb.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 19:47   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 22/31] string: Hook up the default implementation on test-strlen Adhemerval Zanella
                   ` (10 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

It uses the bitmanip extension to optimize index_fist and index_last
with clz/ctz (using generic implementation that routes to compiler
builtin) and orc.b to check null bytes.

Checked the string test on riscv64 user mode.
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 sysdeps/riscv/string-fza.h | 70 ++++++++++++++++++++++++++++++++++
 sysdeps/riscv/string-fzi.h | 77 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 sysdeps/riscv/string-fza.h
 create mode 100644 sysdeps/riscv/string-fzi.h

diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
new file mode 100644
index 0000000000..9c7a6efba2
--- /dev/null
+++ b/sysdeps/riscv/string-fza.h
@@ -0,0 +1,70 @@
+/* Zero byte detection; basics.  RISCV 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/>.  */
+
+#ifndef _RISCV_STRING_FZA_H
+#define _RISCV_STRING_FZA_H 1
+
+#ifdef __riscv_zbb
+/* With bitmap extension we can use orc.b to find all zero bytes.  */
+# include <string-misc.h>
+# include <string-optype.h>
+
+/* The functions return a byte mask.  */
+typedef op_t find_t;
+
+/* This function returns 0xff for each byte that is zero in X.  */
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  find_t r;
+  asm ("orc.b %0, %1" : "=r" (r) : "r" (x));
+  return ~r;
+}
+
+/* This function returns 0xff for each byte that is equal between X1 and
+   X2.  */
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1 ^ x2);
+}
+
+/* Identify zero bytes in X1 or equality between X1 and X2.  */
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_eq_all (x1, x2);
+}
+
+/* Identify zero bytes in X1 or inequality between X1 and X2.  */
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | ~find_eq_all (x1, x2);
+}
+
+/* Define the "inexact" versions in terms of the exact versions.  */
+# define find_zero_low		find_zero_all
+# define find_eq_low		find_eq_all
+# define find_zero_eq_low	find_zero_eq_all
+# define find_zero_ne_low	find_zero_ne_all
+#else
+#include <sysdeps/generic/string-fza.h>
+#endif
+
+#endif /* _RISCV_STRING_FZA_H  */
diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
new file mode 100644
index 0000000000..3cde113afb
--- /dev/null
+++ b/sysdeps/riscv/string-fzi.h
@@ -0,0 +1,77 @@
+/* Zero byte detection; indexes.  RISCV 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/>.  */
+
+#ifndef _STRING_RISCV_FZI_H
+#define _STRING_RISCV_FZI_H 1
+
+#ifdef __riscv_zbb
+# include <sysdeps/generic/string-fzi.h>
+#else
+/* Without bitmap clz/ctz extensions, it is faster to direct test the bits
+   instead of calling compiler auxiliary functions.  */ 
+# include <string-optype.h>
+
+static __always_inline unsigned int
+index_first (find_t c)
+{
+  if (c & 0x80U)
+    return 0;
+  if (c & 0x8000U)
+    return 1;
+  if (c & 0x800000U)
+    return 2;
+
+  if (sizeof (op_t) == 4)
+    return 3;
+
+  if (c & 0x80000000U)
+    return 3;
+  if (c & 0x8000000000UL)
+    return 4;
+  if (c & 0x800000000000UL)
+    return 5;
+  if (c & 0x80000000000000UL)
+    return 6;
+  return 7;
+}
+
+static __always_inline unsigned int
+index_last (find_t c)
+{
+  if (sizeof (op_t) == 8)
+    {
+      if (c & 0x8000000000000000UL)
+	return 7;
+      if (c & 0x80000000000000UL)
+	return 6;
+      if (c & 0x800000000000UL)
+	return 5;
+      if (c & 0x8000000000UL)
+	return 4;
+    }
+  if (c & 0x80000000U)
+    return 3;
+  if (c & 0x800000U)
+    return 2;
+  if (c & 0x8000U)
+    return 1;
+  return 0;
+}
+#endif
+
+#endif /* STRING_FZI_H */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 22/31] string: Hook up the default implementation on test-strlen
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (20 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03 23:30   ` Noah Goldstein
  2023-02-02 18:11 ` [PATCH v12 23/31] string: Hook up the default implementation on test-strnlen Adhemerval Zanella
                   ` (9 subsequent siblings)
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Also remove the simple_STRLEN and builtin_strlen, which are not used
anywhere.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strlen.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/string/test-strlen.c b/string/test-strlen.c
index 81adf23737..0f98576f40 100644
--- a/string/test-strlen.c
+++ b/string/test-strlen.c
@@ -37,27 +37,24 @@
 
 typedef size_t (*proto_t) (const CHAR *);
 
-/* Naive implementation to verify results.  */
-size_t
-simple_STRLEN (const CHAR *s)
-{
-  const CHAR *p;
-
-  for (p = s; *p; ++p);
-  return p - s;
-}
+IMPL (STRLEN, 1)
 
+/* Also check the generic implementation.  */
+#undef STRLEN
+#undef weak_alias
+#define weak_alias(a, b)
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
 #ifndef WIDE
-size_t
-builtin_strlen (const CHAR *p)
-{
-  return __builtin_strlen (p);
-}
-IMPL (builtin_strlen, 0)
+# define STRLEN __strlen_default
+# include "string/strlen.c"
+IMPL (__strlen_default, 1)
+#else
+# define WCSLEN __wcslen_default
+# include "wcsmbs/wcslen.c"
+IMPL (__wcslen_default, 1)
 #endif
 
-IMPL (STRLEN, 1)
-
 
 static void
 do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 23/31] string: Hook up the default implementation on test-strnlen
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (21 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 22/31] string: Hook up the default implementation on test-strlen Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 24/31] string: Hook up the default implementation on test-strchr Adhemerval Zanella
                   ` (8 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Also remove the SIMPLE_STRNLEN, which is not used anywhere.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strnlen.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/string/test-strnlen.c b/string/test-strnlen.c
index 8b5372c3a1..ef73dd34f4 100644
--- a/string/test-strnlen.c
+++ b/string/test-strnlen.c
@@ -43,17 +43,30 @@
 
 typedef size_t (*proto_t) (const CHAR *, size_t);
 
-IMPL (STRNLEN, 1)
-
-/* Naive implementation to verify results.  */
-size_t
-SIMPLE_STRNLEN (const CHAR *s, size_t maxlen)
-{
-  size_t i;
-
-  for (i = 0; i < maxlen && s[i]; ++i);
-  return i;
-}
+/* Also check the default implementation.  */
+#undef STRNLEN
+#ifndef WIDE
+# define MEMCHR __memchr_default
+# define weak_alias(a, b)
+# define libc_hidden_def(a)
+# define libc_hidden_builtin_def(a)
+# include "string/memchr.c"
+# undef STRNLEN
+# define STRNLEN __strnlen_default
+# define memchr __memchr_default
+# include "string/strnlen.c"
+IMPL (__strnlen_default, 1)
+#else
+# define WMEMCHR __wmemchr_default
+# define weak_alias(a, b)
+# define libc_hidden_def(a)
+# define libc_hidden_weak(a)
+# include "wcsmbs/wmemchr.c"
+# define WCSNLEN __wcsnlen_default
+# define wmemchr __wmemchr_default
+# include "wcsmbs/wcsnlen.c"
+IMPL (__wcsnlen_default, 1)
+#endif
 
 static void
 do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 24/31] string: Hook up the default implementation on test-strchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (22 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 23/31] string: Hook up the default implementation on test-strnlen Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 25/31] string: Hook up the default implementation on test-strcmp Adhemerval Zanella
                   ` (7 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Also remove the simple_STRCHR, which can be easily replaced.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strchr.c | 53 +++++++++++++++++++++++++++++++-------------
 1 file changed, 38 insertions(+), 15 deletions(-)

diff --git a/string/test-strchr.c b/string/test-strchr.c
index 323c844d21..933fc0bbba 100644
--- a/string/test-strchr.c
+++ b/string/test-strchr.c
@@ -35,7 +35,6 @@
 #ifndef WIDE
 # ifdef USE_FOR_STRCHRNUL
 #  define STRCHR strchrnul
-#  define simple_STRCHR simple_STRCHRNUL
 # else
 #  define STRCHR strchr
 # endif /* !USE_FOR_STRCHRNUL */
@@ -50,7 +49,6 @@
 # include <wchar.h>
 # ifdef USE_FOR_STRCHRNUL
 #  define STRCHR wcschrnul
-#  define simple_STRCHR simple_WCSCHRNUL
 # else
 #  define STRCHR wcschr
 # endif /* !USE_FOR_STRCHRNUL */
@@ -72,20 +70,41 @@
 
 typedef CHAR *(*proto_t) (const CHAR *, int);
 
-/* Naive implementation to verify results.  */
-CHAR *
-simple_STRCHR (const CHAR *s, int c)
-{
-  size_t n = STRLEN (s) + 1;
-
-  while (n--)
-    if (*s++ == (CHAR) c)
-      return (CHAR *) s - 1;
-  return NULLRET ((CHAR *) s - 1);
-}
-
 IMPL (STRCHR, 1)
 
+/* Also check the generic implementation.  */
+#undef STRCHR
+#undef weak_alias
+#define weak_alias(a, b)
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef libc_hidden_weak
+#define libc_hidden_weak(a)
+#ifndef WIDE
+# define STRCHRNUL __strchrnul_default
+# include "string/strchrnul.c"
+# ifndef USE_FOR_STRCHRNUL
+#  define STRCHR __strchr_default
+#  include "string/strchr.c"
+#  define STRCHR_DEFAULT STRCHR
+# else
+#  define STRCHR_DEFAULT STRCHRNUL
+# endif
+#else
+# ifndef USE_FOR_STRCHRNUL
+#  define WCSCHR __wcschr_default
+#  include "wcsmbs/wcschr.c"
+#  define STRCHR_DEFAULT WCSCHR
+# else
+#  define WCSCHRNUL __wcschrnul_default
+#  include "wcsmbs/wcschrnul.c"
+#  define STRCHR_DEFAULT WCSCHRNUL
+# endif
+#endif
+IMPL (STRCHR_DEFAULT, 1)
+
 static int
 check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
 {
@@ -219,7 +238,11 @@ check1 (void)
 {
   CHAR s[] __attribute__((aligned(16))) = L ("\xff");
   CHAR c = L ('\xfe');
-  CHAR *exp_result = simple_STRCHR (s, c);
+#ifndef USE_FOR_STRCHRNUL
+  CHAR *exp_result = NULL;
+#else
+  CHAR *exp_result = s + STRLEN (s);
+#endif
 
   FOR_EACH_IMPL (impl, 0)
     check_result (impl, s, c, exp_result);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 25/31] string: Hook up the default implementation on test-strcmp
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (23 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 24/31] string: Hook up the default implementation on test-strchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 26/31] string: Hook up the default implementation on test-strncmp Adhemerval Zanella
                   ` (6 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strcmp.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/string/test-strcmp.c b/string/test-strcmp.c
index ceb0d29c74..fa3161688c 100644
--- a/string/test-strcmp.c
+++ b/string/test-strcmp.c
@@ -101,6 +101,28 @@ typedef int (*proto_t) (const CHAR *, const CHAR *);
 
 IMPL (STRCMP, 1)
 
+/* Also check the default implementation.  */
+#undef STRCMP
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef weak_alias
+#define weak_alias(a, b)
+#undef attribute_hidden
+#define attribute_hidden
+#ifndef WIDE
+# define STRCMP __strcmp_default
+# include "string/strcmp.c"
+# define STRCMP_DEFAULT STRCMP
+#else
+# define WCSCMP __wcscmp_default
+# include "wcsmbs/wcscmp.c"
+# define STRCMP_DEFAULT WCSCMP
+#endif
+IMPL (STRCMP_DEFAULT, 1)
+
+
 static int
 check_result (impl_t *impl,
 	     const CHAR *s1, const CHAR *s2,
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 26/31] string: Hook up the default implementation on test-strncmp
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (24 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 25/31] string: Hook up the default implementation on test-strcmp Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 27/31] string: Hook up the default implementation on test-stpcpy Adhemerval Zanella
                   ` (5 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strncmp.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/string/test-strncmp.c b/string/test-strncmp.c
index fb4c6420b8..35dc496eae 100644
--- a/string/test-strncmp.c
+++ b/string/test-strncmp.c
@@ -90,6 +90,22 @@ typedef int (*proto_t) (const CHAR *, const CHAR *, size_t);
 
 IMPL (STRNCMP, 1)
 
+/* Also check the default implementation.  */
+#undef STRNCMP
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef attribute_hidden
+#define attribute_hidden
+#ifndef WIDE
+# define STRNCMP __strncmp_default
+# include "string/strncmp.c"
+# define STRNCMP_DEFAULT STRNCMP
+#else
+# define WCSNCMP __wcsncmp_default
+# include "wcsmbs/wcsncmp.c"
+# define STRNCMP_DEFAULT WCSNCMP
+#endif
+IMPL (STRNCMP_DEFAULT, 1)
 
 static int
 check_result (impl_t *impl, const CHAR *s1, const CHAR *s2, size_t n,
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 27/31] string: Hook up the default implementation on test-stpcpy
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (25 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 26/31] string: Hook up the default implementation on test-strncmp Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 28/31] string: Hook up the default implementation on test-strcpy Adhemerval Zanella
                   ` (4 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-stpcpy.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/string/test-stpcpy.c b/string/test-stpcpy.c
index c7a416f93a..5d344b6a23 100644
--- a/string/test-stpcpy.c
+++ b/string/test-stpcpy.c
@@ -26,26 +26,36 @@
 #include "test-string.h"
 #ifndef WIDE
 # define CHAR char
-# define SIMPLE_STPCPY simple_stpcpy
 # define STPCPY stpcpy
 #else
 # include <wchar.h>
 # define CHAR wchar_t
-# define SIMPLE_STPCPY simple_wcpcpy
 # define STPCPY wcpcpy
 #endif /* !WIDE */
 
-CHAR *SIMPLE_STPCPY (CHAR *, const CHAR *);
-
-IMPL (SIMPLE_STPCPY, 0)
 IMPL (STPCPY, 1)
 
-CHAR *
-SIMPLE_STPCPY (CHAR *dst, const CHAR *src)
-{
-  while ((*dst++ = *src++) != '\0');
-  return dst - 1;
-}
+/* Also check the generic implementation.  */
+#undef STPCPY
+#undef weak_alias
+#define weak_alias(a, b)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef attribute_hidden
+#define attribute_hidden
+#ifndef WIDE
+# define STPCPY __stpcpy_default
+# include "string/stpcpy.c"
+IMPL (__stpcpy_default, 1)
+#else
+# define __wcslen wcslen
+# define __wmemcpy wmemcpy
+# define WCPCPY __wcpcpy_default
+# include "wcsmbs/wcpcpy.c"
+IMPL (__wcpcpy_default, 1)
+#endif
 
 #undef CHAR
 #include "test-strcpy.c"
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 28/31] string: Hook up the default implementation on test-strcpy
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (26 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 27/31] string: Hook up the default implementation on test-stpcpy Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 29/31] string: Hook up the default implementation on test-memchr Adhemerval Zanella
                   ` (3 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-strcpy.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/string/test-strcpy.c b/string/test-strcpy.c
index 8183c4b7f3..3e04c754d0 100644
--- a/string/test-strcpy.c
+++ b/string/test-strcpy.c
@@ -47,23 +47,37 @@
 # endif
 # include "test-string.h"
 # ifndef WIDE
-#  define SIMPLE_STRCPY simple_strcpy
 #  define STRCPY strcpy
 # else
-#  define SIMPLE_STRCPY simple_wcscpy
 #  define STRCPY wcscpy
 # endif
 
 IMPL (STRCPY, 1)
 
-/* Naive implementation to verify results.  */
-CHAR *
-SIMPLE_STRCPY (CHAR *dst, const CHAR *src)
-{
-  CHAR *ret = dst;
-  while ((*dst++ = *src++) != '\0');
-  return ret;
-}
+/* Also check the generic implementation.  */
+#undef STRCPY
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef weak_alias
+#define weak_alias(a,b)
+#undef attribute_hidden
+#define attribute_hidden
+# ifndef WIDE
+#  define STPCPY __stpcpy_default
+#  include "string/stpcpy.c"
+#  define STRCPY __strcpy_default
+#  define __stpcpy __stpcpy_default
+#  include "string/strcpy.c"
+IMPL (__strcpy_default, 1)
+# else
+#  define __wcslen wcslen
+#  define __wmemcpy wmemcpy
+#  define WCSCPY __wcscpy_default
+#  include "wcsmbs/wcscpy.c"
+IMPL (__wcscpy_default, 1)
+# endif
 #endif
 
 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 29/31] string: Hook up the default implementation on test-memchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (27 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 28/31] string: Hook up the default implementation on test-strcpy Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 30/31] string: Hook up the default implementation on test-memrchr Adhemerval Zanella
                   ` (2 subsequent siblings)
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

And remove SIMPLE_MEMCHR, which is not used anywhere.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-memchr.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/string/test-memchr.c b/string/test-memchr.c
index 862094ef0b..0daecab537 100644
--- a/string/test-memchr.c
+++ b/string/test-memchr.c
@@ -30,7 +30,6 @@
 # define MEMCHR memchr
 # define CHAR char
 # define UCHAR unsigned char
-# define SIMPLE_MEMCHR simple_memchr
 # define BIG_CHAR CHAR_MAX
 # define SMALL_CHAR 127
 #else
@@ -38,7 +37,6 @@
 # define MEMCHR wmemchr
 # define CHAR wchar_t
 # define UCHAR wchar_t
-# define SIMPLE_MEMCHR simple_wmemchr
 # define BIG_CHAR WCHAR_MAX
 # define SMALL_CHAR 1273
 #endif /* WIDE */
@@ -47,15 +45,26 @@ typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
 
 IMPL (MEMCHR, 1)
 
-/* Naive implementation to verify results.  */
-CHAR *
-SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
-{
-  while (n--)
-    if (*s++ == (CHAR) c)
-      return (CHAR *) s - 1;
-  return NULL;
-}
+/* Also check the generic implementation.  */
+#undef MEMCHR
+#undef weak_alias
+#define weak_alias(a, b)
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef libc_hidden_weak
+#define libc_hidden_weak(a)
+#ifndef WIDE
+# define MEMCHR __memchr_default
+# include "string/memchr.c"
+# define MEMCHR_DEFAULT MEMCHR
+#else
+# define WMEMCHR __wmemchr_default
+# include "wcsmbs/wmemchr.c"
+# define MEMCHR_DEFAULT WMEMCHR
+#endif
+IMPL (MEMCHR_DEFAULT, 1)
 
 static void
 do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 30/31] string: Hook up the default implementation on test-memrchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (28 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 29/31] string: Hook up the default implementation on test-memchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-02 18:11 ` [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr Adhemerval Zanella
  2023-02-02 18:17 ` [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella Netto
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 string/test-memrchr.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/string/test-memrchr.c b/string/test-memrchr.c
index a4eac3592e..e4cba1878a 100644
--- a/string/test-memrchr.c
+++ b/string/test-memrchr.c
@@ -24,6 +24,13 @@ typedef char *(*proto_t) (const char *, int, size_t);
 
 IMPL (memrchr, 1)
 
+/* Also check the generic implementation.  */
+#undef weak_alias
+#define weak_alias(a, b)
+#define MEMRCHR __memrchr_default
+#include "string/memrchr.c"
+IMPL (__memrchr_default, 1)
+
 /* Naive implementation to verify results.  */
 char *
 simple_memrchr (const char *s, int c, size_t n)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (29 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 30/31] string: Hook up the default implementation on test-memrchr Adhemerval Zanella
@ 2023-02-02 18:11 ` Adhemerval Zanella
  2023-02-03  1:55   ` Richard Henderson
  2023-02-02 18:17 ` [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella Netto
  31 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella @ 2023-02-02 18:11 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

And remove SIMPLE_STRRCHR, which is not used anywhere.
---
 string/test-strrchr.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/string/test-strrchr.c b/string/test-strrchr.c
index 2cd5de42cd..c979574c92 100644
--- a/string/test-strrchr.c
+++ b/string/test-strrchr.c
@@ -26,14 +26,12 @@
 
 #ifdef WIDE
 # include <wchar.h>
-# define SIMPLE_STRRCHR simple_wcsrchr
 # define STRRCHR wcsrchr
 # define CHAR wchar_t
 # define UCHAR wchar_t
 # define BIG_CHAR WCHAR_MAX
 # define SMALL_CHAR 1273
 #else
-# define SIMPLE_STRRCHR simple_strrchr
 # define STRRCHR strrchr
 # define CHAR char
 # define UCHAR unsigned char
@@ -45,18 +43,30 @@ typedef CHAR *(*proto_t) (const CHAR *, int);
 
 IMPL (STRRCHR, 1)
 
-/* Naive implementation to verify results.  */
-CHAR *
-SIMPLE_STRRCHR (const CHAR *s, int c)
-{
-  const CHAR *ret = NULL;
-
-  for (; *s != '\0'; ++s)
-    if (*s == (CHAR) c)
-      ret = s;
-
-  return (CHAR *) (c == '\0' ? s : ret);
-}
+/* Also check the generic implementation.  */
+#undef STRRCHR
+#undef weak_alias
+#define weak_alias(a, b)
+#undef libc_hidden_builtin_def
+#define libc_hidden_builtin_def(a)
+#undef libc_hidden_def
+#define libc_hidden_def(a)
+#undef libc_hidden_weak
+#define libc_hidden_weak(a)
+#ifndef WIDE
+# define STRLEN __strlen_default
+# include "string/strlen.c"
+# define MEMRCHR __memrchr_default
+# include "string/memrchr.c"
+# define STRRCHR __strrchr_default
+# include "string/strrchr.c"
+# define STRRCHR_DEFAULT __strrchr_default
+#else
+# define WCSRCHR __wcsrchr_default
+# include "wcsmbs/wcsrchr.c"
+# define STRRCHR_DEFAULT __wcsrchr_default
+#endif
+IMPL (STRRCHR_DEFAULT, 1)
 
 static void
 do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
-- 
2.34.1


^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 00/31] Improve generic string routines
  2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
                   ` (30 preceding siblings ...)
  2023-02-02 18:11 ` [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr Adhemerval Zanella
@ 2023-02-02 18:17 ` Adhemerval Zanella Netto
  31 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-02 18:17 UTC (permalink / raw)
  To: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein



On 02/02/23 15:11, Adhemerval Zanella wrote:
> It is done by:
> 
>   1. Parametrizing the internal routines (for instance the find zero
>      in a word) so each architecture can reimplement without the need
>      to reimplement the whole routine.
> 
>   2. Vectorizing more string implementations (for instance strcpy
>      and strcmp).
> 
>   3. Change some implementations to use already possible optimized
>      ones (strnlen and strchr).  It makes new ports to focus on
>      only provide optimized implementation of a hardful symbols
>      (for instance memchr) and make its improvement to be used in
>      a larger set of routines.
> 
> I checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powerpc64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multiarch (it covers both LE and BE
> for 64 and 32 bits). I also checked the string routines on alpha, hppa,
> and sh.
> 
> Changes since v11:
>   * Use index_first_zero_ne strcmp/strncmp, and fixed it on LE.
>   * Added strrchr optimization based on strlen/memrchr.
>   * Reorder the patches so composed optimizations (such as strrchr) as
>     ordered later.

To summarize, only the following patches are missing review:

https://patchwork.sourceware.org/project/glibc/patch/20230202181149.2181553-8-adhemerval.zanella@linaro.org/
https://patchwork.sourceware.org/project/glibc/patch/20230202181149.2181553-9-adhemerval.zanella@linaro.org/
https://patchwork.sourceware.org/project/glibc/patch/20230202181149.2181553-13-adhemerval.zanella@linaro.org/
https://patchwork.sourceware.org/project/glibc/patch/20230202181149.2181553-15-adhemerval.zanella@linaro.org/
https://patchwork.sourceware.org/project/glibc/patch/20230202181149.2181553-32-adhemerval.zanella@linaro.org/

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 03/31] Add string vectorized find and detection functions
  2023-02-02 18:11 ` [PATCH v12 03/31] Add string vectorized find and detection functions Adhemerval Zanella
@ 2023-02-03  0:24   ` Richard Henderson
  2023-02-03 12:39     ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Richard Henderson @ 2023-02-03  0:24 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein

On 2/2/23 08:11, Adhemerval Zanella wrote:
> +/* With similar caveats, identify zero bytes in X1 and bytes that are
> +   not equal between in X1 and X2.  */
> +static __always_inline find_t
> +find_zero_ne_low (op_t x1, op_t x2)
> +{
> +  return (~find_zero_eq_low (x1, x2)) + 1;
> +}

This is no longer used, and suspected buggy.  Duh, I now see why it's buggy -- it's 
inverting both zero comparison and eq comparison -- we wanted to invert only eq.

Let's remove this rather than attempting to fix.  I suspect it'll work out very similar to 
our existing find_zero_ne_all().


> +/* Similarly, but perform the search for byte equality between X1 and X2.  */
> +static __always_inline unsigned int
> +index_first_zero (op_t x1, op_t x2)
> +{
> +  if (__BYTE_ORDER == __LITTLE_ENDIAN)
> +    x1 = find_zero_low (x1, x2);
> +  else
> +    x1 = find_zero_all (x1, x2);
> +  return index_first (x1);
> +}
...
> +/* Similarly, but search for the last zero within X.  */
> +static __always_inline unsigned int
> +index_last_zero (op_t x)
> +{
> +  return index_last (find_zero_all (x));
> +}

Why did this lose the __BYTE_ORDER test?  It should be the inverse of index_first_zero.


> +static __always_inline unsigned int
> +index_last_eq (op_t x1, op_t x2)
> +{
> +  return index_last_zero (x1 ^ x2);
> +}

Similarly.


r~

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 08/31] string: Improve generic strncmp
  2023-02-02 18:11 ` [PATCH v12 08/31] string: Improve generic strncmp Adhemerval Zanella
@ 2023-02-03  0:34   ` Richard Henderson
  2023-02-21  9:28   ` Szabolcs Nagy
  1 sibling, 0 replies; 61+ messages in thread
From: Richard Henderson @ 2023-02-03  0:34 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein

On 2/2/23 08:11, Adhemerval Zanella wrote:
> It follows the strategy:
> 
>     - Align the first input to word boundary using byte operations.
> 
>     - If second input is also word aligned, read a word per time, check
>       for  null (using has_zero), and check final words using byte
>       operation.
> 
>     - If second input is not word aligned, loop by aligning the source,
>       and merge the result of two reads.  Similar to aligned case, check
>       for null with has_zero, and check final words using byte operation.
> 
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
> ---
>   string/strncmp.c | 138 ++++++++++++++++++++++++++++++++++-------------
>   1 file changed, 101 insertions(+), 37 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr
  2023-02-02 18:11 ` [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr Adhemerval Zanella
@ 2023-02-03  1:55   ` Richard Henderson
  0 siblings, 0 replies; 61+ messages in thread
From: Richard Henderson @ 2023-02-03  1:55 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein

On 2/2/23 08:11, Adhemerval Zanella wrote:
> And remove SIMPLE_STRRCHR, which is not used anywhere.
> ---
>   string/test-strrchr.c | 38 ++++++++++++++++++++++++--------------
>   1 file changed, 24 insertions(+), 14 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 03/31] Add string vectorized find and detection functions
  2023-02-03  0:24   ` Richard Henderson
@ 2023-02-03 12:39     ` Adhemerval Zanella Netto
  2023-02-03 13:40       ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-03 12:39 UTC (permalink / raw)
  To: Richard Henderson, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein



On 02/02/23 21:24, Richard Henderson wrote:
> On 2/2/23 08:11, Adhemerval Zanella wrote:
>> +/* With similar caveats, identify zero bytes in X1 and bytes that are
>> +   not equal between in X1 and X2.  */
>> +static __always_inline find_t
>> +find_zero_ne_low (op_t x1, op_t x2)
>> +{
>> +  return (~find_zero_eq_low (x1, x2)) + 1;
>> +}
> 
> This is no longer used, and suspected buggy.  Duh, I now see why it's buggy -- it's inverting both zero comparison and eq comparison -- we wanted to invert only eq.
> 
> Let's remove this rather than attempting to fix.  I suspect it'll work out very similar to our existing find_zero_ne_all().

Ack, we haven't see any issue because it not really use anywhere.

> 
> 
>> +/* Similarly, but perform the search for byte equality between X1 and X2.  */
>> +static __always_inline unsigned int
>> +index_first_zero (op_t x1, op_t x2)
>> +{
>> +  if (__BYTE_ORDER == __LITTLE_ENDIAN)
>> +    x1 = find_zero_low (x1, x2);
>> +  else
>> +    x1 = find_zero_all (x1, x2);
>> +  return index_first (x1);
>> +}
> ...
>> +/* Similarly, but search for the last zero within X.  */
>> +static __always_inline unsigned int
>> +index_last_zero (op_t x)
>> +{
>> +  return index_last (find_zero_all (x));
>> +}
> 
> Why did this lose the __BYTE_ORDER test?  It should be the inverse of index_first_zero.

I think because find_zero_all works for both LE and BE.  But we can optimize it
slight for BE:

static __always_inline unsigned int
index_last_zero (op_t x)
{
  if (__BYTE_ORDER == __LITTLE_ENDIAN)
    x = find_zero_all (x);
  else
    x = find_zero_low (x);
  return index_last (x);
}


> 
> 
>> +static __always_inline unsigned int
>> +index_last_eq (op_t x1, op_t x2)
>> +{
>> +  return index_last_zero (x1 ^ x2);
>> +}

I think it should not be required with index_last_zero using __BYTE_ORDER
test as above.

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 03/31] Add string vectorized find and detection functions
  2023-02-03 12:39     ` Adhemerval Zanella Netto
@ 2023-02-03 13:40       ` Adhemerval Zanella Netto
  2023-02-04  2:54         ` Richard Henderson
  0 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-03 13:40 UTC (permalink / raw)
  To: Richard Henderson, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein



On 03/02/23 09:39, Adhemerval Zanella Netto wrote:
> 
> 
> On 02/02/23 21:24, Richard Henderson wrote:
>> On 2/2/23 08:11, Adhemerval Zanella wrote:
>>> +/* With similar caveats, identify zero bytes in X1 and bytes that are
>>> +   not equal between in X1 and X2.  */
>>> +static __always_inline find_t
>>> +find_zero_ne_low (op_t x1, op_t x2)
>>> +{
>>> +  return (~find_zero_eq_low (x1, x2)) + 1;
>>> +}
>>
>> This is no longer used, and suspected buggy.  Duh, I now see why it's buggy -- it's inverting both zero comparison and eq comparison -- we wanted to invert only eq.
>>
>> Let's remove this rather than attempting to fix.  I suspect it'll work out very similar to our existing find_zero_ne_all().
> 
> Ack, we haven't see any issue because it not really use anywhere.
> 
>>
>>
>>> +/* Similarly, but perform the search for byte equality between X1 and X2.  */
>>> +static __always_inline unsigned int
>>> +index_first_zero (op_t x1, op_t x2)
>>> +{
>>> +  if (__BYTE_ORDER == __LITTLE_ENDIAN)
>>> +    x1 = find_zero_low (x1, x2);
>>> +  else
>>> +    x1 = find_zero_all (x1, x2);
>>> +  return index_first (x1);
>>> +}
>> ...
>>> +/* Similarly, but search for the last zero within X.  */
>>> +static __always_inline unsigned int
>>> +index_last_zero (op_t x)
>>> +{
>>> +  return index_last (find_zero_all (x));
>>> +}
>>
>> Why did this lose the __BYTE_ORDER test?  It should be the inverse of index_first_zero.
> 
> I think because find_zero_all works for both LE and BE.  But we can optimize it
> slight for BE:
> 
> static __always_inline unsigned int
> index_last_zero (op_t x)
> {
>   if (__BYTE_ORDER == __LITTLE_ENDIAN)
>     x = find_zero_all (x);
>   else
>     x = find_zero_low (x);
>   return index_last (x);
> }
> 
> 
>>
>>
>>> +static __always_inline unsigned int
>>> +index_last_eq (op_t x1, op_t x2)
>>> +{
>>> +  return index_last_zero (x1 ^ x2);
>>> +}
> 
> I think it should not be required with index_last_zero using __BYTE_ORDER
> test as above.

I will send only this patch to avoid resend all the thread that is already
reviewed.

---

[PATCH 03/31] Add string vectorized find and detection functions

This patch adds generic string find and detection meant to be used in
generic vectorized string implementation.  The idea is to decompose the
basic string operation so each architecture can reimplement if it
provides any specialized hardware instruction.

The 'string-misc.h' provides miscellaneous functions:

  - extractbyte: extracts the byte from an specific index.
  - repeat_bytes: setup an word by replicate the argument on each byte.

The 'string-fza.h' provides zero byte detection functions:

  - find_zero_low, find_zero_all, find_eq_low, find_eq_all,
    find_zero_eq_low, find_zero_eq_all, and find_zero_ne_all

The 'string-fzb.h' provides boolean zero byte detection functions:

  - has_zero: determine if any byte within a word is zero.
  - has_eq: determine byte equality between two words.
  - has_zero_eq: determine if any byte within a word is zero along with
    byte equality between two words.

The 'string-fzi.h' provides positions for string-fza.h results:

  - index_first: return index of first zero byte within a word.
  - index_last: return index of first byte different between two words.

The 'string-fzc.h' provides a combined version of fza and fzi:

  - index_first_zero_eq: return index of first zero byte within a word or
    first byte different between two words.
  - index_first_zero_ne: return index of first zero byte within a word or
    first byte equal between two words.
  - index_last_zero: return index of last zero byte within a word.
  - index_last_eq: return index of last byte different between two words.

The 'string-shift.h' provides a way to mask off parts of a work based on
some alignmnet (to handle unaligned arguments):

  - shift_find, shift_find_last.

Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
---
 sysdeps/generic/string-fza.h   | 98 ++++++++++++++++++++++++++++++++++
 sysdeps/generic/string-fzb.h   | 49 +++++++++++++++++
 sysdeps/generic/string-fzc.h   | 87 ++++++++++++++++++++++++++++++
 sysdeps/generic/string-fzi.h   | 71 ++++++++++++++++++++++++
 sysdeps/generic/string-misc.h  | 45 ++++++++++++++++
 sysdeps/generic/string-shift.h | 52 ++++++++++++++++++
 6 files changed, 402 insertions(+)
 create mode 100644 sysdeps/generic/string-fza.h
 create mode 100644 sysdeps/generic/string-fzb.h
 create mode 100644 sysdeps/generic/string-fzc.h
 create mode 100644 sysdeps/generic/string-fzi.h
 create mode 100644 sysdeps/generic/string-misc.h
 create mode 100644 sysdeps/generic/string-shift.h

diff --git a/sysdeps/generic/string-fza.h b/sysdeps/generic/string-fza.h
new file mode 100644
index 0000000000..69ee04d750
--- /dev/null
+++ b/sysdeps/generic/string-fza.h
@@ -0,0 +1,98 @@
+/* Basic zero byte detection.  Generic C 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/>.  */
+
+#ifndef _STRING_FZA_H
+#define _STRING_FZA_H 1
+
+#include <string-misc.h>
+#include <string-optype.h>
+
+/* The function return a byte mask.  */
+typedef op_t find_t;
+
+/* This function returns non-zero if any byte in X is zero.
+   More specifically, at least one bit set within the least significant
+   byte that was zero; other bytes within the word are indeterminate.  */
+static __always_inline find_t
+find_zero_low (op_t x)
+{
+  /* This expression comes from
+       https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
+     Subtracting 1 sets 0x80 in a byte that was 0; anding ~x clears
+     0x80 in a byte that was >= 128; anding 0x80 isolates that test bit.  */
+  op_t lsb = repeat_bytes (0x01);
+  op_t msb = repeat_bytes (0x80);
+  return (x - lsb) & ~x & msb;
+}
+
+/* This function returns at least one bit set within every byte of X that
+   is zero.  The result is exact in that, unlike find_zero_low, all bytes
+   are determinate.  This is usually used for finding the index of the
+   most significant byte that was zero.  */
+static __always_inline find_t
+find_zero_all (op_t x)
+{
+  /* For each byte, find not-zero by
+     (0) And 0x7f so that we cannot carry between bytes,
+     (1) Add 0x7f so that non-zero carries into 0x80,
+     (2) Or in the original byte (which might have had 0x80 set).
+     Then invert and mask such that 0x80 is set iff that byte was zero.  */
+  op_t m = repeat_bytes (0x7f);
+  return ~(((x & m) + m) | x | m);
+}
+
+/* With similar caveats, identify bytes that are equal between X1 and X2.  */
+static __always_inline find_t
+find_eq_low (op_t x1, op_t x2)
+{
+  return find_zero_low (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1 ^ x2);
+}
+
+/* With similar caveats, identify zero bytes in X1 and bytes that are
+   equal between in X1 and X2.  */
+static __always_inline find_t
+find_zero_eq_low (op_t x1, op_t x2)
+{
+  return find_zero_low (x1) | find_zero_low (x1 ^ x2);
+}
+
+static __always_inline find_t
+find_zero_eq_all (op_t x1, op_t x2)
+{
+  return find_zero_all (x1) | find_zero_all (x1 ^ x2);
+}
+
+/* With similar caveats, identify zero bytes in X1 and bytes that are
+   not equal between in X1 and X2.  */
+static __always_inline find_t
+find_zero_ne_all (op_t x1, op_t x2)
+{
+  op_t m = repeat_bytes (0x7f);
+  op_t eq = x1 ^ x2;
+  op_t nz1 = ((x1 & m) + m) | x1;
+  op_t ne2 = ((eq & m) + m) | eq;
+  return (ne2 | ~nz1) & ~m;
+}
+
+#endif /* _STRING_FZA_H */
diff --git a/sysdeps/generic/string-fzb.h b/sysdeps/generic/string-fzb.h
new file mode 100644
index 0000000000..42de500d67
--- /dev/null
+++ b/sysdeps/generic/string-fzb.h
@@ -0,0 +1,49 @@
+/* Zero byte detection, boolean.  Generic C 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/>.  */
+
+#ifndef _STRING_FZB_H
+#define _STRING_FZB_H 1
+
+#include <endian.h>
+#include <string-fza.h>
+
+/* Determine if any byte within X is zero.  This is a pure boolean test.  */
+
+static __always_inline _Bool
+has_zero (op_t x)
+{
+  return find_zero_low (x) != 0;
+}
+
+/* Likewise, but for byte equality between X1 and X2.  */
+
+static __always_inline _Bool
+has_eq (op_t x1, op_t x2)
+{
+  return find_eq_low (x1, x2) != 0;
+}
+
+/* Likewise, but for zeros in X1 and equal bytes between X1 and X2.  */
+
+static __always_inline _Bool
+has_zero_eq (op_t x1, op_t x2)
+{
+  return find_zero_eq_low (x1, x2);
+}
+
+#endif /* _STRING_FZB_H */
diff --git a/sysdeps/generic/string-fzc.h b/sysdeps/generic/string-fzc.h
new file mode 100644
index 0000000000..ae2f9640ab
--- /dev/null
+++ b/sysdeps/generic/string-fzc.h
@@ -0,0 +1,87 @@
+/* Zero byte detection; indexes.  Generic C 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/>.  */
+
+#ifndef _STRING_FZC_H
+#define _STRING_FZC_H 1
+
+#include <endian.h>
+#include <string-fza.h>
+#include <string-fzi.h>
+
+
+/* Given a word X that is known to contain a zero byte, return the index of
+   the first such within the word in memory order.  */
+static __always_inline unsigned int
+index_first_zero (op_t x)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x = find_zero_low (x);
+  else
+    x = find_zero_all (x);
+  return index_first (x);
+}
+
+/* Similarly, but perform the search for byte equality between X1 and X2.  */
+static __always_inline unsigned int
+index_first_eq (op_t x1, op_t x2)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x1 = find_eq_low (x1, x2);
+  else
+    x1 = find_eq_all (x1, x2);
+  return index_first (x1);
+}
+
+/* Similarly, but perform the search for zero within X1 or equality between
+   X1 and X2.  */
+static __always_inline unsigned int
+index_first_zero_eq (op_t x1, op_t x2)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x1 = find_zero_eq_low (x1, x2);
+  else
+    x1 = find_zero_eq_all (x1, x2);
+  return index_first (x1);
+}
+
+/* Similarly, but perform the search for zero within X1 or inequality between
+   X1 and X2.  */
+static __always_inline unsigned int
+index_first_zero_ne (op_t x1, op_t x2)
+{
+  return index_first (find_zero_ne_all (x1, x2));
+}
+
+/* Similarly, but search for the last zero within X.  */
+static __always_inline unsigned int
+index_last_zero (op_t x)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    x = find_zero_all (x);
+  else
+    x = find_zero_low (x);
+  return index_last (x);
+}
+
+static __always_inline unsigned int
+index_last_eq (op_t x1, op_t x2)
+{
+  return index_last_zero (x1 ^ x2);
+}
+
+#endif /* STRING_FZC_H */
diff --git a/sysdeps/generic/string-fzi.h b/sysdeps/generic/string-fzi.h
new file mode 100644
index 0000000000..2deecefc23
--- /dev/null
+++ b/sysdeps/generic/string-fzi.h
@@ -0,0 +1,71 @@
+/* Zero byte detection; indexes.  Generic C 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/>.  */
+
+#ifndef _STRING_FZI_H
+#define _STRING_FZI_H 1
+
+#include <limits.h>
+#include <endian.h>
+#include <string-fza.h>
+
+static __always_inline int
+clz (find_t c)
+{
+  if (sizeof (find_t) == sizeof (unsigned long))
+    return __builtin_clzl (c);
+  else
+    return __builtin_clzll (c);
+}
+
+static __always_inline int
+ctz (find_t c)
+{
+  if (sizeof (find_t) == sizeof (unsigned long))
+    return __builtin_ctzl (c);
+  else
+    return __builtin_ctzll (c);
+}
+
+/* A subroutine for the index_zero functions.  Given a test word C, return
+   the (memory order) index of the first byte (in memory order) that is
+   non-zero.  */
+static __always_inline unsigned int
+index_first (find_t c)
+{
+  int r;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    r = ctz (c);
+  else
+    r = clz (c);
+  return r / CHAR_BIT;
+}
+
+/* Similarly, but return the (memory order) index of the last byte that is
+   non-zero.  */
+static __always_inline unsigned int
+index_last (find_t c)
+{
+  int r;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    r = clz (c);
+  else
+    r = ctz (c);
+  return sizeof (find_t) - 1 - (r / CHAR_BIT);
+}
+
+#endif /* STRING_FZI_H */
diff --git a/sysdeps/generic/string-misc.h b/sysdeps/generic/string-misc.h
new file mode 100644
index 0000000000..a119646957
--- /dev/null
+++ b/sysdeps/generic/string-misc.h
@@ -0,0 +1,45 @@
+/* Miscellaneous functions used in string implementations.  Generic C 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/>.  */
+
+#ifndef _STRING_MISC_H
+#define _STRING_MISC_H 1
+
+#include <limits.h>
+#include <endian.h>
+#include <string-optype.h>
+
+/* Extract the byte at index IDX from word X, with index 0 being the
+   least significant byte.  */
+static __always_inline unsigned char
+extractbyte (op_t x, unsigned int idx)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return x >> (idx * CHAR_BIT);
+  else
+    return x >> (sizeof (x) - 1 - idx) * CHAR_BIT;
+}
+
+/* Setup an word with each byte being c_in.  For instance, on a 64 bits
+   machine with input as 0xce the functions returns 0xcececececececece.  */
+static __always_inline op_t
+repeat_bytes (unsigned char c_in)
+{
+  return ((op_t)-1 / 0xff) * c_in;
+}
+
+#endif /* _STRING_MISC_H */
diff --git a/sysdeps/generic/string-shift.h b/sysdeps/generic/string-shift.h
new file mode 100644
index 0000000000..9491e8b580
--- /dev/null
+++ b/sysdeps/generic/string-shift.h
@@ -0,0 +1,52 @@
+/* Shift unaligned word read  Generic C 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/>.  */
+
+#ifndef _STRING_SHIFT_H
+#define _STRING_SHIFT_H 1
+
+#include <endian.h>
+#include <limits.h>
+#include <stdint.h>
+#include <string-fza.h>
+
+/* Return the mask WORD shifted based on S_INT address value, to ignore
+   values not presented in the aligned word read.  */
+static __always_inline find_t
+shift_find (find_t word, uintptr_t s)
+{
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return word >> (CHAR_BIT * (s % sizeof (op_t)));
+  else
+    return word << (CHAR_BIT * (s % sizeof (op_t)));
+}
+
+/* Mask off the bits defined for the S alignment value, or return WORD if
+   S is 0.  */
+static __always_inline find_t
+shift_find_last (find_t word, uintptr_t s)
+{
+  s = s % sizeof (op_t);
+  if (s == 0)
+    return word;
+  if (__BYTE_ORDER == __LITTLE_ENDIAN)
+    return word & ~(((op_t)-1) << (s * CHAR_BIT));
+  else
+    return word & ~(((op_t)-1) >> (s * CHAR_BIT));
+}
+
+#endif /* _STRING_SHIFT_H */
-- 
2.34.1

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h
  2023-02-02 18:11 ` [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h Adhemerval Zanella
@ 2023-02-03 19:47   ` Noah Goldstein
  2023-02-06 16:14     ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 19:47 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> It uses the bitmanip extension to optimize index_fist and index_last
> with clz/ctz (using generic implementation that routes to compiler
> builtin) and orc.b to check null bytes.
>
> Checked the string test on riscv64 user mode.
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  sysdeps/riscv/string-fza.h | 70 ++++++++++++++++++++++++++++++++++
>  sysdeps/riscv/string-fzi.h | 77 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 147 insertions(+)
>  create mode 100644 sysdeps/riscv/string-fza.h
>  create mode 100644 sysdeps/riscv/string-fzi.h
>
> diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
> new file mode 100644
> index 0000000000..9c7a6efba2
> --- /dev/null
> +++ b/sysdeps/riscv/string-fza.h
> @@ -0,0 +1,70 @@
> +/* Zero byte detection; basics.  RISCV 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/>.  */
> +
> +#ifndef _RISCV_STRING_FZA_H
> +#define _RISCV_STRING_FZA_H 1
> +
> +#ifdef __riscv_zbb
> +/* With bitmap extension we can use orc.b to find all zero bytes.  */
> +# include <string-misc.h>
> +# include <string-optype.h>
> +
> +/* The functions return a byte mask.  */
> +typedef op_t find_t;
> +
> +/* This function returns 0xff for each byte that is zero in X.  */
> +static __always_inline find_t
> +find_zero_all (op_t x)
> +{
> +  find_t r;
> +  asm ("orc.b %0, %1" : "=r" (r) : "r" (x));
> +  return ~r;
> +}
> +
> +/* This function returns 0xff for each byte that is equal between X1 and
> +   X2.  */
> +static __always_inline find_t
> +find_eq_all (op_t x1, op_t x2)
> +{
> +  return find_zero_all (x1 ^ x2);
> +}
> +
> +/* Identify zero bytes in X1 or equality between X1 and X2.  */
> +static __always_inline find_t
> +find_zero_eq_all (op_t x1, op_t x2)
> +{
> +  return find_zero_all (x1) | find_eq_all (x1, x2);
> +}
> +
> +/* Identify zero bytes in X1 or inequality between X1 and X2.  */
> +static __always_inline find_t
> +find_zero_ne_all (op_t x1, op_t x2)
> +{
> +  return find_zero_all (x1) | ~find_eq_all (x1, x2);
> +}
> +
> +/* Define the "inexact" versions in terms of the exact versions.  */
> +# define find_zero_low         find_zero_all
> +# define find_eq_low           find_eq_all
> +# define find_zero_eq_low      find_zero_eq_all
> +# define find_zero_ne_low      find_zero_ne_all
> +#else
> +#include <sysdeps/generic/string-fza.h>
> +#endif
> +
> +#endif /* _RISCV_STRING_FZA_H  */
> diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
> new file mode 100644
> index 0000000000..3cde113afb
> --- /dev/null
> +++ b/sysdeps/riscv/string-fzi.h
> @@ -0,0 +1,77 @@
> +/* Zero byte detection; indexes.  RISCV 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/>.  */
> +
> +#ifndef _STRING_RISCV_FZI_H
> +#define _STRING_RISCV_FZI_H 1
> +
> +#ifdef __riscv_zbb
> +# include <sysdeps/generic/string-fzi.h>
> +#else
> +/* Without bitmap clz/ctz extensions, it is faster to direct test the bits
> +   instead of calling compiler auxiliary functions.  */
> +# include <string-optype.h>
> +
> +static __always_inline unsigned int
> +index_first (find_t c)
> +{
> +  if (c & 0x80U)
> +    return 0;
> +  if (c & 0x8000U)
> +    return 1;
> +  if (c & 0x800000U)
> +    return 2;
> +
> +  if (sizeof (op_t) == 4)
> +    return 3;
> +
> +  if (c & 0x80000000U)
> +    return 3;
> +  if (c & 0x8000000000UL)
> +    return 4;
> +  if (c & 0x800000000000UL)
> +    return 5;
> +  if (c & 0x80000000000000UL)
> +    return 6;
> +  return 7;
> +}
> +
> +static __always_inline unsigned int
> +index_last (find_t c)
> +{
> +  if (sizeof (op_t) == 8)
> +    {
> +      if (c & 0x8000000000000000UL)
> +       return 7;
> +      if (c & 0x80000000000000UL)
> +       return 6;
> +      if (c & 0x800000000000UL)
> +       return 5;
> +      if (c & 0x8000000000UL)
> +       return 4;
> +    }
> +  if (c & 0x80000000U)
> +    return 3;
> +  if (c & 0x800000U)
> +    return 2;
> +  if (c & 0x8000U)
> +    return 1;
> +  return 0;
> +}
> +#endif
> +
> +#endif /* STRING_FZI_H */
> --
> 2.34.1
>

Has WS error:
```
Applying: riscv: Add string-fza.h and string-fzi.h
.git/rebase-apply/patch:115: trailing whitespace.
   instead of calling compiler auxiliary functions.  */
warning: 1 line adds whitespace errors.
```

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 04/31] string: Improve generic strlen
  2023-02-02 18:11 ` [PATCH v12 04/31] string: Improve generic strlen Adhemerval Zanella
@ 2023-02-03 23:23   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:23 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> New algorithm read the first aligned address and mask off the
> unwanted bytes (this strategy is similar to arch-specific
> implementations used on powerpc, sparc, and sh).
>
> The loop now read word-aligned address and check using the has_zero
> macro.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powercp64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
> Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
> ---
>  string/strlen.c         | 92 ++++++++++-------------------------------
>  sysdeps/s390/strlen-c.c | 10 +++--
>  2 files changed, 28 insertions(+), 74 deletions(-)
>
> diff --git a/string/strlen.c b/string/strlen.c
> index ee1aae0fff..5a4424f9a5 100644
> --- a/string/strlen.c
> +++ b/string/strlen.c
> @@ -15,86 +15,38 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>
> +#include <libc-pointer-arith.h>
> +#include <string-fzb.h>
> +#include <string-fzc.h>
> +#include <string-fzi.h>
> +#include <string-shift.h>
>  #include <string.h>
> -#include <stdlib.h>
>
> -#undef strlen
> -
> -#ifndef STRLEN
> -# define STRLEN strlen
> +#ifdef STRLEN
> +# define __strlen STRLEN
>  #endif
>
>  /* Return the length of the null-terminated string STR.  Scan for
>     the null terminator quickly by testing four bytes at a time.  */
>  size_t
> -STRLEN (const char *str)
> +__strlen (const char *str)
>  {
> -  const char *char_ptr;
> -  const unsigned long int *longword_ptr;
> -  unsigned long int longword, himagic, lomagic;
> -
> -  /* Handle the first few characters by reading one character at a time.
> -     Do this until CHAR_PTR is aligned on a longword boundary.  */
> -  for (char_ptr = str; ((unsigned long int) char_ptr
> -                       & (sizeof (longword) - 1)) != 0;
> -       ++char_ptr)
> -    if (*char_ptr == '\0')
> -      return char_ptr - str;
> -
> -  /* All these elucidatory comments refer to 4-byte longwords,
> -     but the theory applies equally well to 8-byte longwords.  */
> -
> -  longword_ptr = (unsigned long int *) char_ptr;
> +  /* Align pointer to sizeof op_t.  */
> +  const uintptr_t s_int = (uintptr_t) str;
> +  const op_t *word_ptr = (const op_t*) PTR_ALIGN_DOWN (str, sizeof (op_t));
>
> -  /* Computing (longword - lomagic) sets the high bit of any corresponding
> -     byte that is either zero or greater than 0x80.  The latter case can be
> -     filtered out by computing (~longword & himagic).  The final result
> -     will always be non-zero if one of the bytes of longword is zero.  */
> -  himagic = 0x80808080L;
> -  lomagic = 0x01010101L;
> -  if (sizeof (longword) > 4)
> -    {
> -      /* 64-bit version of the magic.  */
> -      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
> -      himagic = ((himagic << 16) << 16) | himagic;
> -      lomagic = ((lomagic << 16) << 16) | lomagic;
> -    }
> -  if (sizeof (longword) > 8)
> -    abort ();
> +  op_t word = *word_ptr;
> +  find_t mask = shift_find (find_zero_all (word), s_int);
> +  if (mask != 0)
> +    return index_first (mask);
>
> -  /* Instead of the traditional loop which tests each character,
> -     we will test a longword at a time.  The tricky part is testing
> -     if *any of the four* bytes in the longword in question are zero.  */
> -  for (;;)
> -    {
> -      longword = *longword_ptr++;
> +  do
> +    word = *++word_ptr;
> +  while (! has_zero (word));
>
> -      if (((longword - lomagic) & ~longword & himagic) != 0)
> -       {
> -         /* Which of the bytes was the zero?  */
> -
> -         const char *cp = (const char *) (longword_ptr - 1);
> -
> -         if (cp[0] == 0)
> -           return cp - str;
> -         if (cp[1] == 0)
> -           return cp - str + 1;
> -         if (cp[2] == 0)
> -           return cp - str + 2;
> -         if (cp[3] == 0)
> -           return cp - str + 3;
> -         if (sizeof (longword) > 4)
> -           {
> -             if (cp[4] == 0)
> -               return cp - str + 4;
> -             if (cp[5] == 0)
> -               return cp - str + 5;
> -             if (cp[6] == 0)
> -               return cp - str + 6;
> -             if (cp[7] == 0)
> -               return cp - str + 7;
> -           }
> -       }
> -    }
> +  return ((const char *) word_ptr) + index_first_zero (word) - str;
>  }
> +#ifndef STRLEN
> +weak_alias (__strlen, strlen)
>  libc_hidden_builtin_def (strlen)
> +#endif
> diff --git a/sysdeps/s390/strlen-c.c b/sysdeps/s390/strlen-c.c
> index b829ef2452..0a33a6f8e5 100644
> --- a/sysdeps/s390/strlen-c.c
> +++ b/sysdeps/s390/strlen-c.c
> @@ -21,12 +21,14 @@
>  #if HAVE_STRLEN_C
>  # if HAVE_STRLEN_IFUNC
>  #  define STRLEN STRLEN_C
> +# endif
> +
> +# include <string/strlen.c>
> +
> +# if HAVE_STRLEN_IFUNC
>  #  if defined SHARED && IS_IN (libc)
> -#   undef libc_hidden_builtin_def
> -#   define libc_hidden_builtin_def(name)               \
> -  __hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c);
> +__hidden_ver1 (__strlen_c, __GI_strlen, __strlen_c);
>  #  endif
>  # endif
>
> -# include <string/strlen.c>
>  #endif
> --
> 2.34.1
>

LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 05/31] string: Improve generic strchrnul
  2023-02-02 18:11 ` [PATCH v12 05/31] string: Improve generic strchrnul Adhemerval Zanella
@ 2023-02-03 23:23   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:23 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> New algorithm read the first aligned address and mask off the unwanted
> bytes (this strategy is similar to arch-specific  implementations used
> on powerpc, sparc, and sh).
>
> The loop now read word-aligned address and check using the has_zero_eq
> function.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
> Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
> ---
>  string/strchrnul.c                            | 155 +++---------------
>  .../power4/multiarch/strchrnul-ppc32.c        |   4 -
>  sysdeps/s390/strchrnul-c.c                    |   2 -
>  3 files changed, 22 insertions(+), 139 deletions(-)
>
> diff --git a/string/strchrnul.c b/string/strchrnul.c
> index fa2db4b417..e7887fa285 100644
> --- a/string/strchrnul.c
> +++ b/string/strchrnul.c
> @@ -1,10 +1,5 @@
>  /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
>     This file is part of the GNU C Library.
> -   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
> -   with help from Dan Sahlin (dan@sics.se) and
> -   bug fix and commentary by Jim Blandy (jimb@ai.mit.edu);
> -   adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
> -   and implemented by Roland McGrath (roland@ai.mit.edu).
>
>     The GNU C Library is free software; you can redistribute it and/or
>     modify it under the terms of the GNU Lesser General Public
> @@ -20,147 +15,41 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>
> +#include <libc-pointer-arith.h>
> +#include <string-fzb.h>
> +#include <string-fzc.h>
> +#include <string-fzi.h>
> +#include <string-shift.h>
>  #include <string.h>
> -#include <memcopy.h>
> -#include <stdlib.h>
>
>  #undef __strchrnul
>  #undef strchrnul
>
> -#ifndef STRCHRNUL
> -# define STRCHRNUL __strchrnul
> +#ifdef STRCHRNUL
> +# define __strchrnul STRCHRNUL
>  #endif
>
>  /* Find the first occurrence of C in S or the final NUL byte.  */
>  char *
> -STRCHRNUL (const char *s, int c_in)
> +__strchrnul (const char *str, int c_in)
>  {
> -  const unsigned char *char_ptr;
> -  const unsigned long int *longword_ptr;
> -  unsigned long int longword, magic_bits, charmask;
> -  unsigned char c;
> -
> -  c = (unsigned char) c_in;
> -
> -  /* Handle the first few characters by reading one character at a time.
> -     Do this until CHAR_PTR is aligned on a longword boundary.  */
> -  for (char_ptr = (const unsigned char *) s;
> -       ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0;
> -       ++char_ptr)
> -    if (*char_ptr == c || *char_ptr == '\0')
> -      return (void *) char_ptr;
> -
> -  /* All these elucidatory comments refer to 4-byte longwords,
> -     but the theory applies equally well to 8-byte longwords.  */
> -
> -  longword_ptr = (unsigned long int *) char_ptr;
> -
> -  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
> -     the "holes."  Note that there is a hole just to the left of
> -     each byte, with an extra at the end:
> -
> -     bits:  01111110 11111110 11111110 11111111
> -     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
> -
> -     The 1-bits make sure that carries propagate to the next 0-bit.
> -     The 0-bits provide holes for carries to fall into.  */
> -  magic_bits = -1;
> -  magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
> -
> -  /* Set up a longword, each of whose bytes is C.  */
> -  charmask = c | (c << 8);
> -  charmask |= charmask << 16;
> -  if (sizeof (longword) > 4)
> -    /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
> -    charmask |= (charmask << 16) << 16;
> -  if (sizeof (longword) > 8)
> -    abort ();
> -
> -  /* Instead of the traditional loop which tests each character,
> -     we will test a longword at a time.  The tricky part is testing
> -     if *any of the four* bytes in the longword in question are zero.  */
> -  for (;;)
> -    {
> -      /* We tentatively exit the loop if adding MAGIC_BITS to
> -        LONGWORD fails to change any of the hole bits of LONGWORD.
> -
> -        1) Is this safe?  Will it catch all the zero bytes?
> -        Suppose there is a byte with all zeros.  Any carry bits
> -        propagating from its left will fall into the hole at its
> -        least significant bit and stop.  Since there will be no
> -        carry from its most significant bit, the LSB of the
> -        byte to the left will be unchanged, and the zero will be
> -        detected.
> +  /* Align pointer to sizeof op_t.  */
> +  uintptr_t s_int = (uintptr_t) str;
> +  const op_t *word_ptr = (const op_t *) PTR_ALIGN_DOWN (str, sizeof (op_t));
>
> -        2) Is this worthwhile?  Will it ignore everything except
> -        zero bytes?  Suppose every byte of LONGWORD has a bit set
> -        somewhere.  There will be a carry into bit 8.  If bit 8
> -        is set, this will carry into bit 16.  If bit 8 is clear,
> -        one of bits 9-15 must be set, so there will be a carry
> -        into bit 16.  Similarly, there will be a carry into bit
> -        24.  If one of bits 24-30 is set, there will be a carry
> -        into bit 31, so all of the hole bits will be changed.
> +  op_t repeated_c = repeat_bytes (c_in);
>
> -        The one misfire occurs when bits 24-30 are clear and bit
> -        31 is set; in this case, the hole at bit 31 is not
> -        changed.  If we had access to the processor carry flag,
> -        we could close this loophole by putting the fourth hole
> -        at bit 32!
> +  op_t word = *word_ptr;
> +  find_t mask = shift_find (find_zero_eq_all (word, repeated_c), s_int);
> +  if (mask != 0)
> +    return (char *) str + index_first (mask);
>
> -        So it ignores everything except 128's, when they're aligned
> -        properly.
> +  do
> +    word = *++word_ptr;
> +  while (! has_zero_eq (word, repeated_c));
>
> -        3) But wait!  Aren't we looking for C as well as zero?
> -        Good point.  So what we do is XOR LONGWORD with a longword,
> -        each of whose bytes is C.  This turns each byte that is C
> -        into a zero.  */
> -
> -      longword = *longword_ptr++;
> -
> -      /* Add MAGIC_BITS to LONGWORD.  */
> -      if ((((longword + magic_bits)
> -
> -           /* Set those bits that were unchanged by the addition.  */
> -           ^ ~longword)
> -
> -          /* Look at only the hole bits.  If any of the hole bits
> -             are unchanged, most likely one of the bytes was a
> -             zero.  */
> -          & ~magic_bits) != 0
> -
> -         /* That caught zeroes.  Now test for C.  */
> -         || ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
> -             & ~magic_bits) != 0)
> -       {
> -         /* Which of the bytes was C or zero?
> -            If none of them were, it was a misfire; continue the search.  */
> -
> -         const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
> -
> -         if (*cp == c || *cp == '\0')
> -           return (char *) cp;
> -         if (*++cp == c || *cp == '\0')
> -           return (char *) cp;
> -         if (*++cp == c || *cp == '\0')
> -           return (char *) cp;
> -         if (*++cp == c || *cp == '\0')
> -           return (char *) cp;
> -         if (sizeof (longword) > 4)
> -           {
> -             if (*++cp == c || *cp == '\0')
> -               return (char *) cp;
> -             if (*++cp == c || *cp == '\0')
> -               return (char *) cp;
> -             if (*++cp == c || *cp == '\0')
> -               return (char *) cp;
> -             if (*++cp == c || *cp == '\0')
> -               return (char *) cp;
> -           }
> -       }
> -    }
> -
> -  /* This should never happen.  */
> -  return NULL;
> +  return (char *) word_ptr + index_first_zero_eq (word, repeated_c);
>  }
> -
> +#ifndef STRCHRNUL
>  weak_alias (__strchrnul, strchrnul)
> +#endif
> diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
> index 88ce5dfffa..da03ac7c04 100644
> --- a/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
> +++ b/sysdeps/powerpc/powerpc32/power4/multiarch/strchrnul-ppc32.c
> @@ -19,10 +19,6 @@
>  #include <string.h>
>
>  #define STRCHRNUL  __strchrnul_ppc
> -
> -#undef weak_alias
> -#define weak_alias(a,b )
> -
>  extern __typeof (strchrnul) __strchrnul_ppc attribute_hidden;
>
>  #include <string/strchrnul.c>
> diff --git a/sysdeps/s390/strchrnul-c.c b/sysdeps/s390/strchrnul-c.c
> index e1248d1dbf..ff6aa38d4f 100644
> --- a/sysdeps/s390/strchrnul-c.c
> +++ b/sysdeps/s390/strchrnul-c.c
> @@ -22,8 +22,6 @@
>  # if HAVE_STRCHRNUL_IFUNC
>  #  define STRCHRNUL STRCHRNUL_C
>  #  define __strchrnul STRCHRNUL
> -#  undef weak_alias
> -#  define weak_alias(name, alias)
>  # endif
>
>  # include <string/strchrnul.c>
> --
> 2.34.1
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 06/31] string: Improve generic strchr
  2023-02-02 18:11 ` [PATCH v12 06/31] string: Improve generic strchr Adhemerval Zanella
@ 2023-02-03 23:24   ` Noah Goldstein
  2023-02-04  2:58   ` Richard Henderson
  1 sibling, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:24 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> New algorithm now calls strchrnul.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powerpc64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  string/strchr.c         | 164 ++--------------------------------------
>  sysdeps/s390/strchr-c.c |  11 +--
>  2 files changed, 14 insertions(+), 161 deletions(-)
>
> diff --git a/string/strchr.c b/string/strchr.c
> index 1572b8b42e..90ae0b69fc 100644
> --- a/string/strchr.c
> +++ b/string/strchr.c
> @@ -1,10 +1,5 @@
>  /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
>     This file is part of the GNU C Library.
> -   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
> -   with help from Dan Sahlin (dan@sics.se) and
> -   bug fix and commentary by Jim Blandy (jimb@ai.mit.edu);
> -   adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
> -   and implemented by Roland McGrath (roland@ai.mit.edu).
>
>     The GNU C Library is free software; you can redistribute it and/or
>     modify it under the terms of the GNU Lesser General Public
> @@ -21,165 +16,22 @@
>     <https://www.gnu.org/licenses/>.  */
>
>  #include <string.h>
> -#include <stdlib.h>
>
>  #undef strchr
> +#undef index
>
> -#ifndef STRCHR
> -# define STRCHR strchr
> +#ifdef STRCHR
> +# define strchr STRCHR
>  #endif
>
>  /* Find the first occurrence of C in S.  */
>  char *
> -STRCHR (const char *s, int c_in)
> +strchr (const char *s, int c_in)
>  {
> -  const unsigned char *char_ptr;
> -  const unsigned long int *longword_ptr;
> -  unsigned long int longword, magic_bits, charmask;
> -  unsigned char c;
> -
> -  c = (unsigned char) c_in;
> -
> -  /* Handle the first few characters by reading one character at a time.
> -     Do this until CHAR_PTR is aligned on a longword boundary.  */
> -  for (char_ptr = (const unsigned char *) s;
> -       ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0;
> -       ++char_ptr)
> -    if (*char_ptr == c)
> -      return (void *) char_ptr;
> -    else if (*char_ptr == '\0')
> -      return NULL;
> -
> -  /* All these elucidatory comments refer to 4-byte longwords,
> -     but the theory applies equally well to 8-byte longwords.  */
> -
> -  longword_ptr = (unsigned long int *) char_ptr;
> -
> -  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
> -     the "holes."  Note that there is a hole just to the left of
> -     each byte, with an extra at the end:
> -
> -     bits:  01111110 11111110 11111110 11111111
> -     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
> -
> -     The 1-bits make sure that carries propagate to the next 0-bit.
> -     The 0-bits provide holes for carries to fall into.  */
> -  magic_bits = -1;
> -  magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1;
> -
> -  /* Set up a longword, each of whose bytes is C.  */
> -  charmask = c | (c << 8);
> -  charmask |= charmask << 16;
> -  if (sizeof (longword) > 4)
> -    /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
> -    charmask |= (charmask << 16) << 16;
> -  if (sizeof (longword) > 8)
> -    abort ();
> -
> -  /* Instead of the traditional loop which tests each character,
> -     we will test a longword at a time.  The tricky part is testing
> -     if *any of the four* bytes in the longword in question are zero.  */
> -  for (;;)
> -    {
> -      /* We tentatively exit the loop if adding MAGIC_BITS to
> -        LONGWORD fails to change any of the hole bits of LONGWORD.
> -
> -        1) Is this safe?  Will it catch all the zero bytes?
> -        Suppose there is a byte with all zeros.  Any carry bits
> -        propagating from its left will fall into the hole at its
> -        least significant bit and stop.  Since there will be no
> -        carry from its most significant bit, the LSB of the
> -        byte to the left will be unchanged, and the zero will be
> -        detected.
> -
> -        2) Is this worthwhile?  Will it ignore everything except
> -        zero bytes?  Suppose every byte of LONGWORD has a bit set
> -        somewhere.  There will be a carry into bit 8.  If bit 8
> -        is set, this will carry into bit 16.  If bit 8 is clear,
> -        one of bits 9-15 must be set, so there will be a carry
> -        into bit 16.  Similarly, there will be a carry into bit
> -        24.  If one of bits 24-30 is set, there will be a carry
> -        into bit 31, so all of the hole bits will be changed.
> -
> -        The one misfire occurs when bits 24-30 are clear and bit
> -        31 is set; in this case, the hole at bit 31 is not
> -        changed.  If we had access to the processor carry flag,
> -        we could close this loophole by putting the fourth hole
> -        at bit 32!
> -
> -        So it ignores everything except 128's, when they're aligned
> -        properly.
> -
> -        3) But wait!  Aren't we looking for C as well as zero?
> -        Good point.  So what we do is XOR LONGWORD with a longword,
> -        each of whose bytes is C.  This turns each byte that is C
> -        into a zero.  */
> -
> -      longword = *longword_ptr++;
> -
> -      /* Add MAGIC_BITS to LONGWORD.  */
> -      if ((((longword + magic_bits)
> -
> -           /* Set those bits that were unchanged by the addition.  */
> -           ^ ~longword)
> -
> -          /* Look at only the hole bits.  If any of the hole bits
> -             are unchanged, most likely one of the bytes was a
> -             zero.  */
> -          & ~magic_bits) != 0
> -
> -         /* That caught zeroes.  Now test for C.  */
> -         || ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
> -             & ~magic_bits) != 0)
> -       {
> -         /* Which of the bytes was C or zero?
> -            If none of them were, it was a misfire; continue the search.  */
> -
> -         const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
> -
> -         if (*cp == c)
> -           return (char *) cp;
> -         else if (*cp == '\0')
> -           return NULL;
> -         if (*++cp == c)
> -           return (char *) cp;
> -         else if (*cp == '\0')
> -           return NULL;
> -         if (*++cp == c)
> -           return (char *) cp;
> -         else if (*cp == '\0')
> -           return NULL;
> -         if (*++cp == c)
> -           return (char *) cp;
> -         else if (*cp == '\0')
> -           return NULL;
> -         if (sizeof (longword) > 4)
> -           {
> -             if (*++cp == c)
> -               return (char *) cp;
> -             else if (*cp == '\0')
> -               return NULL;
> -             if (*++cp == c)
> -               return (char *) cp;
> -             else if (*cp == '\0')
> -               return NULL;
> -             if (*++cp == c)
> -               return (char *) cp;
> -             else if (*cp == '\0')
> -               return NULL;
> -             if (*++cp == c)
> -               return (char *) cp;
> -             else if (*cp == '\0')
> -               return NULL;
> -           }
> -       }
> -    }
> -
> -  return NULL;
> +  char *r = __strchrnul (s, c_in);
> +  return (*(unsigned char *)r == (unsigned char)c_in) ? r : NULL;
>  }
> -
> -#ifdef weak_alias
> -# undef index
> +#ifndef STRCHR
>  weak_alias (strchr, index)
> -#endif
>  libc_hidden_builtin_def (strchr)
> +#endif
> diff --git a/sysdeps/s390/strchr-c.c b/sysdeps/s390/strchr-c.c
> index c00f2cceea..90822ae0f4 100644
> --- a/sysdeps/s390/strchr-c.c
> +++ b/sysdeps/s390/strchr-c.c
> @@ -21,13 +21,14 @@
>  #if HAVE_STRCHR_C
>  # if HAVE_STRCHR_IFUNC
>  #  define STRCHR STRCHR_C
> -#  undef weak_alias
> +# endif
> +
> +# include <string/strchr.c>
> +
> +# if HAVE_STRCHR_IFUNC
>  #  if defined SHARED && IS_IN (libc)
> -#   undef libc_hidden_builtin_def
> -#   define libc_hidden_builtin_def(name)                       \
> -     __hidden_ver1 (__strchr_c, __GI_strchr, __strchr_c);
> +__hidden_ver1 (__strchr_c, __GI_strchr, __strchr_c);
>  #  endif
>  # endif
>
> -# include <string/strchr.c>
>  #endif
> --
> 2.34.1
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 07/31] string: Improve generic strcmp
  2023-02-02 18:11 ` [PATCH v12 07/31] string: Improve generic strcmp Adhemerval Zanella
@ 2023-02-03 23:25   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:25 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> It follows the strategy:
>
>   - Align the first input to word boundary using byte operations.
>
>   - If second input is also word aligned, read a word per time, check for
>     null (using has_zero), and check final words using byte operation.
>
>   - If second input is not word aligned, loop by aligning the source, and
>     merging the result of two reads.  Similar to aligned case, check for
>     null with has_zero, and check final words using byte operation.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  string/strcmp.c | 110 +++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 94 insertions(+), 16 deletions(-)
>
> diff --git a/string/strcmp.c b/string/strcmp.c
> index 053f5a8d2b..11ec8bac81 100644
> --- a/string/strcmp.c
> +++ b/string/strcmp.c
> @@ -15,33 +15,111 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>
> +#include <stdint.h>
> +#include <string-fzb.h>
> +#include <string-fzc.h>
> +#include <string-fzi.h>
>  #include <string.h>
> +#include <memcopy.h>
>
> -#undef strcmp
> -
> -#ifndef STRCMP
> -# define STRCMP strcmp
> +#ifdef STRCMP
> +# define strcmp STRCMP
>  #endif
>
> +static inline int
> +final_cmp (const op_t w1, const op_t w2)
> +{
> +  unsigned int idx = index_first_zero_ne (w1, w2);
> +  return extractbyte (w1, idx) - extractbyte (w2, idx);
> +}
> +
> +/* Aligned loop: if a difference is found, exit to compare the bytes.  Else
> +   if a zero is found we have equal strings.  */
> +static inline int
> +strcmp_aligned_loop (const op_t *x1, const op_t *x2, op_t w1)
> +{
> +  op_t w2 = *x2++;
> +
> +  while (w1 == w2)
> +    {
> +      if (has_zero (w1))
> +       return 0;
> +      w1 = *x1++;
> +      w2 = *x2++;
> +    }
> +
> +  return final_cmp (w1, w2);
> +}
> +
> +/* Unaligned loop: align the first partial of P2, with 0xff for the rest of
> +   the bytes so that we can also apply the has_zero test to see if we have
> +   already reached EOS.  If we have, then we can simply fall through to the
> +   final comparison.  */
> +static inline int
> +strcmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs)
> +{
> +  op_t w2a = *x2++;
> +  uintptr_t sh_1 = ofs * CHAR_BIT;
> +  uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
> +
> +  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
> +  if (!has_zero (w2))
> +    {
> +      op_t w2b;
> +
> +      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
> +        does not contain end-of-string.  Therefore it is safe (and necessary)
> +        to read another word from each while we do not have a difference.  */
> +      while (1)
> +       {
> +         w2b = *x2++;
> +         w2 = MERGE (w2a, sh_1, w2b, sh_2);
> +         if (w1 != w2)
> +           return final_cmp (w1, w2);
> +         if (has_zero (w2b))
> +           break;
> +         w1 = *x1++;
> +         w2a = w2b;
> +       }
> +
> +      /* Zero found in the second partial of P2.  If we had EOS in the aligned
> +        word, we have equality.  */
> +      if (has_zero (w1))
> +       return 0;
> +
> +      /* Load the final word of P1 and align the final partial of P2.  */
> +      w1 = *x1++;
> +      w2 = MERGE (w2b, sh_1, 0, sh_2);
> +    }
> +
> +  return final_cmp (w1, w2);
> +}
> +
>  /* Compare S1 and S2, returning less than, equal to or
>     greater than zero if S1 is lexicographically less than,
>     equal to or greater than S2.  */
>  int
> -STRCMP (const char *p1, const char *p2)
> +strcmp (const char *p1, const char *p2)
>  {
> -  const unsigned char *s1 = (const unsigned char *) p1;
> -  const unsigned char *s2 = (const unsigned char *) p2;
> -  unsigned char c1, c2;
> -
> -  do
> +  /* Handle the unaligned bytes of p1 first.  */
> +  uintptr_t n = -(uintptr_t)p1 % sizeof(op_t);
> +  for (int i = 0; i < n; ++i)
>      {
> -      c1 = (unsigned char) *s1++;
> -      c2 = (unsigned char) *s2++;
> -      if (c1 == '\0')
> -       return c1 - c2;
> +      unsigned char c1 = *p1++;
> +      unsigned char c2 = *p2++;
> +      int diff = c1 - c2;
> +      if (c1 == '\0' || diff != 0)
> +       return diff;
>      }
> -  while (c1 == c2);
>
> -  return c1 - c2;
> +  /* P1 is now aligned to op_t.  P2 may or may not be.  */
> +  const op_t *x1 = (const op_t *) p1;
> +  op_t w1 = *x1++;
> +  uintptr_t ofs = (uintptr_t) p2 % sizeof(op_t);
> +  return ofs == 0
> +    ? strcmp_aligned_loop (x1, (const op_t *)p2, w1)
> +    : strcmp_unaligned_loop (x1, (const op_t *)(p2 - ofs), w1, ofs);
>  }
> +#ifndef STRCMP
>  libc_hidden_builtin_def (strcmp)
> +#endif
> --
> 2.34.1
>

LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 09/31] string: Improve generic stpcpy
  2023-02-02 18:11 ` [PATCH v12 09/31] string: Improve generic stpcpy Adhemerval Zanella
@ 2023-02-03 23:26   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:26 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> It follows the strategy:
>
>   - Align the destination on word boundary using byte operations.
>
>   - If source is also word aligned, read a word per time, check for
>     null (using has_zero from string-fzb.h), and write the remaining
>     bytes.
>
>   - If source is not word aligned, loop by aligning the source, and
>     merging the result of two reads.  Similar to aligned case,
>     check for null with has_zero, and write the remaining bytes if
>     null is found.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  string/stpcpy.c | 92 +++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 86 insertions(+), 6 deletions(-)
>
> diff --git a/string/stpcpy.c b/string/stpcpy.c
> index 8df5065cfe..dd0fef12ef 100644
> --- a/string/stpcpy.c
> +++ b/string/stpcpy.c
> @@ -15,12 +15,12 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>
> -#ifdef HAVE_CONFIG_H
> -# include <config.h>
> -#endif
> -
>  #define NO_MEMPCPY_STPCPY_REDIRECT
>  #include <string.h>
> +#include <stdint.h>
> +#include <memcopy.h>
> +#include <string-fzb.h>
> +#include <string-misc.h>
>
>  #undef __stpcpy
>  #undef stpcpy
> @@ -29,12 +29,92 @@
>  # define STPCPY __stpcpy
>  #endif
>
> +static __always_inline char *
> +write_byte_from_word (op_t *dest, op_t word)
> +{
> +  char *d = (char *) dest;
> +  for (size_t i = 0; i < OPSIZ; i++, ++d)
> +    {
> +      char c = extractbyte (word, i);
> +      *d = c;
> +      if (c == '\0')
> +       break;
> +    }
> +  return d;
> +}
> +
> +static __always_inline char *
> +stpcpy_aligned_loop (op_t *restrict dst, const op_t *restrict src)
> +{
> +  op_t word;
> +  while (1)
> +    {
> +      word = *src++;
> +      if (has_zero (word))
> +       break;
> +      *dst++ = word;
> +    }
> +
> +  return write_byte_from_word (dst, word);
> +}
> +
> +static __always_inline char *
> +stpcpy_unaligned_loop (op_t *restrict dst, const op_t *restrict src,
> +                      uintptr_t ofs)
> +{
> +  op_t w2a = *src++;
> +  uintptr_t sh_1 = ofs * CHAR_BIT;
> +  uintptr_t sh_2 = OPSIZ * CHAR_BIT - sh_1;
> +
> +  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
> +  if (!has_zero (w2))
> +    {
> +      op_t w2b;
> +
> +      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
> +        does not contain end-of-string.  Therefore it is safe (and necessary)
> +        to read another word from each while we do not have a difference.  */
> +      while (1)
> +       {
> +         w2b = *src++;
> +         w2 = MERGE (w2a, sh_1, w2b, sh_2);
> +         /* Check if there is zero on w2a.  */
> +         if (has_zero (w2))
> +           goto out;
> +         *dst++ = w2;
> +         if (has_zero (w2b))
> +           break;
> +         w2a = w2b;
> +       }
> +
> +      /* Align the final partial of P2.  */
> +      w2 = MERGE (w2b, sh_1, 0, sh_2);
> +    }
> +
> +out:
> +  return write_byte_from_word (dst, w2);
> +}
> +
> +
>  /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST.  */
>  char *
>  STPCPY (char *dest, const char *src)
>  {
> -  size_t len = strlen (src);
> -  return memcpy (dest, src, len + 1) + len;
> +  /* Copy just a few bytes to make DEST aligned.  */
> +  size_t len = (-(uintptr_t) dest) % OPSIZ;
> +  for (; len != 0; len--, ++dest)
> +    {
> +      char c = *src++;
> +      *dest = c;
> +      if (c == '\0')
> +       return dest;
> +    }
> +
> +  /* DEST is now aligned to op_t, SRC may or may not be.  */
> +  uintptr_t ofs = (uintptr_t) src % OPSIZ;
> +  return ofs == 0 ? stpcpy_aligned_loop ((op_t*) dest, (const op_t *) src)
> +                 : stpcpy_unaligned_loop ((op_t*) dest,
> +                                          (const op_t *) (src - ofs) , ofs);
>  }
>  weak_alias (__stpcpy, stpcpy)
>  libc_hidden_def (__stpcpy)
> --
> 2.34.1
>

LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 10/31] string: Improve generic strcpy
  2023-02-02 18:11 ` [PATCH v12 10/31] string: Improve generic strcpy Adhemerval Zanella
@ 2023-02-03 23:26   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:26 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> Now that stpcpy is vectorized based on op_t, it should be better to
> call it instead of strlen plus memcpy.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  string/strcpy.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/string/strcpy.c b/string/strcpy.c
> index 0345c71d15..d945d8fdf0 100644
> --- a/string/strcpy.c
> +++ b/string/strcpy.c
> @@ -19,6 +19,9 @@
>  #include <string.h>
>
>  #undef strcpy
> +/* Disable internal stpcpy optimization, otherwise the __stpcpy might it
> +   generate a strcpy call.  */
> +#undef __stpcpy
>
>  #ifndef STRCPY
>  # define STRCPY strcpy
> @@ -28,6 +31,7 @@
>  char *
>  STRCPY (char *dest, const char *src)
>  {
> -  return memcpy (dest, src, strlen (src) + 1);
> +  __stpcpy (dest, src);
> +  return dest;
>  }
>  libc_hidden_builtin_def (strcpy)
> --
> 2.34.1
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 11/31] string: Improve generic memchr
  2023-02-02 18:11 ` [PATCH v12 11/31] string: Improve generic memchr Adhemerval Zanella
@ 2023-02-03 23:26   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:26 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> New algorithm read the first aligned address and mask off the
> unwanted bytes (this strategy is similar to arch-specific
> implementations used on powerpc, sparc, and sh).
>
> The loop now read word-aligned address and check using the has_eq
> macro.
>
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powerpc64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
>
> Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
> Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>
> ---
>  string/memchr.c                               | 176 +++++-------------
>  .../powerpc32/power4/multiarch/memchr-ppc32.c |  14 +-
>  .../powerpc64/multiarch/memchr-ppc64.c        |   9 +-
>  3 files changed, 56 insertions(+), 143 deletions(-)
>
> diff --git a/string/memchr.c b/string/memchr.c
> index f800d47dce..4f497d6166 100644
> --- a/string/memchr.c
> +++ b/string/memchr.c
> @@ -1,10 +1,6 @@
> -/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
> +/* Scan memory for a character.  Generic version
> +   Copyright (C) 1991-2023 Free Software Foundation, Inc.
>     This file is part of the GNU C Library.
> -   Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
> -   with help from Dan Sahlin (dan@sics.se) and
> -   commentary by Jim Blandy (jimb@ai.mit.edu);
> -   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
> -   and implemented by Roland McGrath (roland@ai.mit.edu).
>
>     The GNU C Library is free software; you can redistribute it and/or
>     modify it under the terms of the GNU Lesser General Public
> @@ -20,143 +16,73 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>
> -#ifndef _LIBC
> -# include <config.h>
> -#endif
> -
> +#include <libc-pointer-arith.h>
> +#include <string-fzb.h>
> +#include <string-fzc.h>
> +#include <string-fzi.h>
> +#include <string-shift.h>
>  #include <string.h>
>
> -#include <stddef.h>
> +#undef memchr
>
> -#include <limits.h>
> -
> -#undef __memchr
> -#ifdef _LIBC
> -# undef memchr
> +#ifdef MEMCHR
> +# define __memchr MEMCHR
>  #endif
>
> -#ifndef weak_alias
> -# define __memchr memchr
> -#endif
> -
> -#ifndef MEMCHR
> -# define MEMCHR __memchr
> -#endif
> +static __always_inline const char *
> +sadd (uintptr_t x, uintptr_t y)
> +{
> +  return (const char *)(y > UINTPTR_MAX - x ? UINTPTR_MAX : x + y);
> +}
>
>  /* Search no more than N bytes of S for C.  */
>  void *
> -MEMCHR (void const *s, int c_in, size_t n)
> +__memchr (void const *s, int c_in, size_t n)
>  {
> -  /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
> -     long instead of a 64-bit uintmax_t tends to give better
> -     performance.  On 64-bit hardware, unsigned long is generally 64
> -     bits already.  Change this typedef to experiment with
> -     performance.  */
> -  typedef unsigned long int longword;
> -
> -  const unsigned char *char_ptr;
> -  const longword *longword_ptr;
> -  longword repeated_one;
> -  longword repeated_c;
> -  unsigned char c;
> -
> -  c = (unsigned char) c_in;
> -
> -  /* Handle the first few bytes by reading one byte at a time.
> -     Do this until CHAR_PTR is aligned on a longword boundary.  */
> -  for (char_ptr = (const unsigned char *) s;
> -       n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
> -       --n, ++char_ptr)
> -    if (*char_ptr == c)
> -      return (void *) char_ptr;
> -
> -  longword_ptr = (const longword *) char_ptr;
> -
> -  /* All these elucidatory comments refer to 4-byte longwords,
> -     but the theory applies equally well to any size longwords.  */
> -
> -  /* Compute auxiliary longword values:
> -     repeated_one is a value which has a 1 in every byte.
> -     repeated_c has c in every byte.  */
> -  repeated_one = 0x01010101;
> -  repeated_c = c | (c << 8);
> -  repeated_c |= repeated_c << 16;
> -  if (0xffffffffU < (longword) -1)
> +  if (__glibc_unlikely (n == 0))
> +    return NULL;
> +
> +  /* Read the first word, but munge it so that bytes before the array
> +     will not match goal.  */
> +  const op_t *word_ptr = PTR_ALIGN_DOWN (s, sizeof (op_t));
> +  uintptr_t s_int = (uintptr_t) s;
> +
> +  op_t word = *word_ptr;
> +  op_t repeated_c = repeat_bytes (c_in);
> +  /* Compute the address of the last byte taking in consideration possible
> +     overflow.  */
> +  const char *lbyte = sadd (s_int, n - 1);
> +  /* And also the address of the word containing the last byte. */
> +  const op_t *lword = (const op_t *) PTR_ALIGN_DOWN (lbyte, sizeof (op_t));
> +
> +  find_t mask = shift_find (find_eq_all (word, repeated_c), s_int);
> +  if (mask != 0)
>      {
> -      repeated_one |= repeated_one << 31 << 1;
> -      repeated_c |= repeated_c << 31 << 1;
> -      if (8 < sizeof (longword))
> -       {
> -         size_t i;
> -
> -         for (i = 64; i < sizeof (longword) * 8; i *= 2)
> -           {
> -             repeated_one |= repeated_one << i;
> -             repeated_c |= repeated_c << i;
> -           }
> -       }
> +      char *ret = (char *) s + index_first (mask);
> +      return (ret <= lbyte) ? ret : NULL;
>      }
> +  if (word_ptr == lword)
> +    return NULL;
>
> -  /* Instead of the traditional loop which tests each byte, we will test a
> -     longword at a time.  The tricky part is testing if *any of the four*
> -     bytes in the longword in question are equal to c.  We first use an xor
> -     with repeated_c.  This reduces the task to testing whether *any of the
> -     four* bytes in longword1 is zero.
> -
> -     We compute tmp =
> -       ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
> -     That is, we perform the following operations:
> -       1. Subtract repeated_one.
> -       2. & ~longword1.
> -       3. & a mask consisting of 0x80 in every byte.
> -     Consider what happens in each byte:
> -       - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
> -        and step 3 transforms it into 0x80.  A carry can also be propagated
> -        to more significant bytes.
> -       - If a byte of longword1 is nonzero, let its lowest 1 bit be at
> -        position k (0 <= k <= 7); so the lowest k bits are 0.  After step 1,
> -        the byte ends in a single bit of value 0 and k bits of value 1.
> -        After step 2, the result is just k bits of value 1: 2^k - 1.  After
> -        step 3, the result is 0.  And no carry is produced.
> -     So, if longword1 has only non-zero bytes, tmp is zero.
> -     Whereas if longword1 has a zero byte, call j the position of the least
> -     significant zero byte.  Then the result has a zero at positions 0, ...,
> -     j-1 and a 0x80 at position j.  We cannot predict the result at the more
> -     significant bytes (positions j+1..3), but it does not matter since we
> -     already have a non-zero bit at position 8*j+7.
> -
> -     So, the test whether any byte in longword1 is zero is equivalent to
> -     testing whether tmp is nonzero.  */
> -
> -  while (n >= sizeof (longword))
> +  word = *++word_ptr;
> +  while (word_ptr != lword)
>      {
> -      longword longword1 = *longword_ptr ^ repeated_c;
> -
> -      if ((((longword1 - repeated_one) & ~longword1)
> -          & (repeated_one << 7)) != 0)
> -       break;
> -      longword_ptr++;
> -      n -= sizeof (longword);
> +      if (has_eq (word, repeated_c))
> +       return (char *) word_ptr + index_first_eq (word, repeated_c);
> +      word = *++word_ptr;
>      }
>
> -  char_ptr = (const unsigned char *) longword_ptr;
> -
> -  /* At this point, we know that either n < sizeof (longword), or one of the
> -     sizeof (longword) bytes starting at char_ptr is == c.  On little-endian
> -     machines, we could determine the first such byte without any further
> -     memory accesses, just by looking at the tmp result from the last loop
> -     iteration.  But this does not work on big-endian machines.  Choose code
> -     that works in both cases.  */
> -
> -  for (; n > 0; --n, ++char_ptr)
> +  if (has_eq (word, repeated_c))
>      {
> -      if (*char_ptr == c)
> -       return (void *) char_ptr;
> +      /* We found a match, but it might be in a byte past the end of the
> +        array.  */
> +      char *ret = (char *) word_ptr + index_first_eq (word, repeated_c);
> +      if (ret <= lbyte)
> +       return ret;
>      }
> -
>    return NULL;
>  }
> -#ifdef weak_alias
> +#ifndef MEMCHR
>  weak_alias (__memchr, memchr)
> -#endif
>  libc_hidden_builtin_def (memchr)
> +#endif
> diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
> index 39ff84f3f3..a78585650f 100644
> --- a/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
> +++ b/sysdeps/powerpc/powerpc32/power4/multiarch/memchr-ppc32.c
> @@ -18,17 +18,11 @@
>
>  #include <string.h>
>
> -#define MEMCHR  __memchr_ppc
> +extern __typeof (memchr) __memchr_ppc attribute_hidden;
>
> -#undef weak_alias
> -#define weak_alias(a, b)
> +#define MEMCHR  __memchr_ppc
> +#include <string/memchr.c>
>
>  #ifdef SHARED
> -# undef libc_hidden_builtin_def
> -# define libc_hidden_builtin_def(name) \
> -  __hidden_ver1(__memchr_ppc, __GI_memchr, __memchr_ppc);
> +__hidden_ver1(__memchr_ppc, __GI_memchr, __memchr_ppc);
>  #endif
> -
> -extern __typeof (memchr) __memchr_ppc attribute_hidden;
> -
> -#include <string/memchr.c>
> diff --git a/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
> index 8097df709c..49ba5521fe 100644
> --- a/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
> +++ b/sysdeps/powerpc/powerpc64/multiarch/memchr-ppc64.c
> @@ -18,14 +18,7 @@
>
>  #include <string.h>
>
> -#define MEMCHR  __memchr_ppc
> -
> -#undef weak_alias
> -#define weak_alias(a, b)
> -
> -# undef libc_hidden_builtin_def
> -# define libc_hidden_builtin_def(name)
> -
>  extern __typeof (memchr) __memchr_ppc attribute_hidden;
>
> +#define MEMCHR  __memchr_ppc
>  #include <string/memchr.c>
> --
> 2.34.1
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 12/31] string: Improve generic strnlen with memchr
  2023-02-02 18:11 ` [PATCH v12 12/31] string: Improve generic strnlen with memchr Adhemerval Zanella
@ 2023-02-03 23:27   ` Noah Goldstein
  0 siblings, 0 replies; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:27 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> It also cleanups the multiple inclusion by leaving the ifunc
> implementation to undef the weak_alias and libc_hidden_def.
>
> Co-authored-by: Richard Henderson  <richard.henderson@linaro.org>
> ---
>  string/strnlen.c                              | 137 +-----------------
>  sysdeps/i386/i686/multiarch/strnlen-c.c       |  14 +-
>  .../power4/multiarch/strnlen-ppc32.c          |  14 +-
>  sysdeps/s390/strnlen-c.c                      |  14 +-
>  4 files changed, 27 insertions(+), 152 deletions(-)
>
> diff --git a/string/strnlen.c b/string/strnlen.c
> index 6ff294eab1..dc23354ec8 100644
> --- a/string/strnlen.c
> +++ b/string/strnlen.c
> @@ -1,10 +1,6 @@
>  /* Find the length of STRING, but scan at most MAXLEN characters.
>     Copyright (C) 1991-2023 Free Software Foundation, Inc.
>
> -   Based on strlen written by Torbjorn Granlund (tege@sics.se),
> -   with help from Dan Sahlin (dan@sics.se);
> -   commentary by Jim Blandy (jimb@ai.mit.edu).
> -
>     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
> @@ -20,7 +16,6 @@
>     not, see <https://www.gnu.org/licenses/>.  */
>
>  #include <string.h>
> -#include <stdlib.h>
>
>  /* Find the length of S, but scan at most MAXLEN characters.  If no
>     '\0' terminator is found in that many characters, return MAXLEN.  */
> @@ -32,134 +27,12 @@
>  size_t
>  __strnlen (const char *str, size_t maxlen)
>  {
> -  const char *char_ptr, *end_ptr = str + maxlen;
> -  const unsigned long int *longword_ptr;
> -  unsigned long int longword, himagic, lomagic;
> -
> -  if (maxlen == 0)
> -    return 0;
> -
> -  if (__glibc_unlikely (end_ptr < str))
> -    end_ptr = (const char *) ~0UL;
> -
> -  /* Handle the first few characters by reading one character at a time.
> -     Do this until CHAR_PTR is aligned on a longword boundary.  */
> -  for (char_ptr = str; ((unsigned long int) char_ptr
> -                       & (sizeof (longword) - 1)) != 0;
> -       ++char_ptr)
> -    if (*char_ptr == '\0')
> -      {
> -       if (char_ptr > end_ptr)
> -         char_ptr = end_ptr;
> -       return char_ptr - str;
> -      }
> -
> -  /* All these elucidatory comments refer to 4-byte longwords,
> -     but the theory applies equally well to 8-byte longwords.  */
> -
> -  longword_ptr = (unsigned long int *) char_ptr;
> -
> -  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
> -     the "holes."  Note that there is a hole just to the left of
> -     each byte, with an extra at the end:
> -
> -     bits:  01111110 11111110 11111110 11111111
> -     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
> -
> -     The 1-bits make sure that carries propagate to the next 0-bit.
> -     The 0-bits provide holes for carries to fall into.  */
> -  himagic = 0x80808080L;
> -  lomagic = 0x01010101L;
> -  if (sizeof (longword) > 4)
> -    {
> -      /* 64-bit version of the magic.  */
> -      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
> -      himagic = ((himagic << 16) << 16) | himagic;
> -      lomagic = ((lomagic << 16) << 16) | lomagic;
> -    }
> -  if (sizeof (longword) > 8)
> -    abort ();
> -
> -  /* Instead of the traditional loop which tests each character,
> -     we will test a longword at a time.  The tricky part is testing
> -     if *any of the four* bytes in the longword in question are zero.  */
> -  while (longword_ptr < (unsigned long int *) end_ptr)
> -    {
> -      /* We tentatively exit the loop if adding MAGIC_BITS to
> -        LONGWORD fails to change any of the hole bits of LONGWORD.
> -
> -        1) Is this safe?  Will it catch all the zero bytes?
> -        Suppose there is a byte with all zeros.  Any carry bits
> -        propagating from its left will fall into the hole at its
> -        least significant bit and stop.  Since there will be no
> -        carry from its most significant bit, the LSB of the
> -        byte to the left will be unchanged, and the zero will be
> -        detected.
> -
> -        2) Is this worthwhile?  Will it ignore everything except
> -        zero bytes?  Suppose every byte of LONGWORD has a bit set
> -        somewhere.  There will be a carry into bit 8.  If bit 8
> -        is set, this will carry into bit 16.  If bit 8 is clear,
> -        one of bits 9-15 must be set, so there will be a carry
> -        into bit 16.  Similarly, there will be a carry into bit
> -        24.  If one of bits 24-30 is set, there will be a carry
> -        into bit 31, so all of the hole bits will be changed.
> -
> -        The one misfire occurs when bits 24-30 are clear and bit
> -        31 is set; in this case, the hole at bit 31 is not
> -        changed.  If we had access to the processor carry flag,
> -        we could close this loophole by putting the fourth hole
> -        at bit 32!
> -
> -        So it ignores everything except 128's, when they're aligned
> -        properly.  */
> -
> -      longword = *longword_ptr++;
> -
> -      if ((longword - lomagic) & himagic)
> -       {
> -         /* Which of the bytes was the zero?  If none of them were, it was
> -            a misfire; continue the search.  */
> -
> -         const char *cp = (const char *) (longword_ptr - 1);
> -
> -         char_ptr = cp;
> -         if (cp[0] == 0)
> -           break;
> -         char_ptr = cp + 1;
> -         if (cp[1] == 0)
> -           break;
> -         char_ptr = cp + 2;
> -         if (cp[2] == 0)
> -           break;
> -         char_ptr = cp + 3;
> -         if (cp[3] == 0)
> -           break;
> -         if (sizeof (longword) > 4)
> -           {
> -             char_ptr = cp + 4;
> -             if (cp[4] == 0)
> -               break;
> -             char_ptr = cp + 5;
> -             if (cp[5] == 0)
> -               break;
> -             char_ptr = cp + 6;
> -             if (cp[6] == 0)
> -               break;
> -             char_ptr = cp + 7;
> -             if (cp[7] == 0)
> -               break;
> -           }
> -       }
> -      char_ptr = end_ptr;
> -    }
> -
> -  if (char_ptr > end_ptr)
> -    char_ptr = end_ptr;
> -  return char_ptr - str;
> +  const char *found = memchr (str, '\0', maxlen);
> +  return found ? found - str : maxlen;
>  }
> +
>  #ifndef STRNLEN
> -libc_hidden_def (__strnlen)
>  weak_alias (__strnlen, strnlen)
> -#endif
> +libc_hidden_def (__strnlen)
>  libc_hidden_def (strnlen)
> +#endif
> diff --git a/sysdeps/i386/i686/multiarch/strnlen-c.c b/sysdeps/i386/i686/multiarch/strnlen-c.c
> index 351e939a93..beb0350d53 100644
> --- a/sysdeps/i386/i686/multiarch/strnlen-c.c
> +++ b/sysdeps/i386/i686/multiarch/strnlen-c.c
> @@ -1,10 +1,10 @@
>  #define STRNLEN  __strnlen_ia32
> +#include <string/strnlen.c>
> +
>  #ifdef SHARED
> -# undef libc_hidden_def
> -# define libc_hidden_def(name)  \
> -    __hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32); \
> -    strong_alias (__strnlen_ia32, __strnlen_ia32_1); \
> -    __hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
> +/* Alias for internal symbol to avoid PLT generation, it redirects the
> +   libc_hidden_def (__strnlen/strlen) to default implementation.  */
> +__hidden_ver1 (__strnlen_ia32, __GI_strnlen, __strnlen_ia32);
> +strong_alias (__strnlen_ia32, __strnlen_ia32_1);
> +__hidden_ver1 (__strnlen_ia32_1, __GI___strnlen, __strnlen_ia32_1);
>  #endif
> -
> -#include "string/strnlen.c"
> diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
> index 957b9b99e8..2ca1cd7181 100644
> --- a/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
> +++ b/sysdeps/powerpc/powerpc32/power4/multiarch/strnlen-ppc32.c
> @@ -17,12 +17,12 @@
>     <https://www.gnu.org/licenses/>.  */
>
>  #define STRNLEN  __strnlen_ppc
> +#include <string/strnlen.c>
> +
>  #ifdef SHARED
> -# undef libc_hidden_def
> -# define libc_hidden_def(name)  \
> -    __hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
> -    strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
> -    __hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
> +/* Alias for internal symbol to avoid PLT generation, it redirects the
> +   libc_hidden_def (__strnlen/strlen) to default implementation.  */
> +__hidden_ver1 (__strnlen_ppc, __GI_strnlen, __strnlen_ppc); \
> +strong_alias (__strnlen_ppc, __strnlen_ppc_1); \
> +__hidden_ver1 (__strnlen_ppc_1, __GI___strnlen, __strnlen_ppc_1);
>  #endif
> -
> -#include <string/strnlen.c>
> diff --git a/sysdeps/s390/strnlen-c.c b/sysdeps/s390/strnlen-c.c
> index 172fcc7caa..95156a0ff5 100644
> --- a/sysdeps/s390/strnlen-c.c
> +++ b/sysdeps/s390/strnlen-c.c
> @@ -21,14 +21,16 @@
>  #if HAVE_STRNLEN_C
>  # if HAVE_STRNLEN_IFUNC
>  #  define STRNLEN STRNLEN_C
> +# endif
> +
> +# include <string/strnlen.c>
> +
> +# if HAVE_STRNLEN_IFUNC
>  #  if defined SHARED && IS_IN (libc)
> -#   undef libc_hidden_def
> -#   define libc_hidden_def(name)                                       \
> -  __hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);      \
> -  strong_alias (__strnlen_c, __strnlen_c_1);                   \
> -  __hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
> +__hidden_ver1 (__strnlen_c, __GI_strnlen, __strnlen_c);
> +strong_alias (__strnlen_c, __strnlen_c_1);
> +__hidden_ver1 (__strnlen_c_1, __GI___strnlen, __strnlen_c_1);
>  #  endif
>  # endif
>
> -# include <string/strnlen.c>
>  #endif
> --
> 2.34.1
>
LGTM.
Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 22/31] string: Hook up the default implementation on test-strlen
  2023-02-02 18:11 ` [PATCH v12 22/31] string: Hook up the default implementation on test-strlen Adhemerval Zanella
@ 2023-02-03 23:30   ` Noah Goldstein
  2023-02-06 17:36     ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Noah Goldstein @ 2023-02-03 23:30 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao

On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> Also remove the simple_STRLEN and builtin_strlen, which are not used
> anywhere.
Think I'm slightly opposed to removing the simple_* test implementations
as they can be useful to hook in when debugging.
>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  string/test-strlen.c | 31 ++++++++++++++-----------------
>  1 file changed, 14 insertions(+), 17 deletions(-)
>
> diff --git a/string/test-strlen.c b/string/test-strlen.c
> index 81adf23737..0f98576f40 100644
> --- a/string/test-strlen.c
> +++ b/string/test-strlen.c
> @@ -37,27 +37,24 @@
>
>  typedef size_t (*proto_t) (const CHAR *);
>
> -/* Naive implementation to verify results.  */
> -size_t
> -simple_STRLEN (const CHAR *s)
> -{
> -  const CHAR *p;
> -
> -  for (p = s; *p; ++p);
> -  return p - s;
> -}
> +IMPL (STRLEN, 1)
>
> +/* Also check the generic implementation.  */
> +#undef STRLEN
> +#undef weak_alias
> +#define weak_alias(a, b)
> +#undef libc_hidden_builtin_def
> +#define libc_hidden_builtin_def(a)
>  #ifndef WIDE
> -size_t
> -builtin_strlen (const CHAR *p)
> -{
> -  return __builtin_strlen (p);
> -}
> -IMPL (builtin_strlen, 0)
> +# define STRLEN __strlen_default
> +# include "string/strlen.c"
> +IMPL (__strlen_default, 1)
> +#else
> +# define WCSLEN __wcslen_default
> +# include "wcsmbs/wcslen.c"
> +IMPL (__wcslen_default, 1)
>  #endif
>
> -IMPL (STRLEN, 1)
> -
>
>  static void
>  do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
> --
> 2.34.1
>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 03/31] Add string vectorized find and detection functions
  2023-02-03 13:40       ` Adhemerval Zanella Netto
@ 2023-02-04  2:54         ` Richard Henderson
  0 siblings, 0 replies; 61+ messages in thread
From: Richard Henderson @ 2023-02-04  2:54 UTC (permalink / raw)
  To: Adhemerval Zanella Netto, libc-alpha, Jeff Law, Xi Ruoyao,
	Noah Goldstein

On 2/3/23 03:40, Adhemerval Zanella Netto wrote:
> I will send only this patch to avoid resend all the thread that is already
> reviewed.
> 
> ---
> 
> [PATCH 03/31] Add string vectorized find and detection functions
> 
> This patch adds generic string find and detection meant to be used in
> generic vectorized string implementation.  The idea is to decompose the
> basic string operation so each architecture can reimplement if it
> provides any specialized hardware instruction.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

> 
> The 'string-misc.h' provides miscellaneous functions:
> 
>    - extractbyte: extracts the byte from an specific index.
>    - repeat_bytes: setup an word by replicate the argument on each byte.
> 
> The 'string-fza.h' provides zero byte detection functions:
> 
>    - find_zero_low, find_zero_all, find_eq_low, find_eq_all,
>      find_zero_eq_low, find_zero_eq_all, and find_zero_ne_all
> 
> The 'string-fzb.h' provides boolean zero byte detection functions:
> 
>    - has_zero: determine if any byte within a word is zero.
>    - has_eq: determine byte equality between two words.
>    - has_zero_eq: determine if any byte within a word is zero along with
>      byte equality between two words.
> 
> The 'string-fzi.h' provides positions for string-fza.h results:
> 
>    - index_first: return index of first zero byte within a word.
>    - index_last: return index of first byte different between two words.
> 
> The 'string-fzc.h' provides a combined version of fza and fzi:
> 
>    - index_first_zero_eq: return index of first zero byte within a word or
>      first byte different between two words.
>    - index_first_zero_ne: return index of first zero byte within a word or
>      first byte equal between two words.
>    - index_last_zero: return index of last zero byte within a word.
>    - index_last_eq: return index of last byte different between two words.
> 
> The 'string-shift.h' provides a way to mask off parts of a work based on
> some alignmnet (to handle unaligned arguments):
> 
>    - shift_find, shift_find_last.
> 
> Co-authored-by: Richard Henderson <richard.henderson@linaro.org>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 06/31] string: Improve generic strchr
  2023-02-02 18:11 ` [PATCH v12 06/31] string: Improve generic strchr Adhemerval Zanella
  2023-02-03 23:24   ` Noah Goldstein
@ 2023-02-04  2:58   ` Richard Henderson
  2023-02-06 13:07     ` Adhemerval Zanella Netto
  1 sibling, 1 reply; 61+ messages in thread
From: Richard Henderson @ 2023-02-04  2:58 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein

On 2/2/23 08:11, Adhemerval Zanella wrote:
> New algorithm now calls strchrnul.
> 
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powerpc64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
> 
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

strchrnul needs libc_hidden_builtin_proto.  On riscv64:

    8:	00000097          	auipc	ra,0x0
			8: R_RISCV_CALL_PLT	__strchrnul
			8: R_RISCV_RELAX	*ABS*
    c:	000080e7          	jalr	ra # 8 <__GI_strchr+0x8>


r~

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen
  2023-02-02 18:11 ` [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen Adhemerval Zanella
@ 2023-02-04  3:06   ` Richard Henderson
  2023-02-06 14:01     ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Richard Henderson @ 2023-02-04  3:06 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein

On 2/2/23 08:11, Adhemerval Zanella wrote:
> Now that both strlen and memrchr have word vectorized implementation,
> it should be faster to implement strrchr based on memrchr over the
> string length instead of calling strchr on a loop.
> 
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
> and powerpc64-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).
> ---
>   string/strrchr.c | 18 +-----------------
>   1 file changed, 1 insertion(+), 17 deletions(-)

memrchr needs libc_hidden_builtin_proto.  On riscv64:

    c:   00000097                auipc   ra,0x0
                         c: R_RISCV_CALL __GI_strlen
                         c: R_RISCV_RELAX        *ABS*
   10:   000080e7                jalr    ra # c <__GI_strrchr+0xc>
...
   24:   00000317                auipc   t1,0x0
                         24: R_RISCV_CALL_PLT    __memrchr
                         24: R_RISCV_RELAX       *ABS*
   28:   00030067                jr      t1 # 24 <.LVL2+0x8>


r~

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 06/31] string: Improve generic strchr
  2023-02-04  2:58   ` Richard Henderson
@ 2023-02-06 13:07     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-06 13:07 UTC (permalink / raw)
  To: Richard Henderson, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein



On 03/02/23 23:58, Richard Henderson wrote:
> On 2/2/23 08:11, Adhemerval Zanella wrote:
>> New algorithm now calls strchrnul.
>>
>> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
>> and powerpc64-linux-gnu by removing the arch-specific assembly
>> implementation and disabling multi-arch (it covers both LE and BE
>> for 64 and 32 bits).
>>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> 
> strchrnul needs libc_hidden_builtin_proto.  On riscv64:
> 
>    8:    00000097              auipc    ra,0x0
>             8: R_RISCV_CALL_PLT    __strchrnul
>             8: R_RISCV_RELAX    *ABS*
>    c:    000080e7              jalr    ra # 8 <__GI_strchr+0x8>

It is similar to x86_64 as well:

x86_64-linux-gnu$ objdump -dwr posix/execvpe.os
[...]
 234:   4c 89 ff                mov    %r15,%rdi
 237:   e8 00 00 00 00          call   23c <__execvpe_common+0x12c>     238: R_X86_64_PLT32     __strchrnul-0x4
[...]

But the static linker ends up generating a local call as expected:

$ riscv64-glibc-linux-gnu-objdump -dr libc.so
[...]
000000000008726e <strchr>:
   8726e:       7179                    addi    sp,sp,-48
   87270:       ec26                    sd      s1,24(sp)
   87272:       000e9497                auipc   s1,0xe9
   87276:       4264b483                ld      s1,1062(s1) # 170698 <__stack_chk_guard@GLIBC_2.27>
   8727a:       609c                    ld      a5,0(s1)
   8727c:       e43e                    sd      a5,8(sp)
   8727e:       4781                    li      a5,0
   87280:       f022                    sd      s0,32(sp)
   87282:       f406                    sd      ra,40(sp)
   87284:       842e                    mv      s0,a1
   87286:       02a000ef                jal     ra,872b0 <strchrnul>
[...]

The intra PLT should trigger a regression with make check local-plt test,
and I did a make check for all architecture to make sure I am not missing
anything.

I will send a patch to fix this internal inconsistencies.

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen
  2023-02-04  3:06   ` Richard Henderson
@ 2023-02-06 14:01     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-06 14:01 UTC (permalink / raw)
  To: Richard Henderson, libc-alpha, Jeff Law, Xi Ruoyao, Noah Goldstein



On 04/02/23 00:06, Richard Henderson wrote:
> On 2/2/23 08:11, Adhemerval Zanella wrote:
>> Now that both strlen and memrchr have word vectorized implementation,
>> it should be faster to implement strrchr based on memrchr over the
>> string length instead of calling strchr on a loop.
>>
>> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
>> and powerpc64-linux-gnu by removing the arch-specific assembly
>> implementation and disabling multi-arch (it covers both LE and BE
>> for 64 and 32 bits).
>> ---
>>   string/strrchr.c | 18 +-----------------
>>   1 file changed, 1 insertion(+), 17 deletions(-)
> 
> memrchr needs libc_hidden_builtin_proto.  On riscv64:
> 
>    c:   00000097                auipc   ra,0x0
>                         c: R_RISCV_CALL __GI_strlen
>                         c: R_RISCV_RELAX        *ABS*
>   10:   000080e7                jalr    ra # c <__GI_strrchr+0xc>
> ...
>   24:   00000317                auipc   t1,0x0
>                         24: R_RISCV_CALL_PLT    __memrchr
>                         24: R_RISCV_RELAX       *ABS*
>   28:   00030067                jr      t1 # 24 <.LVL2+0x8>
> 
> 
> r~

Similar to strchr [1], static linker ends up creating the expected local
call:

$ riscv64-glibc-linux-gnu-objdump -dr libc.so
[...]
0000000000088f5c <strrchr>:
   88f5c:       7179                    addi    sp,sp,-48
   88f5e:       e84a                    sd      s2,16(sp)
   88f60:       000e7917                auipc   s2,0xe7
   88f64:       73893903                ld      s2,1848(s2) # 170698 <__stack_chk_guard@GLIBC_2.27>
   88f68:       00093783                ld      a5,0(s2)
[...]
   88f9a:       6145                    addi    sp,sp,48
   88f9c:       c70fd06f                j       8640c <memrchr>
   88fa0:       193500ef                jal     ra,d9932 <__stack_chk_fail>
[...]

But I agree that we should change to have the libc_hidden_builin_proto
in this case.  I will send a patch to fix it.

[1] https://sourceware.org/pipermail/libc-alpha/2023-February/145322.html

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h
  2023-02-03 19:47   ` Noah Goldstein
@ 2023-02-06 16:14     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-06 16:14 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao



On 03/02/23 16:47, Noah Goldstein wrote:
> On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>> It uses the bitmanip extension to optimize index_fist and index_last
>> with clz/ctz (using generic implementation that routes to compiler
>> builtin) and orc.b to check null bytes.
>>
>> Checked the string test on riscv64 user mode.
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>> ---
>>  sysdeps/riscv/string-fza.h | 70 ++++++++++++++++++++++++++++++++++
>>  sysdeps/riscv/string-fzi.h | 77 ++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 147 insertions(+)
>>  create mode 100644 sysdeps/riscv/string-fza.h
>>  create mode 100644 sysdeps/riscv/string-fzi.h
>>
>> diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
>> new file mode 100644
>> index 0000000000..9c7a6efba2
>> --- /dev/null
>> +++ b/sysdeps/riscv/string-fza.h
>> @@ -0,0 +1,70 @@
>> +/* Zero byte detection; basics.  RISCV 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/>.  */
>> +
>> +#ifndef _RISCV_STRING_FZA_H
>> +#define _RISCV_STRING_FZA_H 1
>> +
>> +#ifdef __riscv_zbb
>> +/* With bitmap extension we can use orc.b to find all zero bytes.  */
>> +# include <string-misc.h>
>> +# include <string-optype.h>
>> +
>> +/* The functions return a byte mask.  */
>> +typedef op_t find_t;
>> +
>> +/* This function returns 0xff for each byte that is zero in X.  */
>> +static __always_inline find_t
>> +find_zero_all (op_t x)
>> +{
>> +  find_t r;
>> +  asm ("orc.b %0, %1" : "=r" (r) : "r" (x));
>> +  return ~r;
>> +}
>> +
>> +/* This function returns 0xff for each byte that is equal between X1 and
>> +   X2.  */
>> +static __always_inline find_t
>> +find_eq_all (op_t x1, op_t x2)
>> +{
>> +  return find_zero_all (x1 ^ x2);
>> +}
>> +
>> +/* Identify zero bytes in X1 or equality between X1 and X2.  */
>> +static __always_inline find_t
>> +find_zero_eq_all (op_t x1, op_t x2)
>> +{
>> +  return find_zero_all (x1) | find_eq_all (x1, x2);
>> +}
>> +
>> +/* Identify zero bytes in X1 or inequality between X1 and X2.  */
>> +static __always_inline find_t
>> +find_zero_ne_all (op_t x1, op_t x2)
>> +{
>> +  return find_zero_all (x1) | ~find_eq_all (x1, x2);
>> +}
>> +
>> +/* Define the "inexact" versions in terms of the exact versions.  */
>> +# define find_zero_low         find_zero_all
>> +# define find_eq_low           find_eq_all
>> +# define find_zero_eq_low      find_zero_eq_all
>> +# define find_zero_ne_low      find_zero_ne_all
>> +#else
>> +#include <sysdeps/generic/string-fza.h>
>> +#endif
>> +
>> +#endif /* _RISCV_STRING_FZA_H  */
>> diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
>> new file mode 100644
>> index 0000000000..3cde113afb
>> --- /dev/null
>> +++ b/sysdeps/riscv/string-fzi.h
>> @@ -0,0 +1,77 @@
>> +/* Zero byte detection; indexes.  RISCV 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/>.  */
>> +
>> +#ifndef _STRING_RISCV_FZI_H
>> +#define _STRING_RISCV_FZI_H 1
>> +
>> +#ifdef __riscv_zbb
>> +# include <sysdeps/generic/string-fzi.h>
>> +#else
>> +/* Without bitmap clz/ctz extensions, it is faster to direct test the bits
>> +   instead of calling compiler auxiliary functions.  */
>> +# include <string-optype.h>
>> +
>> +static __always_inline unsigned int
>> +index_first (find_t c)
>> +{
>> +  if (c & 0x80U)
>> +    return 0;
>> +  if (c & 0x8000U)
>> +    return 1;
>> +  if (c & 0x800000U)
>> +    return 2;
>> +
>> +  if (sizeof (op_t) == 4)
>> +    return 3;
>> +
>> +  if (c & 0x80000000U)
>> +    return 3;
>> +  if (c & 0x8000000000UL)
>> +    return 4;
>> +  if (c & 0x800000000000UL)
>> +    return 5;
>> +  if (c & 0x80000000000000UL)
>> +    return 6;
>> +  return 7;
>> +}
>> +
>> +static __always_inline unsigned int
>> +index_last (find_t c)
>> +{
>> +  if (sizeof (op_t) == 8)
>> +    {
>> +      if (c & 0x8000000000000000UL)
>> +       return 7;
>> +      if (c & 0x80000000000000UL)
>> +       return 6;
>> +      if (c & 0x800000000000UL)
>> +       return 5;
>> +      if (c & 0x8000000000UL)
>> +       return 4;
>> +    }
>> +  if (c & 0x80000000U)
>> +    return 3;
>> +  if (c & 0x800000U)
>> +    return 2;
>> +  if (c & 0x8000U)
>> +    return 1;
>> +  return 0;
>> +}
>> +#endif
>> +
>> +#endif /* STRING_FZI_H */
>> --
>> 2.34.1
>>
> 
> Has WS error:
> ```
> Applying: riscv: Add string-fza.h and string-fzi.h
> .git/rebase-apply/patch:115: trailing whitespace.
>    instead of calling compiler auxiliary functions.  */
> warning: 1 line adds whitespace errors.
> ```

Thanks, I will fix it before apply.

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 22/31] string: Hook up the default implementation on test-strlen
  2023-02-03 23:30   ` Noah Goldstein
@ 2023-02-06 17:36     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-06 17:36 UTC (permalink / raw)
  To: Noah Goldstein; +Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao



On 03/02/23 20:30, Noah Goldstein wrote:
> On Thu, Feb 2, 2023 at 12:12 PM Adhemerval Zanella
> <adhemerval.zanella@linaro.org> wrote:
>>
>> Also remove the simple_STRLEN and builtin_strlen, which are not used
>> anywhere.
> Think I'm slightly opposed to removing the simple_* test implementations
> as they can be useful to hook in when debugging.

I think the simple_* test are really easy to come by if you need to debug
a string/memory implementation; and they do consume CPU cycles on testing
with no apparent gain.

>>
>> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
>> ---
>>  string/test-strlen.c | 31 ++++++++++++++-----------------
>>  1 file changed, 14 insertions(+), 17 deletions(-)
>>
>> diff --git a/string/test-strlen.c b/string/test-strlen.c
>> index 81adf23737..0f98576f40 100644
>> --- a/string/test-strlen.c
>> +++ b/string/test-strlen.c
>> @@ -37,27 +37,24 @@
>>
>>  typedef size_t (*proto_t) (const CHAR *);
>>
>> -/* Naive implementation to verify results.  */
>> -size_t
>> -simple_STRLEN (const CHAR *s)
>> -{
>> -  const CHAR *p;
>> -
>> -  for (p = s; *p; ++p);
>> -  return p - s;
>> -}
>> +IMPL (STRLEN, 1)
>>
>> +/* Also check the generic implementation.  */
>> +#undef STRLEN
>> +#undef weak_alias
>> +#define weak_alias(a, b)
>> +#undef libc_hidden_builtin_def
>> +#define libc_hidden_builtin_def(a)
>>  #ifndef WIDE
>> -size_t
>> -builtin_strlen (const CHAR *p)
>> -{
>> -  return __builtin_strlen (p);
>> -}
>> -IMPL (builtin_strlen, 0)
>> +# define STRLEN __strlen_default
>> +# include "string/strlen.c"
>> +IMPL (__strlen_default, 1)
>> +#else
>> +# define WCSLEN __wcslen_default
>> +# include "wcsmbs/wcslen.c"
>> +IMPL (__wcslen_default, 1)
>>  #endif
>>
>> -IMPL (STRLEN, 1)
>> -
>>
>>  static void
>>  do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
>> --
>> 2.34.1
>>

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 18/31] arm: Add string-fza.h
  2023-02-02 18:11 ` [PATCH v12 18/31] arm: Add string-fza.h Adhemerval Zanella
@ 2023-02-20 13:24   ` Szabolcs Nagy
  2023-02-20 13:45     ` Szabolcs Nagy
  0 siblings, 1 reply; 61+ messages in thread
From: Szabolcs Nagy @ 2023-02-20 13:24 UTC (permalink / raw)
  To: Adhemerval Zanella
  Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
> From: Richard Henderson <richard.henderson@linaro.org>
> 
> While arm has the more important string functions in assembly,
> there are still a few generic routines used.
> 
> Use the UQSUB8 insn for testing of zeros.
> 
> Checked on armv7-linux-gnueabihf
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> ---
>  sysdeps/arm/armv6t2/string-fza.h | 68 ++++++++++++++++++++++++++++++++
...
> +static __always_inline find_t
> +find_zero_all (op_t x)
> +{
> +  /* Use unsigned saturated subtraction from 1 in each byte.
> +     That leaves 1 for every byte that was zero.  */
> +  op_t ones = repeat_bytes (0x01);
> +  return __builtin_arm_uqsub8 (ones, x);
> +}

__builtin_arm_uqsub8 is only available >=gcc-10

so now the build fails with gcc-9

../sysdeps/arm/armv6t2/string-fza.h:36:10: error: implicit declaration of function ‘__builtin_arm_uqsub8’; did you mean ‘__builtin_arm_stc’? [-Werror=implicit-function-declaration]
   36 |   return __builtin_arm_uqsub8 (ones, x);

so this code should be conditional on gcc version.


^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 18/31] arm: Add string-fza.h
  2023-02-20 13:24   ` Szabolcs Nagy
@ 2023-02-20 13:45     ` Szabolcs Nagy
  2023-02-20 14:01       ` Adhemerval Zanella Netto
  0 siblings, 1 reply; 61+ messages in thread
From: Szabolcs Nagy @ 2023-02-20 13:45 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha, Richard Henderson, Jeff Law,
	Xi Ruoyao, Noah Goldstein

The 02/20/2023 13:24, Szabolcs Nagy via Libc-alpha wrote:
> The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
> > +static __always_inline find_t
> > +find_zero_all (op_t x)
> > +{
> > +  /* Use unsigned saturated subtraction from 1 in each byte.
> > +     That leaves 1 for every byte that was zero.  */
> > +  op_t ones = repeat_bytes (0x01);
> > +  return __builtin_arm_uqsub8 (ones, x);
> > +}
> 
> __builtin_arm_uqsub8 is only available >=gcc-10
> 
> so now the build fails with gcc-9
> 
> ../sysdeps/arm/armv6t2/string-fza.h:36:10: error: implicit declaration of function ‘__builtin_arm_uqsub8’; did you mean ‘__builtin_arm_stc’? [-Werror=implicit-function-declaration]
>    36 |   return __builtin_arm_uqsub8 (ones, x);
> 
> so this code should be conditional on gcc version.
> 

i think

 asm ("uqsub8  %0, %0, %1" : "+r" (ones) : "r" (x));

should be a good fallback (untested).

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 18/31] arm: Add string-fza.h
  2023-02-20 13:45     ` Szabolcs Nagy
@ 2023-02-20 14:01       ` Adhemerval Zanella Netto
  2023-02-20 16:12         ` Szabolcs Nagy
  0 siblings, 1 reply; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-20 14:01 UTC (permalink / raw)
  To: Szabolcs Nagy, libc-alpha, Richard Henderson, Jeff Law,
	Xi Ruoyao, Noah Goldstein



On 20/02/23 10:45, Szabolcs Nagy wrote:
> The 02/20/2023 13:24, Szabolcs Nagy via Libc-alpha wrote:
>> The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
>>> +static __always_inline find_t
>>> +find_zero_all (op_t x)
>>> +{
>>> +  /* Use unsigned saturated subtraction from 1 in each byte.
>>> +     That leaves 1 for every byte that was zero.  */
>>> +  op_t ones = repeat_bytes (0x01);
>>> +  return __builtin_arm_uqsub8 (ones, x);
>>> +}
>>
>> __builtin_arm_uqsub8 is only available >=gcc-10
>>
>> so now the build fails with gcc-9
>>
>> ../sysdeps/arm/armv6t2/string-fza.h:36:10: error: implicit declaration of function ‘__builtin_arm_uqsub8’; did you mean ‘__builtin_arm_stc’? [-Werror=implicit-function-declaration]
>>    36 |   return __builtin_arm_uqsub8 (ones, x);
>>
>> so this code should be conditional on gcc version.
>>
> 
> i think
> 
>  asm ("uqsub8  %0, %0, %1" : "+r" (ones) : "r" (x));
> 
> should be a good fallback (untested).

This is what we have on v7:

static __always_inline op_t
find_zero_all (op_t x)
{
  /* Use unsigned saturated subtraction from 1 in each byte.
     That leaves 1 for every byte that was zero.  */
  op_t ret, ones = repeat_bytes (0x01);
  asm ("uqsub8 %0,%1,%2" : "=r"(ret) : "r"(ones), "r"(x));
  return ret;
}

Maybe extend with:

static __always_inline op_t
find_zero_all (op_t x)
{
  op_t ones = repeat_bytes (0x01);
#if __GNUC_PREREQ (10, 0)
  return __builtin_arm_uqsub8 (ones, x);
#else
  op_t ret;
  asm ("uqsub8 %0,%1,%2" : "=r"(ret) : "r"(ones), "r"(x));
  return ret;
#endif
}

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 18/31] arm: Add string-fza.h
  2023-02-20 14:01       ` Adhemerval Zanella Netto
@ 2023-02-20 16:12         ` Szabolcs Nagy
  0 siblings, 0 replies; 61+ messages in thread
From: Szabolcs Nagy @ 2023-02-20 16:12 UTC (permalink / raw)
  To: Adhemerval Zanella Netto
  Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

The 02/20/2023 11:01, Adhemerval Zanella Netto wrote:
> 
> 
> On 20/02/23 10:45, Szabolcs Nagy wrote:
> > The 02/20/2023 13:24, Szabolcs Nagy via Libc-alpha wrote:
> >> The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
> >>> +static __always_inline find_t
> >>> +find_zero_all (op_t x)
> >>> +{
> >>> +  /* Use unsigned saturated subtraction from 1 in each byte.
> >>> +     That leaves 1 for every byte that was zero.  */
> >>> +  op_t ones = repeat_bytes (0x01);
> >>> +  return __builtin_arm_uqsub8 (ones, x);
> >>> +}
> >>
> >> __builtin_arm_uqsub8 is only available >=gcc-10
> >>
> >> so now the build fails with gcc-9
> >>
> >> ../sysdeps/arm/armv6t2/string-fza.h:36:10: error: implicit declaration of function ‘__builtin_arm_uqsub8’; did you mean ‘__builtin_arm_stc’? [-Werror=implicit-function-declaration]
> >>    36 |   return __builtin_arm_uqsub8 (ones, x);
> >>
> >> so this code should be conditional on gcc version.
> >>
> > 
> > i think
> > 
> >  asm ("uqsub8  %0, %0, %1" : "+r" (ones) : "r" (x));
> > 
> > should be a good fallback (untested).
> 
> This is what we have on v7:
> 
> static __always_inline op_t
> find_zero_all (op_t x)
> {
>   /* Use unsigned saturated subtraction from 1 in each byte.
>      That leaves 1 for every byte that was zero.  */
>   op_t ret, ones = repeat_bytes (0x01);
>   asm ("uqsub8 %0,%1,%2" : "=r"(ret) : "r"(ones), "r"(x));
>   return ret;
> }
> 
> Maybe extend with:
> 
> static __always_inline op_t
> find_zero_all (op_t x)
> {
>   op_t ones = repeat_bytes (0x01);
> #if __GNUC_PREREQ (10, 0)
>   return __builtin_arm_uqsub8 (ones, x);
> #else
>   op_t ret;
>   asm ("uqsub8 %0,%1,%2" : "=r"(ret) : "r"(ones), "r"(x));
>   return ret;
> #endif
> }

yes this looks good to me.


^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 08/31] string: Improve generic strncmp
  2023-02-02 18:11 ` [PATCH v12 08/31] string: Improve generic strncmp Adhemerval Zanella
  2023-02-03  0:34   ` Richard Henderson
@ 2023-02-21  9:28   ` Szabolcs Nagy
  2023-02-21 12:24     ` Adhemerval Zanella Netto
  1 sibling, 1 reply; 61+ messages in thread
From: Szabolcs Nagy @ 2023-02-21  9:28 UTC (permalink / raw)
  To: Adhemerval Zanella
  Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein

The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
> It follows the strategy:
> 
>    - Align the first input to word boundary using byte operations.
> 
>    - If second input is also word aligned, read a word per time, check
>      for  null (using has_zero), and check final words using byte
>      operation.
> 
>    - If second input is not word aligned, loop by aligning the source,
>      and merge the result of two reads.  Similar to aligned case, check
>      for null with has_zero, and check final words using byte operation.
> 
> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
> and powerpc-linux-gnu by removing the arch-specific assembly
> implementation and disabling multi-arch (it covers both LE and BE
> for 64 and 32 bits).

on arm i see

FAIL: crypt/badsalttest

Program received signal SIGSEGV, Segmentation fault.
strncmp_unaligned_loop (n=3, ofs=<optimized out>, w1=2371876, x2=0xf7feb000, x1=0xf7f6563c) at strncmp.c:85
85                w2b = *x2++;

this strncmp does out of bounds read:

Breakpoint 2, __GI_strncmp (p1=0xf7f65638 <md5_salt_prefix> "$1$", p2=p2@entry=0xf7feafff "*", n=n@entry=3) at strncmp.c:115

0xf7feb000 is mapped PROT_NONE.

> +strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
> +			size_t n)
> +{
> +  op_t w2a = *x2++;
> +  uintptr_t sh_1 = ofs * CHAR_BIT;
> +  uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
> +
> +  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
> +  if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
>      {
> -      c1 = (unsigned char) *s1++;
> -      c2 = (unsigned char) *s2++;
> -      if (c1 == '\0' || c1 != c2)
> -	return c1 - c2;
> -      n--;
> +      op_t w2b;
> +
> +      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
> +	 does not contain end-of-string.  Therefore it is safe (and necessary)
> +	 to read another word from each while we do not have a difference.  */
> +      while (1)
> +	{
> +	  w2b = *x2++;
^^^^^^^^^^^^^^^^^^^^^^

reading ahead is wrong if w1 and w2 already mismatches.

> +	  w2 = MERGE (w2a, sh_1, w2b, sh_2);
> +	  if (n <= sizeof (op_t) || w1 != w2)
> +	    return final_cmp (w1, w2, n);
> +	  n -= sizeof(op_t);
> +	  if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
> +	    break;
> +	  w1 = *x1++;
> +	  w2a = w2b;
> +	}
> +
> +      /* Zero found in the second partial of P2.  If we had EOS in the aligned
> +	 word, we have equality.  */
> +      if (has_zero (w1))
> +	return 0;
> +
> +      /* Load the final word of P1 and align the final partial of P2.  */
> +      w1 = *x1++;
> +      w2 = MERGE (w2b, sh_1, 0, sh_2);
>      }
>  
> -  return c1 - c2;
> +  return final_cmp (w1, w2, n);

^ permalink raw reply	[flat|nested] 61+ messages in thread

* Re: [PATCH v12 08/31] string: Improve generic strncmp
  2023-02-21  9:28   ` Szabolcs Nagy
@ 2023-02-21 12:24     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 61+ messages in thread
From: Adhemerval Zanella Netto @ 2023-02-21 12:24 UTC (permalink / raw)
  To: Szabolcs Nagy
  Cc: libc-alpha, Richard Henderson, Jeff Law, Xi Ruoyao, Noah Goldstein



On 21/02/23 06:28, Szabolcs Nagy wrote:
> The 02/02/2023 15:11, Adhemerval Zanella via Libc-alpha wrote:
>> It follows the strategy:
>>
>>    - Align the first input to word boundary using byte operations.
>>
>>    - If second input is also word aligned, read a word per time, check
>>      for  null (using has_zero), and check final words using byte
>>      operation.
>>
>>    - If second input is not word aligned, loop by aligning the source,
>>      and merge the result of two reads.  Similar to aligned case, check
>>      for null with has_zero, and check final words using byte operation.
>>
>> Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64-linux-gnu,
>> and powerpc-linux-gnu by removing the arch-specific assembly
>> implementation and disabling multi-arch (it covers both LE and BE
>> for 64 and 32 bits).
> 
> on arm i see
> 
> FAIL: crypt/badsalttest
> 
> Program received signal SIGSEGV, Segmentation fault.
> strncmp_unaligned_loop (n=3, ofs=<optimized out>, w1=2371876, x2=0xf7feb000, x1=0xf7f6563c) at strncmp.c:85
> 85                w2b = *x2++;
> 
> this strncmp does out of bounds read:
> 
> Breakpoint 2, __GI_strncmp (p1=0xf7f65638 <md5_salt_prefix> "$1$", p2=p2@entry=0xf7feafff "*", n=n@entry=3) at strncmp.c:115
> 
> 0xf7feb000 is mapped PROT_NONE.
> 
>> +strncmp_unaligned_loop (const op_t *x1, const op_t *x2, op_t w1, uintptr_t ofs,
>> +			size_t n)
>> +{
>> +  op_t w2a = *x2++;
>> +  uintptr_t sh_1 = ofs * CHAR_BIT;
>> +  uintptr_t sh_2 = sizeof(op_t) * CHAR_BIT - sh_1;
>> +
>> +  op_t w2 = MERGE (w2a, sh_1, (op_t)-1, sh_2);
>> +  if (!has_zero (w2) && n > (sizeof (op_t) - ofs))
>>      {
>> -      c1 = (unsigned char) *s1++;
>> -      c2 = (unsigned char) *s2++;
>> -      if (c1 == '\0' || c1 != c2)
>> -	return c1 - c2;
>> -      n--;
>> +      op_t w2b;
>> +
>> +      /* Unaligned loop.  The invariant is that W2B, which is "ahead" of W1,
>> +	 does not contain end-of-string.  Therefore it is safe (and necessary)
>> +	 to read another word from each while we do not have a difference.  */
>> +      while (1)
>> +	{
>> +	  w2b = *x2++;
> ^^^^^^^^^^^^^^^^^^^^^^
> 
> reading ahead is wrong if w1 and w2 already mismatches.

Right, I will take a look.

> 
>> +	  w2 = MERGE (w2a, sh_1, w2b, sh_2);
>> +	  if (n <= sizeof (op_t) || w1 != w2)
>> +	    return final_cmp (w1, w2, n);
>> +	  n -= sizeof(op_t);
>> +	  if (has_zero (w2b) || n <= (sizeof (op_t) - ofs))
>> +	    break;
>> +	  w1 = *x1++;
>> +	  w2a = w2b;
>> +	}
>> +
>> +      /* Zero found in the second partial of P2.  If we had EOS in the aligned
>> +	 word, we have equality.  */
>> +      if (has_zero (w1))
>> +	return 0;
>> +
>> +      /* Load the final word of P1 and align the final partial of P2.  */
>> +      w1 = *x1++;
>> +      w2 = MERGE (w2b, sh_1, 0, sh_2);
>>      }
>>  
>> -  return c1 - c2;
>> +  return final_cmp (w1, w2, n);

^ permalink raw reply	[flat|nested] 61+ messages in thread

end of thread, other threads:[~2023-02-21 12:24 UTC | newest]

Thread overview: 61+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02 18:11 [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 01/31] Parameterize op_t from memcopy.h Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 02/31] Parameterize OP_T_THRES " Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 03/31] Add string vectorized find and detection functions Adhemerval Zanella
2023-02-03  0:24   ` Richard Henderson
2023-02-03 12:39     ` Adhemerval Zanella Netto
2023-02-03 13:40       ` Adhemerval Zanella Netto
2023-02-04  2:54         ` Richard Henderson
2023-02-02 18:11 ` [PATCH v12 04/31] string: Improve generic strlen Adhemerval Zanella
2023-02-03 23:23   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 05/31] string: Improve generic strchrnul Adhemerval Zanella
2023-02-03 23:23   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 06/31] string: Improve generic strchr Adhemerval Zanella
2023-02-03 23:24   ` Noah Goldstein
2023-02-04  2:58   ` Richard Henderson
2023-02-06 13:07     ` Adhemerval Zanella Netto
2023-02-02 18:11 ` [PATCH v12 07/31] string: Improve generic strcmp Adhemerval Zanella
2023-02-03 23:25   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 08/31] string: Improve generic strncmp Adhemerval Zanella
2023-02-03  0:34   ` Richard Henderson
2023-02-21  9:28   ` Szabolcs Nagy
2023-02-21 12:24     ` Adhemerval Zanella Netto
2023-02-02 18:11 ` [PATCH v12 09/31] string: Improve generic stpcpy Adhemerval Zanella
2023-02-03 23:26   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 10/31] string: Improve generic strcpy Adhemerval Zanella
2023-02-03 23:26   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 11/31] string: Improve generic memchr Adhemerval Zanella
2023-02-03 23:26   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 12/31] string: Improve generic strnlen with memchr Adhemerval Zanella
2023-02-03 23:27   ` Noah Goldstein
2023-02-02 18:11 ` [PATCH v12 13/31] string: Improve generic memrchr Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 14/31] string: Improve generic strrchr with memrchr and strlen Adhemerval Zanella
2023-02-04  3:06   ` Richard Henderson
2023-02-06 14:01     ` Adhemerval Zanella Netto
2023-02-02 18:11 ` [PATCH v12 15/31] hppa: Add memcopy.h Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 16/31] hppa: Add string-fza.h, string-fzc.h, and string-fzi.h Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 17/31] alpha: Add string-fza, string-fzb.h, string-fzi.h, and string-shift.h Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 18/31] arm: Add string-fza.h Adhemerval Zanella
2023-02-20 13:24   ` Szabolcs Nagy
2023-02-20 13:45     ` Szabolcs Nagy
2023-02-20 14:01       ` Adhemerval Zanella Netto
2023-02-20 16:12         ` Szabolcs Nagy
2023-02-02 18:11 ` [PATCH v12 19/31] powerpc: " Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 20/31] sh: Add string-fzb.h Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 21/31] riscv: Add string-fza.h and string-fzi.h Adhemerval Zanella
2023-02-03 19:47   ` Noah Goldstein
2023-02-06 16:14     ` Adhemerval Zanella Netto
2023-02-02 18:11 ` [PATCH v12 22/31] string: Hook up the default implementation on test-strlen Adhemerval Zanella
2023-02-03 23:30   ` Noah Goldstein
2023-02-06 17:36     ` Adhemerval Zanella Netto
2023-02-02 18:11 ` [PATCH v12 23/31] string: Hook up the default implementation on test-strnlen Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 24/31] string: Hook up the default implementation on test-strchr Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 25/31] string: Hook up the default implementation on test-strcmp Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 26/31] string: Hook up the default implementation on test-strncmp Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 27/31] string: Hook up the default implementation on test-stpcpy Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 28/31] string: Hook up the default implementation on test-strcpy Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 29/31] string: Hook up the default implementation on test-memchr Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 30/31] string: Hook up the default implementation on test-memrchr Adhemerval Zanella
2023-02-02 18:11 ` [PATCH v12 31/31] string: Hook up the default implementation on test-strrchr Adhemerval Zanella
2023-02-03  1:55   ` Richard Henderson
2023-02-02 18:17 ` [PATCH v12 00/31] Improve generic string routines Adhemerval Zanella Netto

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).