ce426f
From 7cb28f3e21ff0c9658fad3d021e5a5548e1e49ae Mon Sep 17 00:00:00 2001
ce426f
From: Rajalakshmi Srinivasaraghavan <raji@linux.vnet.ibm.com>
ce426f
Date: Wed, 20 Apr 2016 23:10:42 +0530
ce426f
Subject: [PATCH] powerpc: strcasestr optmization for power8
ce426f
ce426f
This patch optimizes strcasestr function for power >= 8 systems.  The average
ce426f
improvement of this optimization is ~40% and compares 16 bytes at a time
ce426f
using vector instructions.  This patch is tested on powerpc64 and powerpc64le.
ce426f
ce426f
(cherry picked from commit e413b14e18ac635b5683ab7bbb1c901f79d1b06b)
ce426f
---
ce426f
 ChangeLog                                          |  15 +
ce426f
 sysdeps/powerpc/locale-defines.sym                 |   4 +
ce426f
 sysdeps/powerpc/powerpc64/multiarch/Makefile       |   3 +-
ce426f
 .../powerpc/powerpc64/multiarch/ifunc-impl-list.c  |   8 +
ce426f
 .../powerpc64/multiarch/strcasestr-power8.S        |  49 ++
ce426f
 .../powerpc/powerpc64/multiarch/strcasestr-ppc64.c |  34 ++
ce426f
 sysdeps/powerpc/powerpc64/multiarch/strcasestr.c   |  37 ++
ce426f
 sysdeps/powerpc/powerpc64/power8/Makefile          |   3 +
ce426f
 .../powerpc/powerpc64/power8/strcasestr-ppc64.c    |  29 ++
ce426f
 sysdeps/powerpc/powerpc64/power8/strcasestr.S      | 531 +++++++++++++++++++++
ce426f
 10 files changed, 712 insertions(+), 1 deletion(-)
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strcasestr-power8.S
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strcasestr-ppc64.c
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/multiarch/strcasestr.c
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/power8/Makefile
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/power8/strcasestr-ppc64.c
ce426f
 create mode 100644 sysdeps/powerpc/powerpc64/power8/strcasestr.S
ce426f
ce426f
diff --git a/ChangeLog b/ChangeLog
ce426f
index e7ea58a..6677ea2 100644
ce426f
diff --git a/sysdeps/powerpc/locale-defines.sym b/sysdeps/powerpc/locale-defines.sym
ce426f
index af64b92..5c5379c 100644
ce426f
--- a/sysdeps/powerpc/locale-defines.sym
ce426f
+++ b/sysdeps/powerpc/locale-defines.sym
ce426f
@@ -3,3 +3,7 @@
ce426f
 --
ce426f
 
ce426f
 LOCALE_CTYPE_TOLOWER	offsetof (struct __locale_struct, __ctype_tolower)
ce426f
+LOCALE_CTYPE_TOUPPER	offsetof (struct __locale_struct, __ctype_toupper)
ce426f
+_NL_CTYPE_NONASCII_CASE
ce426f
+LOCALE_DATA_VALUES	offsetof (struct __locale_data, values)
ce426f
+SIZEOF_VALUES		sizeof (((struct __locale_data *) 0)->values[0])
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
index 57abe8f..7f70ceb 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
ce426f
@@ -20,6 +20,7 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \
ce426f
                   mempcpy-power7 mempcpy-ppc64 memchr-power7 memchr-ppc64 \
ce426f
                   memrchr-power7 memrchr-ppc64 rawmemchr-power7 \
ce426f
                   stpcpy-power8 stpcpy-power7 stpcpy-ppc64 \
ce426f
+                  strcasestr-power8 strcasestr-ppc64 \
ce426f
                   strcat-power8 strcat-power7 strcat-ppc64 \
ce426f
                   strcmp-power8 strcmp-power7 strcmp-ppc64 \
ce426f
                   strcpy-power8 strcpy-power7 strcpy-ppc64 \
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
index 583885c..994e852 100644
ce426f
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
ce426f
@@ -341,6 +341,14 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
ce426f
              IFUNC_IMPL_ADD (array, i, strstr, 1,
ce426f
                              __strstr_ppc))
ce426f
ce426f
+  /* Support sysdeps/powerpc/powerpc64/multiarch/strcasestr.c.  */
ce426f
+  IFUNC_IMPL (i, name, strcasestr,
ce426f
+             IFUNC_IMPL_ADD (array, i, strcasestr,
ce426f
+                             hwcap2 & PPC_FEATURE2_ARCH_2_07,
ce426f
+                             __strcasestr_power8)
ce426f
+             IFUNC_IMPL_ADD (array, i, strcasestr, 1,
ce426f
+                             __strcasestr_ppc))
ce426f
+
ce426f
   /* Support sysdeps/powerpc/powerpc64/multiarch/wcschr.c.  */
ce426f
   IFUNC_IMPL (i, name, wcschr,
ce426f
              IFUNC_IMPL_ADD (array, i, wcschr,
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcasestr-power8.S b/sysdeps/powerpc/powerpc64/multiarch/strcasestr-power8.S
ce426f
new file mode 100644
ce426f
index 0000000..c77ff9f
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcasestr-power8.S
ce426f
@@ -0,0 +1,49 @@
ce426f
+/* Optimized strcasestr 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(__strcasestr_power8)					\
ce426f
+  .align ALIGNARG(alignt);					\
ce426f
+  EALIGN_W_##words;						\
ce426f
+  BODY_LABEL(__strcasestr_power8):				\
ce426f
+  cfi_startproc;						\
ce426f
+  LOCALENTRY(__strcasestr_power8)
ce426f
+
ce426f
+#undef END
ce426f
+#define END(name)						\
ce426f
+  cfi_endproc;							\
ce426f
+  TRACEBACK(__strcasestr_power8)				\
ce426f
+  END_2(__strcasestr_power8)
ce426f
+
ce426f
+#undef libc_hidden_builtin_def
ce426f
+#define libc_hidden_builtin_def(name)
ce426f
+
ce426f
+/* The following definitions are used in strcasestr optimization.  */
ce426f
+
ce426f
+/* strlen is used to calculate len of r4.  */
ce426f
+#define STRLEN __strlen_power8
ce426f
+/* strnlen is used to check if len of r3 is more than r4.  */
ce426f
+#define STRNLEN __strnlen_power7
ce426f
+/* strchr is used to check if first char of r4 is present in r3.  */
ce426f
+#define STRCHR __strchr_power7
ce426f
+
ce426f
+#include <sysdeps/powerpc/powerpc64/power8/strcasestr.S>
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcasestr-ppc64.c b/sysdeps/powerpc/powerpc64/multiarch/strcasestr-ppc64.c
ce426f
new file mode 100644
ce426f
index 0000000..7f7bb9e
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcasestr-ppc64.c
ce426f
@@ -0,0 +1,34 @@
ce426f
+/* PowerPC64 default implementation of strcasestr.
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
+
ce426f
+#define STRCASESTR  __strcasestr_ppc
ce426f
+#if IS_IN (libc) && defined(SHARED)
ce426f
+# undef libc_hidden_builtin_def
ce426f
+# define libc_hidden_builtin_def(name) \
ce426f
+  __hidden_ver1(__strcasestr_ppc, __GI_strcasestr, __strcasestr_ppc);
ce426f
+#endif
ce426f
+
ce426f
+
ce426f
+#undef weak_alias
ce426f
+#define weak_alias(a,b)
ce426f
+
ce426f
+extern __typeof (strcasestr) __strcasestr_ppc attribute_hidden;
ce426f
+
ce426f
+#include <string/strcasestr.c>
ce426f
diff --git a/sysdeps/powerpc/powerpc64/multiarch/strcasestr.c b/sysdeps/powerpc/powerpc64/multiarch/strcasestr.c
ce426f
new file mode 100644
ce426f
index 0000000..17ba188
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/multiarch/strcasestr.c
ce426f
@@ -0,0 +1,37 @@
ce426f
+/* Multiple versions of strcasestr.
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
+#if IS_IN (libc)
ce426f
+# include <string.h>
ce426f
+# include <shlib-compat.h>
ce426f
+# include "init-arch.h"
ce426f
+
ce426f
+extern __typeof (__strcasestr) __strcasestr_ppc attribute_hidden;
ce426f
+extern __typeof (__strcasestr) __strcasestr_power8 attribute_hidden;
ce426f
+
ce426f
+/* Avoid DWARF definition DIE on ifunc symbol so that GDB can handle
ce426f
+   ifunc symbol properly.  */
ce426f
+libc_ifunc (__strcasestr,
ce426f
+		(hwcap2 & PPC_FEATURE2_ARCH_2_07)
ce426f
+		? __strcasestr_power8
ce426f
+		: __strcasestr_ppc);
ce426f
+
ce426f
+weak_alias (__strcasestr, strcasestr)
ce426f
+#else
ce426f
+#include <string/strcasestr.c>
ce426f
+#endif
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power8/Makefile b/sysdeps/powerpc/powerpc64/power8/Makefile
ce426f
new file mode 100644
ce426f
index 0000000..71a5952
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/power8/Makefile
ce426f
@@ -0,0 +1,3 @@
ce426f
+ifeq ($(subdir),string)
ce426f
+sysdep_routines += strcasestr-ppc64
ce426f
+endif
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power8/strcasestr-ppc64.c b/sysdeps/powerpc/powerpc64/power8/strcasestr-ppc64.c
ce426f
new file mode 100644
ce426f
index 0000000..09a07b0
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/power8/strcasestr-ppc64.c
ce426f
@@ -0,0 +1,29 @@
ce426f
+/* Optimized strcasestr implementation for PowerPC64/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 <string.h>
ce426f
+
ce426f
+#define STRCASESTR __strcasestr_ppc
ce426f
+#undef libc_hidden_builtin_def
ce426f
+#define libc_hidden_builtin_def(__name)
ce426f
+
ce426f
+#undef weak_alias
ce426f
+#define weak_alias(a,b)
ce426f
+extern __typeof (strcasestr) __strcasestr_ppc attribute_hidden;
ce426f
+
ce426f
+#include <string/strcasestr.c>
ce426f
diff --git a/sysdeps/powerpc/powerpc64/power8/strcasestr.S b/sysdeps/powerpc/powerpc64/power8/strcasestr.S
ce426f
new file mode 100644
ce426f
index 0000000..24b2b76
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/powerpc/powerpc64/power8/strcasestr.S
ce426f
@@ -0,0 +1,531 @@
ce426f
+/* Optimized strcasestr implementation for PowerPC64/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
+#include <locale-defines.h>
ce426f
+
ce426f
+/* Char * [r3] strcasestr (char *s [r3], char * pat[r4])  */
ce426f
+
ce426f
+/* The performance gain is obtained by comparing 16 bytes.  */
ce426f
+
ce426f
+/* When the first char of r4 is hit ITERATIONS times in r3
ce426f
+   fallback to default.  */
ce426f
+#define ITERATIONS	64
ce426f
+
ce426f
+#ifndef STRLEN
ce426f
+/* For builds without IFUNC support, local calls should be made to internal
ce426f
+   GLIBC symbol (created by libc_hidden_builtin_def).  */
ce426f
+# ifdef SHARED
ce426f
+#  define STRLEN   __GI_strlen
ce426f
+# else
ce426f
+#  define STRLEN   strlen
ce426f
+# endif
ce426f
+#endif
ce426f
+
ce426f
+#ifndef STRNLEN
ce426f
+/* For builds without IFUNC support, local calls should be made to internal
ce426f
+   GLIBC symbol (created by libc_hidden_builtin_def).  */
ce426f
+# ifdef SHARED
ce426f
+#  define STRNLEN   __GI_strnlen
ce426f
+# else
ce426f
+#  define STRNLEN    __strnlen
ce426f
+# endif
ce426f
+#endif
ce426f
+
ce426f
+#ifndef STRCHR
ce426f
+# ifdef SHARED
ce426f
+#  define STRCHR   __GI_strchr
ce426f
+# else
ce426f
+#  define STRCHR   strchr
ce426f
+# endif
ce426f
+#endif
ce426f
+
ce426f
+/* Convert 16 bytes of v4 and reg to lowercase and compare.  */
ce426f
+#define TOLOWER(reg)     \
ce426f
+	vcmpgtub	v6, v4, v1; \
ce426f
+	vcmpgtub	v7, v2, v4; \
ce426f
+	vand	v8, v7, v6; \
ce426f
+	vand	v8, v8, v3; \
ce426f
+	vor	v4, v8, v4; \
ce426f
+	vcmpgtub	v6, reg, v1; \
ce426f
+	vcmpgtub	v7, v2, reg; \
ce426f
+	vand	v8, v7, v6; \
ce426f
+	vand	v8, v8, v3; \
ce426f
+	vor	reg, v8, reg; \
ce426f
+	vcmpequb.	v6, reg, v4;
ce426f
+
ce426f
+/* TODO: change these to the actual instructions when the minimum required
ce426f
+   binutils allows it.  */
ce426f
+#ifdef _ARCH_PWR8
ce426f
+#define VCLZD_V8_v7	vclzd	v8, v7;
ce426f
+#else
ce426f
+#define VCLZD_V8_v7	.long	0x11003fc2
ce426f
+#endif
ce426f
+
ce426f
+#define	FRAMESIZE	(FRAME_MIN_SIZE+48)
ce426f
+/* TODO: change this to .machine power8 when the minimum required binutils
ce426f
+   allows it.  */
ce426f
+	.machine  power7
ce426f
+EALIGN (strcasestr, 4, 0)
ce426f
+	CALL_MCOUNT 2
ce426f
+	mflr	r0			/* Load link register LR to r0.  */
ce426f
+	std	r31, -8(r1)		/* Save callers register r31.  */
ce426f
+	std	r30, -16(r1)		/* Save callers register r30.  */
ce426f
+	std	r29, -24(r1)		/* Save callers register r29.  */
ce426f
+	std	r28, -32(r1)		/* Save callers register r28.  */
ce426f
+	std	r27, -40(r1)		/* Save callers register r27.  */
ce426f
+	std	r0, 16(r1)		/* Store the link register.  */
ce426f
+	cfi_offset(r31, -8)
ce426f
+	cfi_offset(r30, -16)
ce426f
+	cfi_offset(r29, -24)
ce426f
+	cfi_offset(r28, -32)
ce426f
+	cfi_offset(r27, -40)
ce426f
+	cfi_offset(lr, 16)
ce426f
+	stdu	r1, -FRAMESIZE(r1)	/* Create the stack frame.  */
ce426f
+	cfi_adjust_cfa_offset(FRAMESIZE)
ce426f
+
ce426f
+	dcbt	0, r3
ce426f
+	dcbt	0, r4
ce426f
+	cmpdi	cr7, r3, 0		/* Input validation.  */
ce426f
+	beq	cr7, L(retnull)
ce426f
+	cmpdi	cr7, r4, 0
ce426f
+	beq	cr7, L(retnull)
ce426f
+
ce426f
+	mr	r29, r3
ce426f
+	mr	r30, r4
ce426f
+	/* Load first byte from r4 and check if its null.  */
ce426f
+	lbz	r6, 0(r4)
ce426f
+	cmpdi	cr7, r6, 0
ce426f
+	beq	cr7, L(ret_r3)
ce426f
+
ce426f
+	ld	r10, __libc_tsd_LOCALE@got@tprel(r2)
ce426f
+	add	r9, r10, __libc_tsd_LOCALE@tls
ce426f
+	ld	r9, 0(r9)
ce426f
+	ld	r9, LOCALE_CTYPE_TOUPPER(r9)
ce426f
+	sldi	r10, r6, 2		/* Convert to upper case.  */
ce426f
+	lwzx	r28, r9, r10
ce426f
+
ce426f
+	ld	r10, __libc_tsd_LOCALE@got@tprel(r2)
ce426f
+	add	r11, r10, __libc_tsd_LOCALE@tls
ce426f
+	ld	r11, 0(r11)
ce426f
+	ld	r11, LOCALE_CTYPE_TOLOWER(r11)
ce426f
+	sldi	r10, r6, 2              /* Convert to lower case.  */
ce426f
+	lwzx	r27, r11, r10
ce426f
+
ce426f
+	/* Check if the first char is present.  */
ce426f
+	mr	r4, r27
ce426f
+	bl	STRCHR
ce426f
+	nop
ce426f
+	mr	r5, r3
ce426f
+	mr	r3, r29
ce426f
+	mr	r29, r5
ce426f
+	mr	r4, r28
ce426f
+	bl	STRCHR
ce426f
+	nop
ce426f
+	cmpdi	cr7, r29, 0
ce426f
+	beq	cr7, L(firstpos)
ce426f
+	cmpdi	cr7, r3, 0
ce426f
+	beq	cr7, L(skipcheck)
ce426f
+	cmpw	cr7, r3, r29
ce426f
+	ble 	cr7, L(firstpos)
ce426f
+	/* Move r3 to the first occurence.  */
ce426f
+L(skipcheck):
ce426f
+	mr	r3, r29
ce426f
+L(firstpos):
ce426f
+	mr	r29, r3
ce426f
+
ce426f
+	sldi	r9, r27, 8
ce426f
+	or	r28, r9, r28
ce426f
+	/* Reg r27 is used to count the number of iterations.  */
ce426f
+	li	r27, 0
ce426f
+	/* If first char of search str is not present.  */
ce426f
+	cmpdi	cr7, r3, 0
ce426f
+	ble	cr7, L(end)
ce426f
+
ce426f
+	/* Find the length of pattern.  */
ce426f
+	mr	r3, r30
ce426f
+	bl	STRLEN
ce426f
+	nop
ce426f
+
ce426f
+	cmpdi	cr7, r3, 0	/* If search str is null.  */
ce426f
+	beq	cr7, L(ret_r3)
ce426f
+
ce426f
+	mr	r31, r3
ce426f
+	mr	r4, r3
ce426f
+	mr	r3, r29
ce426f
+	bl	STRNLEN
ce426f
+	nop
ce426f
+
ce426f
+	cmpd	cr7, r3, r31 	/* If len(r3) < len(r4).  */
ce426f
+	blt	cr7, L(retnull)
ce426f
+
ce426f
+	mr	r3, r29
ce426f
+
ce426f
+	/* Locales not matching ASCII for single bytes.  */
ce426f
+	ld	r10, __libc_tsd_LOCALE@got@tprel(r2)
ce426f
+	add	r9, r10, __libc_tsd_LOCALE@tls
ce426f
+	ld	r9, 0(r9)
ce426f
+	ld	r7, 0(r9)
ce426f
+	addi	r7, r7, LOCALE_DATA_VALUES+_NL_CTYPE_NONASCII_CASE*SIZEOF_VALUES
ce426f
+	lwz	r8, 0(r7)
ce426f
+	cmpdi	cr7, r8, 1
ce426f
+	beq	cr7, L(bytebybyte)
ce426f
+
ce426f
+	/* If len(r4) < 16 handle byte by byte.  */
ce426f
+	/* For shorter strings we will not use vector registers.  */
ce426f
+	cmpdi	cr7, r31, 16
ce426f
+	blt	cr7, L(bytebybyte)
ce426f
+
ce426f
+	/* Comparison values used for TOLOWER.  */
ce426f
+	/* Load v1 = 64('A' - 1), v2 = 91('Z' + 1), v3 = 32 in each byte.  */
ce426f
+	vspltish	v0, 0
ce426f
+	vspltisb	v5, 2
ce426f
+	vspltisb	v4, 4
ce426f
+	vsl	v3, v5, v4
ce426f
+	vaddubm	v1, v3, v3
ce426f
+	vspltisb	v5, 15
ce426f
+	vaddubm	v2, v5, v5
ce426f
+	vaddubm	v2, v1, v2
ce426f
+	vspltisb	v4, -3
ce426f
+	vaddubm	v2, v2, v4
ce426f
+
ce426f
+	/*
ce426f
+	1. Load 16 bytes from r3 and r4
ce426f
+	2. Check if there is null, If yes, proceed byte by byte path.
ce426f
+	3. Else,Convert both to lowercase and compare.
ce426f
+	4. If they are same proceed to 1.
ce426f
+	5. If they dont match, find if first char of r4 is present in the
ce426f
+	   loaded 16 byte of r3.
ce426f
+	6. If yes, move position, load next 16 bytes of r3 and proceed to 2.
ce426f
+	*/
ce426f
+
ce426f
+	mr	r8, r3		/* Save r3 for future use.  */
ce426f
+	mr	r4, r30		/* Restore r4.  */
ce426f
+	clrldi	r10, r4, 60
ce426f
+	lvx	v5, 0, r4	/* Load 16 bytes from r4.  */
ce426f
+	cmpdi	cr7, r10, 0
ce426f
+	beq	cr7, L(begin2)
ce426f
+	/* If r4 is unaligned, load another 16 bytes.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lvsr	v7, 0, r4
ce426f
+#else
ce426f
+	lvsl	v7, 0, r4
ce426f
+#endif
ce426f
+	addi	r5, r4, 16
ce426f
+	lvx	v9, 0, r5
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vperm	v5, v9, v5, v7
ce426f
+#else
ce426f
+	vperm	v5, v5, v9, v7
ce426f
+#endif
ce426f
+L(begin2):
ce426f
+	lvx	v4, 0, r3
ce426f
+	vcmpequb.	v7, v0, v4	/* Check for null.  */
ce426f
+	beq	cr6, L(nullchk6)
ce426f
+	b	L(trailcheck)
ce426f
+
ce426f
+        .align  4
ce426f
+L(nullchk6):
ce426f
+	clrldi	r10, r3, 60
ce426f
+	cmpdi	cr7, r10, 0
ce426f
+	beq	cr7, L(next16)
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lvsr	v7, 0, r3
ce426f
+#else
ce426f
+	lvsl	v7, 0, r3
ce426f
+#endif
ce426f
+	addi	r5, r3, 16
ce426f
+	/* If r3 is unaligned, load another 16 bytes.  */
ce426f
+	lvx	v10, 0, r5
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vperm	v4, v10, v4, v7
ce426f
+#else
ce426f
+	vperm	v4, v4, v10, v7
ce426f
+#endif
ce426f
+L(next16):
ce426f
+	vcmpequb.	v6, v0, v5	/* Check for null.  */
ce426f
+	beq	cr6, L(nullchk)
ce426f
+	b	L(trailcheck)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk):
ce426f
+	vcmpequb.	v6, v0, v4
ce426f
+	beq	cr6, L(nullchk1)
ce426f
+	b	L(retnull)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk1):
ce426f
+	/* Convert both v3 and v4 to lower.  */
ce426f
+	TOLOWER(v5)
ce426f
+	/* If both are same, branch to match.  */
ce426f
+	blt	cr6, L(match)
ce426f
+	/* Find if the first char is present in next 15 bytes.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vspltb	v6, v5, 15
ce426f
+	vsldoi	v7, v0, v4, 15
ce426f
+#else
ce426f
+	vspltb	v6, v5, 0
ce426f
+	vspltisb	v7, 8
ce426f
+	vslo	v7, v4, v7
ce426f
+#endif
ce426f
+	vcmpequb	v7, v6, v7
ce426f
+	vcmpequb.	v6, v0, v7
ce426f
+	/* Shift r3 by 16 bytes and proceed.  */
ce426f
+	blt	cr6, L(shift16)
ce426f
+	VCLZD_V8_v7
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vspltb	v6, v8, 15
ce426f
+#else
ce426f
+	vspltb	v6, v8, 7
ce426f
+#endif
ce426f
+	vcmpequb.	v6, v6, v1
ce426f
+	/* Shift r3 by 8  bytes and proceed.  */
ce426f
+	blt	cr6, L(shift8)
ce426f
+	b	L(begin)
ce426f
+
ce426f
+	.align	4
ce426f
+L(match):
ce426f
+	/* There is a match of 16 bytes, check next bytes.  */
ce426f
+	cmpdi	cr7, r31, 16
ce426f
+	mr	r29, r3
ce426f
+	beq	cr7, L(ret_r3)
ce426f
+
ce426f
+L(secondmatch):
ce426f
+	addi	r3, r3, 16
ce426f
+	addi	r4, r4, 16
ce426f
+	/* Load next 16 bytes of r3 and r4 and compare.  */
ce426f
+	clrldi	r10, r4, 60
ce426f
+	cmpdi	cr7, r10, 0
ce426f
+	beq	cr7, L(nextload)
ce426f
+	/* Handle unaligned case.  */
ce426f
+	vor	v6, v9, v9
ce426f
+	vcmpequb.	v7, v0, v6
ce426f
+	beq	cr6, L(nullchk2)
ce426f
+	b	L(trailcheck)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk2):
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lvsr	v7, 0, r4
ce426f
+#else
ce426f
+	lvsl	v7, 0, r4
ce426f
+#endif
ce426f
+	addi	r5, r4, 16
ce426f
+	/* If r4 is unaligned, load another 16 bytes.  */
ce426f
+	lvx	v9, 0, r5
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vperm	v11, v9, v6, v7
ce426f
+#else
ce426f
+	vperm	v11, v6, v9, v7
ce426f
+#endif
ce426f
+	b	L(compare)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nextload):
ce426f
+	lvx	v11, 0, r4
ce426f
+L(compare):
ce426f
+	vcmpequb.	v7, v0, v11
ce426f
+	beq	cr6, L(nullchk3)
ce426f
+	b	L(trailcheck)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk3):
ce426f
+	clrldi	r10, r3, 60
ce426f
+	cmpdi 	cr7, r10, 0
ce426f
+	beq 	cr7, L(nextload1)
ce426f
+	/* Handle unaligned case.  */
ce426f
+	vor	v4, v10, v10
ce426f
+	vcmpequb.	v7, v0, v4
ce426f
+	beq	cr6, L(nullchk4)
ce426f
+	b	L(retnull)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk4):
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lvsr	v7, 0, r3
ce426f
+#else
ce426f
+	lvsl	v7, 0, r3
ce426f
+#endif
ce426f
+	addi	r5, r3, 16
ce426f
+	/* If r3 is unaligned, load another 16 bytes.  */
ce426f
+	lvx	v10, 0, r5
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	vperm	v4, v10, v4, v7
ce426f
+#else
ce426f
+	vperm	v4, v4, v10, v7
ce426f
+#endif
ce426f
+	b	L(compare1)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nextload1):
ce426f
+	lvx	v4, 0, r3
ce426f
+L(compare1):
ce426f
+	vcmpequb.	v7, v0, v4
ce426f
+	beq	cr6, L(nullchk5)
ce426f
+	b	L(retnull)
ce426f
+
ce426f
+	.align	4
ce426f
+L(nullchk5):
ce426f
+	/* Convert both v3 and v4 to lower.  */
ce426f
+	TOLOWER(v11)
ce426f
+	/* If both are same, branch to secondmatch.  */
ce426f
+	blt 	cr6, L(secondmatch)
ce426f
+	/* Continue the search.  */
ce426f
+        b	L(begin)
ce426f
+
ce426f
+	.align	4
ce426f
+L(trailcheck):
ce426f
+	ld	r10, __libc_tsd_LOCALE@got@tprel(r2)
ce426f
+	add	r11, r10, __libc_tsd_LOCALE@tls
ce426f
+	ld	r11, 0(r11)
ce426f
+	ld	r11, LOCALE_CTYPE_TOLOWER(r11)
ce426f
+L(loop2):
ce426f
+	lbz	r5, 0(r3)               /* Load byte from r3.  */
ce426f
+	lbz	r6, 0(r4)               /* Load next byte from r4.  */
ce426f
+	cmpdi 	cr7, r6, 0              /* Is it null?  */
ce426f
+	beq 	cr7, L(updater3)
ce426f
+	cmpdi 	cr7, r5, 0              /* Is it null?  */
ce426f
+	beq 	cr7, L(retnull)         /* If yes, return.  */
ce426f
+	addi	r3, r3, 1
ce426f
+	addi	r4, r4, 1               /* Increment r4.  */
ce426f
+	sldi	r10, r5, 2              /* Convert to lower case.  */
ce426f
+	lwzx	r10, r11, r10
ce426f
+	sldi	r7, r6, 2               /* Convert to lower case.  */
ce426f
+	lwzx	r7, r11, r7
ce426f
+	cmpw	cr7, r7, r10            /* Compare with byte from r4.  */
ce426f
+	bne	cr7, L(begin)
ce426f
+	b	L(loop2)
ce426f
+
ce426f
+	.align	4
ce426f
+L(shift8):
ce426f
+	addi	r8, r8, 7
ce426f
+	b	L(begin)
ce426f
+	.align	4
ce426f
+L(shift16):
ce426f
+	addi	r8, r8, 15
ce426f
+	.align	4
ce426f
+L(begin):
ce426f
+	addi	r8, r8, 1
ce426f
+	mr	r3, r8
ce426f
+	/* When our iterations exceed ITERATIONS,fall back to default.  */
ce426f
+	addi	r27, r27, 1
ce426f
+	cmpdi	cr7, r27, ITERATIONS
ce426f
+	beq	cr7, L(default)
ce426f
+	mr	r4, r30         /* Restore r4.  */
ce426f
+	b	L(begin2)
ce426f
+
ce426f
+	/* Handling byte by byte.  */
ce426f
+	.align	4
ce426f
+L(loop1):
ce426f
+	mr	r3, r8
ce426f
+	addi	r27, r27, 1
ce426f
+	cmpdi	cr7, r27, ITERATIONS
ce426f
+	beq	cr7, L(default)
ce426f
+	mr	r29, r8
ce426f
+	srdi	r4, r28, 8
ce426f
+	/* Check if the first char is present.  */
ce426f
+	bl	STRCHR
ce426f
+	nop
ce426f
+	mr	r5, r3
ce426f
+	mr	r3, r29
ce426f
+	mr	r29, r5
ce426f
+	sldi	r4, r28, 56
ce426f
+	srdi	r4, r4, 56
ce426f
+	bl	STRCHR
ce426f
+	nop
ce426f
+	cmpdi	cr7, r29, 0
ce426f
+	beq	cr7, L(nextpos)
ce426f
+	cmpdi	cr7, r3, 0
ce426f
+	beq	cr7, L(skipcheck1)
ce426f
+	cmpw	cr7, r3, r29
ce426f
+	ble 	cr7, L(nextpos)
ce426f
+	/* Move r3 to first occurence.  */
ce426f
+L(skipcheck1):
ce426f
+	mr	r3, r29
ce426f
+L(nextpos):
ce426f
+	mr	r29, r3
ce426f
+	cmpdi 	cr7, r3, 0
ce426f
+	ble 	cr7, L(retnull)
ce426f
+L(bytebybyte):
ce426f
+	ld	r10, __libc_tsd_LOCALE@got@tprel(r2)
ce426f
+	add	r11, r10, __libc_tsd_LOCALE@tls
ce426f
+	ld	r11, 0(r11)
ce426f
+	ld	r11, LOCALE_CTYPE_TOLOWER(r11)
ce426f
+	mr	r4, r30                 /* Restore r4.  */
ce426f
+	mr	r8, r3                  /* Save r3.  */
ce426f
+	addi	r8, r8, 1
ce426f
+
ce426f
+L(loop):
ce426f
+	addi	r3, r3, 1
ce426f
+	lbz	r5, 0(r3)               /* Load byte from r3.  */
ce426f
+	addi	r4, r4, 1               /* Increment r4.  */
ce426f
+	lbz	r6, 0(r4)               /* Load next byte from r4.  */
ce426f
+	cmpdi 	cr7, r6, 0              /* Is it null?  */
ce426f
+	beq 	cr7, L(updater3)
ce426f
+	cmpdi 	cr7, r5, 0              /* Is it null?  */
ce426f
+	beq 	cr7, L(retnull)         /* If yes, return.  */
ce426f
+	sldi	r10, r5, 2              /* Convert to lower case.  */
ce426f
+	lwzx	r10, r11, r10
ce426f
+	sldi	r7, r6, 2               /* Convert to lower case.  */
ce426f
+	lwzx	r7, r11, r7
ce426f
+	cmpw	cr7, r7, r10            /* Compare with byte from r4.  */
ce426f
+	bne 	cr7, L(loop1)
ce426f
+	b	L(loop)
ce426f
+
ce426f
+	/* Handling return values.  */
ce426f
+	.align	4
ce426f
+L(updater3):
ce426f
+	subf	r3, r31, r3	/* Reduce r31 (len of r4) from r3.  */
ce426f
+	b	L(end)
ce426f
+
ce426f
+	.align	4
ce426f
+L(ret_r3):
ce426f
+	mr	r3, r29		/* Return point of match.  */
ce426f
+	b	L(end)
ce426f
+
ce426f
+	.align	4
ce426f
+L(retnull):
ce426f
+	li	r3, 0		/* Substring was not found.  */
ce426f
+	b	L(end)
ce426f
+
ce426f
+	.align	4
ce426f
+L(default):
ce426f
+	mr	r4, r30
ce426f
+	bl	__strcasestr_ppc
ce426f
+	nop
ce426f
+
ce426f
+	.align	4
ce426f
+L(end):
ce426f
+	addi	r1, r1, FRAMESIZE	/* Restore stack pointer.  */
ce426f
+	cfi_adjust_cfa_offset(-FRAMESIZE)
ce426f
+	ld	r0, 16(r1)	/* Restore the saved link register.  */
ce426f
+	ld	r27, -40(r1)
ce426f
+	ld	r28, -32(r1)
ce426f
+	ld	r29, -24(r1)	/* Restore callers save register r29.  */
ce426f
+	ld	r30, -16(r1)	/* Restore callers save register r30.  */
ce426f
+	ld	r31, -8(r1)	/* Restore callers save register r31.  */
ce426f
+	cfi_restore(lr)
ce426f
+	cfi_restore(r27)
ce426f
+	cfi_restore(r28)
ce426f
+	cfi_restore(r29)
ce426f
+	cfi_restore(r30)
ce426f
+	cfi_restore(r31)
ce426f
+	mtlr	r0		/* Branch to link register.  */
ce426f
+	blr
ce426f
+END (strcasestr)
ce426f
+libc_hidden_builtin_def (strcasestr)
ce426f
-- 
ce426f
2.1.0
ce426f