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