8ae002
From 1cf3bb5ec15f28245a6840b5b0443685c828a467 Mon Sep 17 00:00:00 2001
8ae002
From: "Paul E. Murphy" <murphyp@linux.vnet.ibm.com>
8ae002
Date: Mon, 14 Mar 2016 17:40:46 -0400
8ae002
Subject: [PATCH] powerpc: Add optimized P8 strspn
8ae002
8ae002
This utilizes vectors and bitmasks.  For small needle, large
8ae002
haystack, the performance improvement is upto 8x.  For short
8ae002
strings (0-4B), the cost of computing the bitmask dominates,
8ae002
and is a tad slower.
8ae002
8ae002
(cherry picked from commit 25dba0ad054723196fb633ba5d8a463ef5cb775c)
8ae002
---
8ae002
 ChangeLog                                          |  15 ++
8ae002
 sysdeps/powerpc/powerpc64/multiarch/Makefile       |   3 +-
8ae002
 .../powerpc/powerpc64/multiarch/ifunc-impl-list.c  |   8 +
8ae002
 .../powerpc/powerpc64/multiarch/strspn-power8.S    |  40 +++++
8ae002
 sysdeps/powerpc/powerpc64/multiarch/strspn-ppc64.c |  25 +++
8ae002
 sysdeps/powerpc/powerpc64/multiarch/strspn.c       |  35 ++++
8ae002
 sysdeps/powerpc/powerpc64/power8/strspn.S          | 179 +++++++++++++++++++++
8ae002
 7 files changed, 304 insertions(+), 1 deletion(-)
8ae002
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strspn-power8.S
8ae002
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strspn-ppc64.c
8ae002
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strspn.c
8ae002
 create mode 100644 sysdeps/powerpc/powerpc64/power8/strspn.S
8ae002
8ae002
diff --git a/ChangeLog b/ChangeLog
8ae002
index 496ef12..f030b68 100644
8ae002
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
8ae002
index 3b0e3a0..7ed56bf 100644
8ae002
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
8ae002
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
8ae002
@@ -19,6 +19,7 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
8ae002
                   strncpy-power8 strncpy-power7 strncpy-ppc64 \
8ae002
                   strncat-power7 \
8ae002
                   strstr-power7 strstr-ppc64 \
8ae002
+                  strspn-power8 strspn-ppc64 \
8ae002
                   rawmemchr-ppc64 strlen-power7 strlen-ppc64 strnlen-power7 \
8ae002
                   strnlen-ppc64 strcasecmp-power7 strcasecmp_l-power7 \
8ae002
                   strncase-power7 strncase_l-power7 \
8ae002
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
8ae002
index 364385b..f6c70ba 100644
8ae002
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
8ae002
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
8ae002
@@ -322,6 +322,14 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
8ae002
                 IFUNC_IMPL_ADD (array, i, strcat, 1,
8ae002
                                 __strcat_ppc))
8ae002
 
8ae002
+  /* Support sysdeps/powerpc/powerpc64/multiarch/strspn.c.  */
8ae002
+  IFUNC_IMPL (i, name, strspn,
8ae002
+             IFUNC_IMPL_ADD (array, i, strspn,
8ae002
+                             hwcap2 & PPC_FEATURE2_ARCH_2_07,
8ae002
+                             __strspn_power8)
8ae002
+             IFUNC_IMPL_ADD (array, i, strspn, 1,
8ae002
+                             __strspn_ppc))
8ae002
+
8ae002
   /* Support sysdeps/powerpc/powerpc64/multiarch/strstr.c.  */
8ae002
   IFUNC_IMPL (i, name, strstr,
8ae002
              IFUNC_IMPL_ADD (array, i, strstr,
8ae002
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strspn-power8.S b/sysdeps/powerpc/powerpc64/multiarch/strspn-power8.S
8ae002
new file mode 100644
8ae002
index 0000000..86a4e09
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/powerpc/powerpc64/multiarch/strspn-power8.S
8ae002
@@ -0,0 +1,40 @@
8ae002
+/* Optimized strspn implementation for POWER8.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <sysdep.h>
8ae002
+
8ae002
+#undef EALIGN
8ae002
+#define EALIGN(name, alignt, words)				\
8ae002
+  .section ".text";						\
8ae002
+  ENTRY_2(__strspn_power8)					\
8ae002
+  .align ALIGNARG(alignt);					\
8ae002
+  EALIGN_W_##words;						\
8ae002
+  BODY_LABEL(__strspn_power8):					\
8ae002
+  cfi_startproc;						\
8ae002
+  LOCALENTRY(__strspn_power8)
8ae002
+
8ae002
+#undef END
8ae002
+#define END(name)						\
8ae002
+  cfi_endproc;							\
8ae002
+  TRACEBACK(__strspn_power8)					\
8ae002
+  END_2(__strspn_power8)
8ae002
+
8ae002
+#undef libc_hidden_builtin_def
8ae002
+#define libc_hidden_builtin_def(name)
8ae002
+
8ae002
+#include <sysdeps/powerpc/powerpc64/power8/strspn.S>
8ae002
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strspn-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/strspn-ppc64.c
8ae002
new file mode 100644
8ae002
index 0000000..4c63665
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/powerpc/powerpc64/multiarch/strspn-ppc64.c
8ae002
@@ -0,0 +1,25 @@
8ae002
+/* Default strspn implementation for PowerPC64.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#define STRSPN __strspn_ppc
8ae002
+#ifdef SHARED
8ae002
+#undef libc_hidden_def
8ae002
+#define libc_hidden_def(name)
8ae002
+#endif
8ae002
+
8ae002
+#include <string/strspn.c>
8ae002
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strspn.c b/sysdeps/powerpc/powerpc64/multiarch/strspn.c
8ae002
new file mode 100644
8ae002
index 0000000..0e653f3
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/powerpc/powerpc64/multiarch/strspn.c
8ae002
@@ -0,0 +1,35 @@
8ae002
+/* Multiple versions of strspn. PowerPC64 version.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+# include <string.h>
8ae002
+# include <shlib-compat.h>
8ae002
+# include "init-arch.h"
8ae002
+
8ae002
+#undef strspn
8ae002
+extern __typeof (strspn) __libc_strspn;
8ae002
+
8ae002
+extern __typeof (strspn) __strspn_ppc attribute_hidden;
8ae002
+extern __typeof (strspn) __strspn_power8 attribute_hidden;
8ae002
+
8ae002
+libc_ifunc (__libc_strspn,
8ae002
+	    (hwcap2 & PPC_FEATURE2_ARCH_2_07)
8ae002
+	    ? __strspn_power8
8ae002
+	    : __strspn_ppc);
8ae002
+
8ae002
+weak_alias (__libc_strspn, strspn)
8ae002
+libc_hidden_builtin_def (strspn)
8ae002
diff --git a/sysdeps/powerpc/powerpc64/power8/strspn.S b/sysdeps/powerpc/powerpc64/power8/strspn.S
8ae002
new file mode 100644
8ae002
index 0000000..0dda437
8ae002
--- /dev/null
8ae002
+++ b/sysdeps/powerpc/powerpc64/power8/strspn.S
8ae002
@@ -0,0 +1,179 @@
8ae002
+/* Optimized strspn implementation for Power8.
8ae002
+
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public
8ae002
+   License as published by the Free Software Foundation; either
8ae002
+   version 2.1 of the License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; if not, see
8ae002
+   <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+/* size_t [r3] strspn (const char *string [r3],
8ae002
+                       const char *needleAccept [r4])  */
8ae002
+
8ae002
+/* This takes a novel approach by computing a 256 bit mask whereby
8ae002
+   each set bit implies the byte is "accepted".  P8 vector hardware
8ae002
+   has extremely efficient hardware for selecting bits from a mask.
8ae002
+
8ae002
+   One might ask "why not use bpermd for short strings"?  It is
8ae002
+   so slow that its performance about matches the generic PPC64
8ae002
+   variant without any fancy masking, with the added expense of
8ae002
+   making the mask.  That was the first variant of this.  */
8ae002
+
8ae002
+
8ae002
+
8ae002
+#include "sysdep.h"
8ae002
+
8ae002
+/* Simple macro to use VSX instructions in overlapping VR's.  */
8ae002
+#define XXVR(insn, vrt, vra, vrb) \
8ae002
+	insn 32+vrt, 32+vra, 32+vrb
8ae002
+
8ae002
+/* ISA 2.07B instructions are not all defined for older binutils.
8ae002
+   Macros are defined below for these newer instructions in order
8ae002
+   to maintain compatibility.  */
8ae002
+
8ae002
+/* Note, TX/SX is always set as VMX regs are the high 32 VSX regs.  */
8ae002
+#define MTVRD(v,r) .long (0x7c000167 | ((v)<<(32-11)) | ((r)<<(32-16)))
8ae002
+#define MFVRD(r,v) .long (0x7c000067 | ((v)<<(32-11)) | ((r)<<(32-16)))
8ae002
+
8ae002
+#define VBPERMQ(t,a,b) .long (0x1000054c \
8ae002
+			      | ((t)<<(32-11))	\
8ae002
+			      | ((a)<<(32-16))	\
8ae002
+			      | ((b)<<(32-21)) )
8ae002
+
8ae002
+	/* This can be updated to power8 once the minimum version of
8ae002
+	   binutils supports power8 and the above instructions.  */
8ae002
+	.machine power7
8ae002
+EALIGN(strspn, 4, 0)
8ae002
+	CALL_MCOUNT 2
8ae002
+
8ae002
+	/* Generate useful constants for later on.  */
8ae002
+	vspltisb v1, 7
8ae002
+	vspltisb v2, -1
8ae002
+	vslb	v1, v1, v1	/* 0x80 to swap high bit for vbpermq.  */
8ae002
+	vspltisb v10, 0
8ae002
+	vsldoi	v4, v10, v2, 2	/* 0xFFFF into vr4.  */
8ae002
+	XXVR(xxmrgld, v4, v4, v10) /* Mask for checking matches.  */
8ae002
+
8ae002
+	/* Prepare to compute 256b mask.  */
8ae002
+	addi	r4, r4, -1
8ae002
+	li	r5, 0
8ae002
+	li	r6, 0
8ae002
+	li	r7, 0
8ae002
+	li	r8, 0
8ae002
+	li	r11, 1
8ae002
+	sldi	r11, r11, 63
8ae002
+
8ae002
+	/* Start interleaved Mask computation.
8ae002
+	   This will eventually or 1's into ignored bits from vbpermq.  */
8ae002
+	lvsr	v11, 0, r3
8ae002
+	vspltb  v11, v11, 0	/* Splat shift constant.  */
8ae002
+
8ae002
+	/* Build a 256b mask in r5-r8.  */
8ae002
+	.align 4
8ae002
+L(next_needle):
8ae002
+	lbzu	r9, 1(r4)
8ae002
+
8ae002
+	cmpldi	cr0, r9, 0
8ae002
+	cmpldi	cr1, r9, 128
8ae002
+
8ae002
+	/* This is a little tricky.  srd only uses the first 7 bits,
8ae002
+	   and if bit 7 is set, value is always 0.  So, we can
8ae002
+	   effectively shift 128b in this case.  */
8ae002
+	xori	r12, r9,  0x40	/* Invert bit 6.  */
8ae002
+	srd	r10, r11, r9	/* Mask for bits 0-63.  */
8ae002
+	srd	r12, r11, r12	/* Mask for bits 64-127.  */
8ae002
+
8ae002
+	beq	cr0, L(start_cmp)
8ae002
+
8ae002
+	/* Now, or the value into the correct GPR.  */
8ae002
+	bge cr1,L(needle_gt128)
8ae002
+	or	r5, r5, r10	/* 0 - 63.  */
8ae002
+	or	r6, r6, r12	/* 64 - 127.  */
8ae002
+	b L(next_needle)
8ae002
+
8ae002
+	.align 4
8ae002
+L(needle_gt128):
8ae002
+	or	r7, r7, r10	/* 128 - 191.  */
8ae002
+	or	r8, r8, r12	/* 192 - 255.  */
8ae002
+	b L(next_needle)
8ae002
+
8ae002
+
8ae002
+	.align 4
8ae002
+L(start_cmp):
8ae002
+	/* Move and merge bitmap into 2 VRs.  bpermd is slower on P8.  */
8ae002
+	mr	r0, r3		/* Save r3 for final length computation.  */
8ae002
+	MTVRD (v5, r5)
8ae002
+	MTVRD (v6, r6)
8ae002
+	MTVRD (v7, r7)
8ae002
+	MTVRD (v8, r8)
8ae002
+
8ae002
+	/* Continue interleaved mask generation.  */
8ae002
+#ifdef __LITTLE_ENDIAN__
8ae002
+	vsrw	v11, v2, v11	/* Note, shift ignores higher order bits.  */
8ae002
+	vsplth  v11, v11, 0	/* Only care about the high 16 bits of v10.  */
8ae002
+#else
8ae002
+	vslw	v11, v2, v11	/* Note, shift ignores higher order bits.  */
8ae002
+	vsplth  v11, v11, 1	/* Only care about the low 16 bits of v10.  */
8ae002
+#endif
8ae002
+	lvx	v0, 0, r3	/* Note, unaligned load ignores lower bits.  */
8ae002
+
8ae002
+	/* Do the merging of the bitmask.  */
8ae002
+	XXVR(xxmrghd, v5, v5, v6)
8ae002
+	XXVR(xxmrghd, v6, v7, v8)
8ae002
+
8ae002
+	/* Finish mask generation.  */
8ae002
+	vand	v11, v11, v4	/* Throwaway bits not in the mask.  */
8ae002
+
8ae002
+	/* Compare the first 1-16B, while masking unwanted bytes.  */
8ae002
+	clrrdi  r3, r3, 4	/* Note,  counts from qw boundaries.  */
8ae002
+	vxor	v9, v0, v1	/* Swap high bit.  */
8ae002
+	VBPERMQ (v8, v5, v0)
8ae002
+	VBPERMQ (v7, v6, v9)
8ae002
+	vor	v7, v7, v8
8ae002
+	vor	v7, v7, v11	/* Ignore non-participating bytes.  */
8ae002
+	vcmpequh. v8, v7, v4
8ae002
+	bnl	cr6, L(done)
8ae002
+
8ae002
+	addi	r3, r3, 16
8ae002
+
8ae002
+	.align 4
8ae002
+L(vec):
8ae002
+	lvx	v0, 0, r3
8ae002
+	addi	r3, r3, 16
8ae002
+	vxor	v9, v0, v1	/* Swap high bit.  */
8ae002
+	VBPERMQ (v8, v5, v0)
8ae002
+	VBPERMQ (v7, v6, v9)
8ae002
+	vor	v7, v7, v8
8ae002
+	vcmpequh. v8, v7, v4
8ae002
+	blt	cr6, L(vec)
8ae002
+
8ae002
+	addi	r3, r3, -16
8ae002
+L(done):
8ae002
+	subf	r3, r0, r3
8ae002
+	MFVRD (r10, v7)
8ae002
+
8ae002
+#ifdef __LITTLE_ENDIAN__
8ae002
+	addi	r0,  r10, 1	/* Count the trailing 1's.  */
8ae002
+	andc	r10, r10, r0
8ae002
+	popcntd	r10, r10
8ae002
+#else
8ae002
+	xori	r10, r10, 0xffff /* Count leading 1's by inverting.  */
8ae002
+	addi	r3,  r3,  -48	/* Account for the extra leading zeros.  */
8ae002
+	cntlzd  r10, r10
8ae002
+#endif
8ae002
+
8ae002
+	add	r3, r3, r10
8ae002
+	blr
8ae002
+
8ae002
+END(strspn)
8ae002
+libc_hidden_builtin_def (strspn)
8ae002
-- 
8ae002
2.1.0
8ae002