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