ce426f
# commit 466b03933234017473c12dd1d92bda5e7fe49df7
ce426f
# Author: Alan Modra <amodra@gmail.com>
ce426f
# Date:   Sat Aug 17 18:48:36 2013 +0930
ce426f
# 
ce426f
#     PowerPC LE memchr and memrchr
ce426f
#     http://sourceware.org/ml/libc-alpha/2013-08/msg00105.html
ce426f
#     
ce426f
#     Like strnlen, memchr and memrchr had a number of defects fixed by this
ce426f
#     patch as well as adding little-endian support.  The first one I
ce426f
#     noticed was that the entry to the main loop needlessly checked for
ce426f
#     "are we done yet?" when we know the size is large enough that we can't
ce426f
#     be done.  The second defect I noticed was that the main loop count was
ce426f
#     wrong, which in turn meant that the small loop needed to handle an
ce426f
#     extra word.  Thirdly, there is nothing to say that the string can't
ce426f
#     wrap around zero, except of course that we'd normally hit a segfault
ce426f
#     on trying to read from address zero.  Fixing that simplified a number
ce426f
#     of places:
ce426f
#     
ce426f
#     -   /* Are we done already?  */
ce426f
#     -   addi    r9,r8,8
ce426f
#     -   cmpld   r9,r7
ce426f
#     -   bge     L(null)
ce426f
#     
ce426f
#     becomes
ce426f
#     
ce426f
#     +   cmpld   r8,r7
ce426f
#     +   beqlr
ce426f
#     
ce426f
#     However, the exit gets an extra test because I test for being on the
ce426f
#     last word then if so whether the byte offset is less than the end.
ce426f
#     Overall, the change is a win.
ce426f
#     
ce426f
#     Lastly, memrchr used the wrong cache hint.
ce426f
#     
ce426f
#         * sysdeps/powerpc/powerpc64/power7/memchr.S: Replace rlwimi with
ce426f
#         insrdi.  Make better use of reg selection to speed exit slightly.
ce426f
#         Schedule entry path a little better.  Remove useless "are we done"
ce426f
#         checks on entry to main loop.  Handle wrapping around zero address.
ce426f
#         Correct main loop count.  Handle single left-over word from main
ce426f
#         loop inline rather than by using loop_small.  Remove extra word
ce426f
#         case in loop_small caused by wrong loop count.  Add little-endian
ce426f
#         support.
ce426f
#         * sysdeps/powerpc/powerpc32/power7/memchr.S: Likewise.
ce426f
#         * sysdeps/powerpc/powerpc64/power7/memrchr.S: Likewise.  Use proper
ce426f
#         cache hint.
ce426f
#         * sysdeps/powerpc/powerpc32/power7/memrchr.S: Likewise.
ce426f
#         * sysdeps/powerpc/powerpc64/power7/rawmemchr.S: Add little-endian
ce426f
#         support.  Avoid rlwimi.
ce426f
#         * sysdeps/powerpc/powerpc32/power7/rawmemchr.S: Likewise.
ce426f
# 
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memchr.S	2014-05-29 13:13:37.000000000 -0500
ce426f
@@ -1,5 +1,5 @@
ce426f
 /* Optimized memchr implementation for PowerPC32/POWER7 using cmpb insn.
ce426f
-   Copyright (C) 2010-2012 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2010-2014 Free Software Foundation, Inc.
ce426f
    Contributed by Luis Machado <luisgpm@br.ibm.com>.
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -18,116 +18,118 @@
ce426f
    <http://www.gnu.org/licenses/>.  */
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
-#include <bp-sym.h>
ce426f
-#include <bp-asm.h>
ce426f
 
ce426f
 /* int [r3] memchr (char *s [r3], int byte [r4], int size [r5])  */
ce426f
 	.machine  power7
ce426f
-ENTRY (BP_SYM (__memchr))
ce426f
+ENTRY (__memchr)
ce426f
 	CALL_MCOUNT
ce426f
 	dcbt	0,r3
ce426f
 	clrrwi  r8,r3,2
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
+	insrwi	r4,r4,8,16    /* Replicate byte to word.  */
ce426f
 	add	r7,r3,r5      /* Calculate the last acceptable address.  */
ce426f
+	insrwi	r4,r4,16,0
ce426f
 	cmplwi	r5,16
ce426f
+	li	r9, -1
ce426f
+	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
+	addi	r7,r7,-1
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	slw	r9,r9,r6
ce426f
+#else
ce426f
+	srw	r9,r9,r6
ce426f
+#endif
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	cmplw	cr7,r3,r7     /* Compare the starting address (r3) with the
ce426f
-				 ending address (r7).  If (r3 >= r7), the size
ce426f
-				 passed in is zero or negative.  */
ce426f
-	ble	cr7,L(proceed)
ce426f
-
ce426f
-	li	r7,-1	      /* Artificially set our ending address (r7)
ce426f
-				 such that we will exit early. */
ce426f
-L(proceed):
ce426f
-	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
-	cmpli	cr6,r6,0      /* cr6 == Do we have padding?  */
ce426f
 	lwz	r12,0(r8)     /* Load word from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE's in WORD1.  */
ce426f
-	beq	cr6,L(proceed_no_padding)
ce426f
-	slw	r10,r10,r6
ce426f
-	srw	r10,r10,r6
ce426f
-L(proceed_no_padding):
ce426f
-	cmplwi	cr7,r10,0     /* If r10 == 0, no BYTEs have been found.  */
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTEs in WORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	clrlwi	r5,r7,30      /* Byte count - 1 in last word.  */
ce426f
+	clrrwi	r7,r7,2       /* Address of last word.  */
ce426f
+	cmplwi	cr7,r3,0      /* If r3 == 0, no BYTEs have been found.  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi	r9,r8,4
ce426f
-	cmplw	cr6,r9,r7
ce426f
-	bge	cr6,L(null)
ce426f
-
ce426f
 	mtcrf   0x01,r8
ce426f
 	/* Are we now aligned to a doubleword boundary?  If so, skip to
ce426f
 	   the main loop.  Otherwise, go through the alignment code.  */
ce426f
-
ce426f
 	bt	29,L(loop_setup)
ce426f
 
ce426f
 	/* Handle WORD2 of pair.  */
ce426f
 	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmplwi	cr7,r10,0
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr7,r3,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi	r9,r8,4
ce426f
-	cmplw	cr6,r9,r7
ce426f
-	bge	cr6,L(null)
ce426f
-
ce426f
 L(loop_setup):
ce426f
-	sub	r5,r7,r9
ce426f
-	srwi	r6,r5,3	      /* Number of loop iterations.  */
ce426f
+	/* The last word we want to read in the loop below is the one
ce426f
+	   containing the last byte of the string, ie. the word at
ce426f
+	   (s + size - 1) & ~3, or r7.  The first word read is at
ce426f
+	   r8 + 4, we read 2 * cnt words, so the last word read will
ce426f
+	   be at r8 + 4 + 8 * cnt - 4.  Solving for cnt gives
ce426f
+	   cnt = (r7 - r8) / 8  */
ce426f
+	sub	r6,r7,r8
ce426f
+	srwi	r6,r6,3	      /* Number of loop iterations.  */
ce426f
 	mtctr	r6            /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for BYTE backwards in the string.  Since
ce426f
-	   it's a small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
-	.p2align  5
ce426f
+
ce426f
+	/* Main loop to look for BYTE in the string.  Since
ce426f
+	   it's a small loop (8 instructions), align it to 32-bytes.  */
ce426f
+	.align	5
ce426f
 L(loop):
ce426f
 	/* Load two words, compare and merge in a
ce426f
 	   single register for speed.  This is an attempt
ce426f
 	   to speed up the byte-checking process for bigger strings.  */
ce426f
 	lwz	r12,4(r8)
ce426f
 	lwzu	r11,8(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
+	cmpb	r3,r12,r4
ce426f
 	cmpb	r9,r11,r4
ce426f
-	or	r5,r9,r10     /* Merge everything in one word.  */
ce426f
-	cmplwi	cr7,r5,0
ce426f
+	or	r6,r9,r3      /* Merge everything in one word.  */
ce426f
+	cmplwi	cr7,r6,0
ce426f
 	bne	cr7,L(found)
ce426f
 	bdnz	L(loop)
ce426f
 
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for BYTE in the whole range.  */
ce426f
-	subi	r11,r7,4
ce426f
-	cmplw	cr6,r8,r11
ce426f
-	blt	cr6,L(loop_small)
ce426f
-	b	L(null)
ce426f
+	/* We may have one more dword to read.  */
ce426f
+	cmplw	r8,r7
ce426f
+	beqlr
ce426f
 
ce426f
+	lwzu	r12,4(r8)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr6,r3,0
ce426f
+	bne	cr6,L(done)
ce426f
+	blr
ce426f
+
ce426f
+	.align	4
ce426f
+L(found):
ce426f
 	/* OK, one (or both) of the words contains BYTE.  Check
ce426f
 	   the first word and decrement the address in case the first
ce426f
 	   word really contains BYTE.  */
ce426f
-	.align	4
ce426f
-L(found):
ce426f
-	cmplwi	cr6,r10,0
ce426f
+	cmplwi	cr6,r3,0
ce426f
 	addi	r8,r8,-4
ce426f
 	bne	cr6,L(done)
ce426f
 
ce426f
 	/* BYTE must be in the second word.  Adjust the address
ce426f
-	   again and move the result of cmpb to r10 so we can calculate the
ce426f
+	   again and move the result of cmpb to r3 so we can calculate the
ce426f
 	   pointer.  */
ce426f
 
ce426f
-	mr	r10,r9
ce426f
+	mr	r3,r9
ce426f
 	addi	r8,r8,4
ce426f
 
ce426f
-	/* r10 has the output of the cmpb instruction, that is, it contains
ce426f
+	/* r3 has the output of the cmpb instruction, that is, it contains
ce426f
 	   0xff in the same position as BYTE in the original
ce426f
 	   word from the string.  Use that to calculate the pointer.
ce426f
 	   We need to make sure BYTE is *before* the end of the range.  */
ce426f
 L(done):
ce426f
-	cntlzw	r0,r10	      /* Count leading zeroes before the match.  */
ce426f
-	srwi	r0,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi    r0,r3,-1
ce426f
+	andc    r0,r0,r3
ce426f
+	popcntw	r0,r0	      /* Count trailing zeros.  */
ce426f
+#else
ce426f
+	cntlzw	r0,r3	      /* Count leading zeros before the match.  */
ce426f
+#endif
ce426f
+	cmplw	r8,r7         /* Are we on the last word?  */
ce426f
+	srwi	r0,r0,3	      /* Convert leading/trailing zeros to bytes.  */
ce426f
 	add	r3,r8,r0
ce426f
-	cmplw	r3,r7
ce426f
-	bge	L(null)
ce426f
+	cmplw	cr7,r0,r5     /* If on the last dword, check byte offset.  */
ce426f
+	bnelr
ce426f
+	blelr	cr7
ce426f
+	li	r3,0
ce426f
 	blr
ce426f
 
ce426f
 	.align	4
ce426f
@@ -139,69 +141,44 @@
ce426f
 	.align	4
ce426f
 L(small_range):
ce426f
 	cmplwi	r5,0
ce426f
-	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
-	beq	L(null)       /* This branch is for the cmplwi r5,0 above */
ce426f
+	beq	L(null)
ce426f
 	lwz	r12,0(r8)     /* Load word from memory.  */
ce426f
-	cmplwi	cr6,r6,0      /* cr6 == Do we have padding?  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE in DWORD1.  */
ce426f
-	beq	cr6,L(small_no_padding)
ce426f
-	slw	r10,r10,r6
ce426f
-	srw	r10,r10,r6
ce426f
-L(small_no_padding):
ce426f
-	cmplwi	cr7,r10,0
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in DWORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmplwi	cr7,r3,0
ce426f
+	clrlwi	r5,r7,30      /* Byte count - 1 in last word.  */
ce426f
+	clrrwi	r7,r7,2       /* Address of last word.  */
ce426f
+	cmplw	r8,r7         /* Are we done already?  */
ce426f
 	bne	cr7,L(done)
ce426f
+	beqlr
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi    r9,r8,4
ce426f
-	cmplw	r9,r7
ce426f
-	bge	L(null)
ce426f
-
ce426f
-L(loop_small):                /* loop_small has been unrolled.  */
ce426f
 	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	cmplw	r9,r7
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr6,r3,0
ce426f
+	cmplw	r8,r7
ce426f
 	bne	cr6,L(done)
ce426f
-	bge	L(null)
ce426f
+	beqlr
ce426f
 
ce426f
 	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	cmplw	r9,r7
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr6,r3,0
ce426f
+	cmplw	r8,r7
ce426f
 	bne	cr6,L(done)
ce426f
-	bge	L(null)
ce426f
+	beqlr
ce426f
 
ce426f
 	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	cmplw	r9,r7
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr6,r3,0
ce426f
+	cmplw	r8,r7
ce426f
 	bne	cr6,L(done)
ce426f
-	bge	L(null)
ce426f
+	beqlr
ce426f
 
ce426f
 	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	cmplw	r9,r7
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr6,r3,0
ce426f
 	bne	cr6,L(done)
ce426f
-	bge	L(null)
ce426f
-
ce426f
-	/* For most cases we will never get here.  Under some combinations of
ce426f
-	   padding + length there is a leftover word that still needs to be
ce426f
-	   checked.  */
ce426f
-	lwzu	r12,4(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	bne	cr6,L(done)
ce426f
-
ce426f
-	/* save a branch and exit directly */
ce426f
-	li	r3,0
ce426f
 	blr
ce426f
 
ce426f
-END (BP_SYM (__memchr))
ce426f
-weak_alias (BP_SYM (__memchr), BP_SYM(memchr))
ce426f
+END (__memchr)
ce426f
+weak_alias (__memchr, memchr)
ce426f
 libc_hidden_builtin_def (memchr)
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memrchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memrchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memrchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/memrchr.S	2014-05-29 13:13:47.000000000 -0500
ce426f
@@ -1,5 +1,5 @@
ce426f
 /* Optimized memrchr implementation for PowerPC32/POWER7 using cmpb insn.
ce426f
-   Copyright (C) 2010 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2010-2014 Free Software Foundation, Inc.
ce426f
    Contributed by Luis Machado <luisgpm@br.ibm.com>.
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -18,124 +18,136 @@
ce426f
    <http://www.gnu.org/licenses/>.  */
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
-#include <bp-sym.h>
ce426f
-#include <bp-asm.h>
ce426f
 
ce426f
 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5])  */
ce426f
 	.machine  power7
ce426f
-ENTRY (BP_SYM (__memrchr))
ce426f
+ENTRY (__memrchr)
ce426f
 	CALL_MCOUNT
ce426f
-	dcbt	0,r3
ce426f
-	mr	r7,r3
ce426f
-	add	r3,r7,r5      /* Calculate the last acceptable address.  */
ce426f
-	cmplw	cr7,r3,r7     /* Is the address equal or less than r3?  */
ce426f
+	add	r7,r3,r5      /* Calculate the last acceptable address.  */
ce426f
+	neg	r0,r7
ce426f
+	addi	r7,r7,-1
ce426f
+	mr	r10,r3
ce426f
+	clrrwi	r6,r7,7
ce426f
+	li	r9,3<<5
ce426f
+	dcbt	r9,r6,16      /* Stream hint, decreasing addresses.  */
ce426f
 
ce426f
 	/* Replicate BYTE to word.  */
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
-	bge	cr7,L(proceed)
ce426f
-
ce426f
-	li	r3,-1	      /* Make r11 the biggest if r4 <= 0.  */
ce426f
-L(proceed):
ce426f
+	insrwi	r4,r4,8,16
ce426f
+	insrwi	r4,r4,16,0
ce426f
 	li	r6,-4
ce426f
-	addi	r9,r3,-1
ce426f
-	clrrwi  r8,r9,2
ce426f
-	addi	r8,r8,4
ce426f
-	neg	r0,r3
ce426f
+	li	r9,-1
ce426f
 	rlwinm	r0,r0,3,27,28 /* Calculate padding.  */
ce426f
-
ce426f
+	clrrwi	r8,r7,2
ce426f
+	srw	r9,r9,r0
ce426f
 	cmplwi	r5,16
ce426f
+	clrrwi	r0,r10,2
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	lwbrx	r12,r8,r6     /* Load reversed word from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE in WORD1.  */
ce426f
-	slw	r10,r10,r0
ce426f
-	srw	r10,r10,r0
ce426f
-	cmplwi	cr7,r10,0     /* If r10 == 0, no BYTE's have been found.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,0,r8
ce426f
+#else
ce426f
+	lwbrx	r12,0,r8      /* Load reversed word from memory.  */
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in WORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmplwi	cr7,r3,0      /* If r3 == 0, no BYTEs have been found.  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi	r9,r8,-4
ce426f
-	cmplw	cr6,r9,r7
ce426f
-	ble	cr6,L(null)
ce426f
-
ce426f
 	mtcrf   0x01,r8
ce426f
 	/* Are we now aligned to a doubleword boundary?  If so, skip to
ce426f
 	   the main loop.  Otherwise, go through the alignment code.  */
ce426f
-	mr	r8,r9
ce426f
-	bt	29,L(loop_setup)
ce426f
+	bf	29,L(loop_setup)
ce426f
 
ce426f
 	/* Handle WORD2 of pair.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,r8,r6
ce426f
+#else
ce426f
 	lwbrx	r12,r8,r6
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmplwi	cr7,r10,0
ce426f
-	bne	cr7,L(done)
ce426f
-
ce426f
-	/* Are we done already?  */
ce426f
+#endif
ce426f
 	addi	r8,r8,-4
ce426f
-	cmplw	cr6,r8,r7
ce426f
-	ble	cr6,L(null)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
 
ce426f
 L(loop_setup):
ce426f
-	li	r0,-8
ce426f
-	sub	r5,r8,r7
ce426f
-	srwi	r9,r5,3	      /* Number of loop iterations.  */
ce426f
+	/* The last word we want to read in the loop below is the one
ce426f
+	   containing the first byte of the string, ie. the word at
ce426f
+	   s & ~3, or r0.  The first word read is at r8 - 4, we
ce426f
+	   read 2 * cnt words, so the last word read will be at
ce426f
+	   r8 - 4 - 8 * cnt + 4.  Solving for cnt gives
ce426f
+	   cnt = (r8 - r0) / 8  */
ce426f
+	sub	r5,r8,r0
ce426f
+	addi	r8,r8,-4
ce426f
+	srwi	r9,r5,3       /* Number of loop iterations.  */
ce426f
 	mtctr	r9	      /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for BYTE backwards in the string.  Since it's a
ce426f
-	   small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
-	.p2align  5
ce426f
+
ce426f
+	/* Main loop to look for BYTE backwards in the string.
ce426f
+	   FIXME: Investigate whether 32 byte align helps with this
ce426f
+	   9 instruction loop.  */
ce426f
+	.align	5
ce426f
 L(loop):
ce426f
 	/* Load two words, compare and merge in a
ce426f
 	   single register for speed.  This is an attempt
ce426f
 	   to speed up the byte-checking process for bigger strings.  */
ce426f
 
ce426f
-	lwbrx	r12,r8,r6
ce426f
-	lwbrx	r11,r8,r0
ce426f
-	addi	r8,r8,-4
ce426f
-	cmpb	r10,r12,r4
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,0,r8
ce426f
+	lwzx	r11,r8,r6
ce426f
+#else
ce426f
+	lwbrx	r12,0,r8
ce426f
+	lwbrx	r11,r8,r6
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
 	cmpb	r9,r11,r4
ce426f
-	or	r5,r9,r10     /* Merge everything in one word.  */
ce426f
+	or	r5,r9,r3      /* Merge everything in one word.  */
ce426f
 	cmplwi	cr7,r5,0
ce426f
 	bne	cr7,L(found)
ce426f
-	addi	r8,r8,-4
ce426f
+	addi	r8,r8,-8
ce426f
 	bdnz	L(loop)
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for BYTE in the whole range.  Just return
ce426f
-	   the original range.  */
ce426f
-	addi	r9,r8,4
ce426f
-	cmplw	cr6,r9,r7
ce426f
-	bgt	cr6,L(loop_small)
ce426f
-	b	L(null)
ce426f
 
ce426f
-	/* OK, one (or both) of the words contains BYTE.  Check
ce426f
-	   the first word and decrement the address in case the first
ce426f
-	   word really contains BYTE.  */
ce426f
+	/* We may have one more word to read.  */
ce426f
+	cmplw	r8,r0
ce426f
+	bnelr
ce426f
+
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,0,r8
ce426f
+#else
ce426f
+	lwbrx	r12,0,r8
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplwi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
+	blr
ce426f
+
ce426f
 	.align	4
ce426f
 L(found):
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	addi	r8,r8,4
ce426f
+	/* OK, one (or both) of the words contains BYTE.  Check
ce426f
+	   the first word.  */
ce426f
+	cmplwi	cr6,r3,0
ce426f
 	bne	cr6,L(done)
ce426f
 
ce426f
 	/* BYTE must be in the second word.  Adjust the address
ce426f
-	   again and move the result of cmpb to r10 so we can calculate the
ce426f
+	   again and move the result of cmpb to r3 so we can calculate the
ce426f
 	   pointer.  */
ce426f
 
ce426f
-	mr	r10,r9
ce426f
+	mr	r3,r9
ce426f
 	addi	r8,r8,-4
ce426f
 
ce426f
-	/* r10 has the output of the cmpb instruction, that is, it contains
ce426f
+	/* r3 has the output of the cmpb instruction, that is, it contains
ce426f
 	   0xff in the same position as BYTE in the original
ce426f
 	   word from the string.  Use that to calculate the pointer.
ce426f
 	   We need to make sure BYTE is *before* the end of the
ce426f
 	   range.  */
ce426f
 L(done):
ce426f
-	cntlzw	r0,r10	      /* Count leading zeroes before the match.  */
ce426f
-	srwi	r6,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
-	addi	r0,r6,1
ce426f
+	cntlzw	r9,r3	      /* Count leading zeros before the match.  */
ce426f
+	cmplw	r8,r0         /* Are we on the last word?  */
ce426f
+	srwi	r6,r9,3	      /* Convert leading zeros to bytes.  */
ce426f
+	addi	r0,r6,-3
ce426f
 	sub	r3,r8,r0
ce426f
-	cmplw	r3,r7
ce426f
-	blt	L(null)
ce426f
+	cmplw	cr7,r3,r10
ce426f
+	bnelr
ce426f
+	bgelr	cr7
ce426f
+	li	r3,0
ce426f
 	blr
ce426f
 
ce426f
 	.align	4
ce426f
@@ -149,29 +161,36 @@
ce426f
 	cmplwi	r5,0
ce426f
 	beq	L(null)
ce426f
 
ce426f
-	lwbrx	r12,r8,r6     /* Load reversed word from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for null bytes in WORD1.  */
ce426f
-	slw	r10,r10,r0
ce426f
-	srw	r10,r10,r0
ce426f
-	cmplwi	cr7,r10,0
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,0,r8
ce426f
+#else
ce426f
+	lwbrx	r12,0,r8      /* Load reversed word from memory.  */
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in WORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmplwi	cr7,r3,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
+	/* Are we done already?  */
ce426f
+	cmplw	r8,r0
ce426f
 	addi	r8,r8,-4
ce426f
-	cmplw	r8,r7
ce426f
-	ble	L(null)
ce426f
-	b	L(loop_small)
ce426f
+	beqlr
ce426f
 
ce426f
-	.p2align  5
ce426f
+	.align	5
ce426f
 L(loop_small):
ce426f
-	lwbrx	r12,r8,r6
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmplwi	cr6,r10,0
ce426f
-	bne	cr6,L(done)
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	lwzx	r12,0,r8
ce426f
+#else
ce426f
+	lwbrx	r12,0,r8
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmplw	r8,r0
ce426f
+	cmplwi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
 	addi	r8,r8,-4
ce426f
-	cmplw	r8,r7
ce426f
-	ble	L(null)
ce426f
-	b	L(loop_small)
ce426f
+	bne	L(loop_small)
ce426f
+	blr
ce426f
 
ce426f
-END (BP_SYM (__memrchr))
ce426f
-weak_alias (BP_SYM (__memrchr), BP_SYM(memrchr))
ce426f
+END (__memrchr)
ce426f
+weak_alias (__memrchr, memrchr)
ce426f
 libc_hidden_builtin_def (memrchr)
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/rawmemchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/rawmemchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/rawmemchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/rawmemchr.S	2014-05-29 13:09:19.000000000 -0500
ce426f
@@ -29,16 +29,21 @@
ce426f
 	clrrwi	r8,r3,2	      /* Align the address to word boundary.  */
ce426f
 
ce426f
 	/* Replicate byte to word.  */
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
+	rldimi	r4,r4,8,48
ce426f
+	rldimi	r4,r4,16,32
ce426f
 
ce426f
 	/* Now r4 has a word of c bytes.  */
ce426f
 
ce426f
 	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
 	lwz	r12,0(r8)     /* Load word from memory.  */
ce426f
 	cmpb	r5,r12,r4     /* Compare each byte against c byte.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srw	r5,r5,r6
ce426f
+	slw	r5,r5,r6
ce426f
+#else
ce426f
 	slw	r5,r5,r6      /* Move left to discard ignored bits.  */
ce426f
 	srw	r5,r5,r6      /* Bring the bits back as zeros.  */
ce426f
+#endif
ce426f
 	cmpwi	cr7,r5,0      /* If r5 == 0, no c bytes have been found.  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
@@ -92,8 +97,14 @@
ce426f
 	   word from the string.  Use that fact to find out what is
ce426f
 	   the position of the byte inside the string.  */
ce426f
 L(done):
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi    r0,r5,-1
ce426f
+	andc    r0,r0,r5
ce426f
+	popcntw	r0,r0
ce426f
+#else
ce426f
 	cntlzw	r0,r5	      /* Count leading zeros before the match.  */
ce426f
-	srwi	r0,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
+#endif
ce426f
+	srwi	r0,r0,3	      /* Convert leading zeros to bytes.  */
ce426f
 	add	r3,r8,r0      /* Return address of the matching char.  */
ce426f
 	blr
ce426f
 END (BP_SYM (__rawmemchr))
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memchr.S	2014-05-29 13:13:57.000000000 -0500
ce426f
@@ -1,5 +1,5 @@
ce426f
 /* Optimized memchr implementation for PowerPC64/POWER7 using cmpb insn.
ce426f
-   Copyright (C) 2010-2012 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2010-2014 Free Software Foundation, Inc.
ce426f
    Contributed by Luis Machado <luisgpm@br.ibm.com>.
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -18,118 +18,119 @@
ce426f
    <http://www.gnu.org/licenses/>.  */
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
-#include <bp-sym.h>
ce426f
-#include <bp-asm.h>
ce426f
 
ce426f
 /* int [r3] memchr (char *s [r3], int byte [r4], int size [r5])  */
ce426f
 	.machine  power7
ce426f
-ENTRY (BP_SYM (__memchr))
ce426f
-	CALL_MCOUNT 2
ce426f
+ENTRY (__memchr)
ce426f
+	CALL_MCOUNT 3
ce426f
 	dcbt	0,r3
ce426f
 	clrrdi  r8,r3,3
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
+	insrdi	r4,r4,8,48
ce426f
 	add	r7,r3,r5      /* Calculate the last acceptable address.  */
ce426f
+	insrdi	r4,r4,16,32
ce426f
 	cmpldi	r5,32
ce426f
+	li	r9, -1
ce426f
+	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
 	insrdi  r4,r4,32,0
ce426f
+	addi	r7,r7,-1
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	sld	r9,r9,r6
ce426f
+#else
ce426f
+	srd	r9,r9,r6
ce426f
+#endif
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	cmpld	cr7,r3,r7     /* Compare the starting address (r3) with the
ce426f
-				 ending address (r7).  If (r3 >= r7),
ce426f
-				 the size passed in was zero or negative.  */
ce426f
-	ble	cr7,L(proceed)
ce426f
-
ce426f
-	li	r7,-1         /* Artificially set our ending address (r7)
ce426f
-				 such that we will exit early.  */
ce426f
-
ce426f
-L(proceed):
ce426f
-	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
-	cmpldi	cr6,r6,0      /* cr6 == Do we have padding?  */
ce426f
 	ld	r12,0(r8)     /* Load doubleword from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTEs in DWORD1.  */
ce426f
-	beq	cr6,L(proceed_no_padding)
ce426f
-	sld	r10,r10,r6
ce426f
-	srd	r10,r10,r6
ce426f
-L(proceed_no_padding):
ce426f
-	cmpldi	cr7,r10,0     /* Does r10 indicate we got a hit?  */
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTEs in DWORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	clrldi	r5,r7,61      /* Byte count - 1 in last dword.  */
ce426f
+	clrrdi	r7,r7,3       /* Address of last doubleword.  */
ce426f
+	cmpldi	cr7,r3,0      /* Does r3 indicate we got a hit?  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* See if we are at the last acceptable address yet.  */
ce426f
-	addi	r9,r8,8
ce426f
-	cmpld	cr6,r9,r7
ce426f
-	bge	cr6,L(null)
ce426f
-
ce426f
 	mtcrf   0x01,r8
ce426f
 	/* Are we now aligned to a quadword boundary?  If so, skip to
ce426f
 	   the main loop.  Otherwise, go through the alignment code.  */
ce426f
-
ce426f
 	bt	28,L(loop_setup)
ce426f
 
ce426f
 	/* Handle DWORD2 of pair.  */
ce426f
 	ldu	r12,8(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmpldi	cr7,r10,0
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr7,r3,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi	r9,r8,8
ce426f
-	cmpld	cr6,r9,r7
ce426f
-	bge	cr6,L(null)
ce426f
-
ce426f
 L(loop_setup):
ce426f
-	sub	r5,r7,r9
ce426f
-	srdi	r6,r5,4	      /* Number of loop iterations.  */
ce426f
+	/* The last dword we want to read in the loop below is the one
ce426f
+	   containing the last byte of the string, ie. the dword at
ce426f
+	   (s + size - 1) & ~7, or r7.  The first dword read is at
ce426f
+	   r8 + 8, we read 2 * cnt dwords, so the last dword read will
ce426f
+	   be at r8 + 8 + 16 * cnt - 8.  Solving for cnt gives
ce426f
+	   cnt = (r7 - r8) / 16  */
ce426f
+	sub	r6,r7,r8
ce426f
+	srdi	r6,r6,4	      /* Number of loop iterations.  */
ce426f
 	mtctr	r6            /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for BYTE backwards in the string.  Since
ce426f
-	   it's a small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
-	.p2align  5
ce426f
+
ce426f
+	/* Main loop to look for BYTE in the string.  Since
ce426f
+	   it's a small loop (8 instructions), align it to 32-bytes.  */
ce426f
+	.align	5
ce426f
 L(loop):
ce426f
 	/* Load two doublewords, compare and merge in a
ce426f
 	   single register for speed.  This is an attempt
ce426f
 	   to speed up the byte-checking process for bigger strings.  */
ce426f
 	ld	r12,8(r8)
ce426f
 	ldu	r11,16(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
+	cmpb	r3,r12,r4
ce426f
 	cmpb	r9,r11,r4
ce426f
-	or	r5,r9,r10     /* Merge everything in one doubleword.  */
ce426f
-	cmpldi	cr7,r5,0
ce426f
+	or	r6,r9,r3      /* Merge everything in one doubleword.  */
ce426f
+	cmpldi	cr7,r6,0
ce426f
 	bne	cr7,L(found)
ce426f
 	bdnz	L(loop)
ce426f
 
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for BYTE in the whole range.  */
ce426f
-	subi	r11,r7,8
ce426f
-	cmpld	cr6,r8,r11
ce426f
-	blt	cr6,L(loop_small)
ce426f
-	b	L(null)
ce426f
+	/* We may have one more dword to read.  */
ce426f
+	cmpld	r8,r7
ce426f
+	beqlr
ce426f
 
ce426f
+	ldu	r12,8(r8)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr6,r3,0
ce426f
+	bne	cr6,L(done)
ce426f
+	blr
ce426f
+
ce426f
+	.align	4
ce426f
+L(found):
ce426f
 	/* OK, one (or both) of the doublewords contains BYTE.  Check
ce426f
 	   the first doubleword and decrement the address in case the first
ce426f
 	   doubleword really contains BYTE.  */
ce426f
-	.align	4
ce426f
-L(found):
ce426f
-	cmpldi	cr6,r10,0
ce426f
+	cmpldi	cr6,r3,0
ce426f
 	addi	r8,r8,-8
ce426f
 	bne	cr6,L(done)
ce426f
 
ce426f
 	/* BYTE must be in the second doubleword.  Adjust the address
ce426f
-	   again and move the result of cmpb to r10 so we can calculate the
ce426f
+	   again and move the result of cmpb to r3 so we can calculate the
ce426f
 	   pointer.  */
ce426f
 
ce426f
-	mr	r10,r9
ce426f
+	mr	r3,r9
ce426f
 	addi	r8,r8,8
ce426f
 
ce426f
-	/* r10 has the output of the cmpb instruction, that is, it contains
ce426f
+	/* r3 has the output of the cmpb instruction, that is, it contains
ce426f
 	   0xff in the same position as BYTE in the original
ce426f
 	   doubleword from the string.  Use that to calculate the pointer.
ce426f
 	   We need to make sure BYTE is *before* the end of the range.  */
ce426f
 L(done):
ce426f
-	cntlzd	r0,r10	      /* Count leading zeroes before the match.  */
ce426f
-	srdi	r0,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi    r0,r3,-1
ce426f
+	andc    r0,r0,r3
ce426f
+	popcntd	r0,r0	      /* Count trailing zeros.  */
ce426f
+#else
ce426f
+	cntlzd	r0,r3	      /* Count leading zeros before the match.  */
ce426f
+#endif
ce426f
+	cmpld	r8,r7         /* Are we on the last dword?  */
ce426f
+	srdi	r0,r0,3	      /* Convert leading/trailing zeros to bytes.  */
ce426f
 	add	r3,r8,r0
ce426f
-	cmpld	r3,r7
ce426f
-	bge	L(null)
ce426f
+	cmpld	cr7,r0,r5     /* If on the last dword, check byte offset.  */
ce426f
+	bnelr
ce426f
+	blelr	cr7
ce426f
+	li	r3,0
ce426f
 	blr
ce426f
 
ce426f
 	.align	4
ce426f
@@ -141,67 +142,44 @@
ce426f
 	.align	4
ce426f
 L(small_range):
ce426f
 	cmpldi	r5,0
ce426f
-	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
-	beq	L(null)       /* This branch is for the cmpldi r5,0 above.  */
ce426f
+	beq	L(null)
ce426f
 	ld	r12,0(r8)     /* Load word from memory.  */
ce426f
-	cmpldi	cr6,r6,0      /* cr6 == Do we have padding?  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE in DWORD1.  */
ce426f
-			      /* If no padding, skip the shifts.  */
ce426f
-	beq	cr6,L(small_no_padding)
ce426f
-	sld	r10,r10,r6
ce426f
-	srd	r10,r10,r6
ce426f
-L(small_no_padding):
ce426f
-	cmpldi	cr7,r10,0
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in DWORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmpldi	cr7,r3,0
ce426f
+	clrldi	r5,r7,61      /* Byte count - 1 in last dword.  */
ce426f
+	clrrdi	r7,r7,3       /* Address of last doubleword.  */
ce426f
+	cmpld	r8,r7         /* Are we done already?  */
ce426f
 	bne	cr7,L(done)
ce426f
-
ce426f
-	/* Are we done already?  */
ce426f
-	addi    r9,r8,8
ce426f
-	cmpld	r9,r7
ce426f
-	bge	L(null)
ce426f
-	/* If we're not done, drop through into loop_small.  */
ce426f
-
ce426f
-L(loop_small):                /* loop_small has been unrolled.  */
ce426f
-	ldu	r12,8(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,8
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	cmpld	r9,r7
ce426f
-	bne	cr6,L(done)   /* Found something.  */
ce426f
-	bge	L(null)       /* Hit end of string (length).  */
ce426f
+	beqlr
ce426f
 
ce426f
 	ldu	r12,8(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,8
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	cmpld	r9,r7
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr6,r3,0
ce426f
+	cmpld	r8,r7
ce426f
 	bne	cr6,L(done)   /* Found something.  */
ce426f
-	bge	L(null)
ce426f
+	beqlr		      /* Hit end of string (length).  */
ce426f
 
ce426f
 	ldu	r12,8(r8)
ce426f
-	subi	r11,r7,8
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	ori	r2,r2,0       /* Force a dispatch group.  */
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr6,r3,0
ce426f
+	cmpld	r8,r7
ce426f
 	bne	cr6,L(done)
ce426f
+	beqlr
ce426f
 
ce426f
-	cmpld	r8,r11        /* At end of range?  */
ce426f
-	bge	L(null)
ce426f
-
ce426f
-	/* For most cases we will never get here.  Under some combinations of
ce426f
-	   padding + length there is a leftover double that still needs to be
ce426f
-	   checked.  */
ce426f
-	ldu	r12,8(r8)
ce426f
-	cmpb	r10,r12,r4
ce426f
-	addi	r9,r8,8
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	cmpld	r9,r7
ce426f
-	bne	cr6,L(done)   /* Found something.  */
ce426f
+	ldu	r12,8(r8)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr6,r3,0
ce426f
+	cmpld	r8,r7
ce426f
+	bne	cr6,L(done)
ce426f
+	beqlr
ce426f
 
ce426f
-	/* Save a branch and exit directly.  */
ce426f
-	li	r3,0
ce426f
+	ldu	r12,8(r8)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr6,r3,0
ce426f
+	bne	cr6,L(done)
ce426f
 	blr
ce426f
 
ce426f
-
ce426f
-END (BP_SYM (__memchr))
ce426f
-weak_alias (BP_SYM (__memchr), BP_SYM(memchr))
ce426f
+END (__memchr)
ce426f
+weak_alias (__memchr, memchr)
ce426f
 libc_hidden_builtin_def (memchr)
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memrchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memrchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memrchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/memrchr.S	2014-05-29 13:14:06.000000000 -0500
ce426f
@@ -1,5 +1,5 @@
ce426f
 /* Optimized memrchr implementation for PowerPC64/POWER7 using cmpb insn.
ce426f
-   Copyright (C) 2010 Free Software Foundation, Inc.
ce426f
+   Copyright (C) 2010-2014 Free Software Foundation, Inc.
ce426f
    Contributed by Luis Machado <luisgpm@br.ibm.com>.
ce426f
    This file is part of the GNU C Library.
ce426f
 
ce426f
@@ -18,125 +18,137 @@
ce426f
    <http://www.gnu.org/licenses/>.  */
ce426f
 
ce426f
 #include <sysdep.h>
ce426f
-#include <bp-sym.h>
ce426f
-#include <bp-asm.h>
ce426f
 
ce426f
 /* int [r3] memrchr (char *s [r3], int byte [r4], int size [r5])  */
ce426f
 	.machine  power7
ce426f
-ENTRY (BP_SYM (__memrchr))
ce426f
-	CALL_MCOUNT
ce426f
-	dcbt	0,r3
ce426f
-	mr	r7,r3
ce426f
-	add	r3,r7,r5      /* Calculate the last acceptable address.  */
ce426f
-	cmpld	cr7,r3,r7     /* Is the address equal or less than r3?  */
ce426f
+ENTRY (__memrchr)
ce426f
+	CALL_MCOUNT 3
ce426f
+	add	r7,r3,r5      /* Calculate the last acceptable address.  */
ce426f
+	neg	r0,r7
ce426f
+	addi	r7,r7,-1
ce426f
+	mr	r10,r3
ce426f
+	clrrdi	r6,r7,7
ce426f
+	li	r9,3<<5
ce426f
+	dcbt	r9,r6,8       /* Stream hint, decreasing addresses.  */
ce426f
 
ce426f
 	/* Replicate BYTE to doubleword.  */
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
+	insrdi	r4,r4,8,48
ce426f
+	insrdi	r4,r4,16,32
ce426f
 	insrdi  r4,r4,32,0
ce426f
-	bge	cr7,L(proceed)
ce426f
-
ce426f
-	li	r3,-1	      /* Make r11 the biggest if r4 <= 0.  */
ce426f
-L(proceed):
ce426f
 	li	r6,-8
ce426f
-	addi	r9,r3,-1
ce426f
-	clrrdi  r8,r9,3
ce426f
-	addi	r8,r8,8
ce426f
-	neg	r0,r3
ce426f
+	li	r9,-1
ce426f
 	rlwinm	r0,r0,3,26,28 /* Calculate padding.  */
ce426f
-
ce426f
+	clrrdi	r8,r7,3
ce426f
+	srd	r9,r9,r0
ce426f
 	cmpldi	r5,32
ce426f
+	clrrdi	r0,r10,3
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	ldbrx	r12,r8,r6     /* Load reversed doubleword from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE in DWORD1.  */
ce426f
-	sld	r10,r10,r0
ce426f
-	srd	r10,r10,r0
ce426f
-	cmpldi	cr7,r10,0     /* If r10 == 0, no BYTE's have been found.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,0,r8
ce426f
+#else
ce426f
+	ldbrx	r12,0,r8      /* Load reversed doubleword from memory.  */
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in DWORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmpldi	cr7,r3,0      /* If r3 == 0, no BYTEs have been found.  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	/* Are we done already?  */
ce426f
-	addi	r9,r8,-8
ce426f
-	cmpld	cr6,r9,r7
ce426f
-	ble	cr6,L(null)
ce426f
-
ce426f
 	mtcrf   0x01,r8
ce426f
-	/* Are we now aligned to a doubleword boundary?  If so, skip to
ce426f
+	/* Are we now aligned to a quadword boundary?  If so, skip to
ce426f
 	   the main loop.  Otherwise, go through the alignment code.  */
ce426f
-	mr	r8,r9
ce426f
-	bt	28,L(loop_setup)
ce426f
+	bf	28,L(loop_setup)
ce426f
 
ce426f
 	/* Handle DWORD2 of pair.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,r8,r6
ce426f
+#else
ce426f
 	ldbrx	r12,r8,r6
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmpldi	cr7,r10,0
ce426f
-	bne	cr7,L(done)
ce426f
-
ce426f
-	/* Are we done already.  */
ce426f
+#endif
ce426f
 	addi	r8,r8,-8
ce426f
-	cmpld	cr6,r8,r7
ce426f
-	ble	cr6,L(null)
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
 
ce426f
 L(loop_setup):
ce426f
-	li	r0,-16
ce426f
-	sub	r5,r8,r7
ce426f
-	srdi	r9,r5,4	      /* Number of loop iterations.  */
ce426f
+	/* The last dword we want to read in the loop below is the one
ce426f
+	   containing the first byte of the string, ie. the dword at
ce426f
+	   s & ~7, or r0.  The first dword read is at r8 - 8, we
ce426f
+	   read 2 * cnt dwords, so the last dword read will be at
ce426f
+	   r8 - 8 - 16 * cnt + 8.  Solving for cnt gives
ce426f
+	   cnt = (r8 - r0) / 16  */
ce426f
+	sub	r5,r8,r0
ce426f
+	addi	r8,r8,-8
ce426f
+	srdi	r9,r5,4       /* Number of loop iterations.  */
ce426f
 	mtctr	r9	      /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for BYTE backwards in the string.  Since it's a
ce426f
-	   small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
-	.p2align  5
ce426f
+
ce426f
+	/* Main loop to look for BYTE backwards in the string.
ce426f
+	   FIXME: Investigate whether 32 byte align helps with this
ce426f
+	   9 instruction loop.  */
ce426f
+	.align	5
ce426f
 L(loop):
ce426f
 	/* Load two doublewords, compare and merge in a
ce426f
 	   single register for speed.  This is an attempt
ce426f
 	   to speed up the byte-checking process for bigger strings.  */
ce426f
 
ce426f
-	ldbrx	r12,r8,r6
ce426f
-	ldbrx	r11,r8,r0
ce426f
-	addi	r8,r8,-8
ce426f
-	cmpb	r10,r12,r4
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,0,r8
ce426f
+	ldx	r11,r8,r6
ce426f
+#else
ce426f
+	ldbrx	r12,0,r8
ce426f
+	ldbrx	r11,r8,r6
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
 	cmpb	r9,r11,r4
ce426f
-	or	r5,r9,r10     /* Merge everything in one doubleword.  */
ce426f
+	or	r5,r9,r3      /* Merge everything in one doubleword.  */
ce426f
 	cmpldi	cr7,r5,0
ce426f
 	bne	cr7,L(found)
ce426f
-	addi	r8,r8,-8
ce426f
+	addi	r8,r8,-16
ce426f
 	bdnz	L(loop)
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for BYTE in the whole range.  Just return
ce426f
-	   the original range.  */
ce426f
-	addi	r9,r8,8
ce426f
-	cmpld	cr6,r9,r7
ce426f
-	bgt	cr6,L(loop_small)
ce426f
-	b	L(null)
ce426f
-
ce426f
-	/* OK, one (or both) of the words contains BYTE.  Check
ce426f
-	   the first word and decrement the address in case the first
ce426f
-	   word really contains BYTE.  */
ce426f
+
ce426f
+	/* We may have one more word to read.  */
ce426f
+	cmpld	r8,r0
ce426f
+	bnelr
ce426f
+
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,0,r8
ce426f
+#else
ce426f
+	ldbrx	r12,0,r8
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpldi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
+	blr
ce426f
+
ce426f
 	.align	4
ce426f
 L(found):
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	addi	r8,r8,8
ce426f
+	/* OK, one (or both) of the dwords contains BYTE.  Check
ce426f
+	   the first dword.  */
ce426f
+	cmpldi	cr6,r3,0
ce426f
 	bne	cr6,L(done)
ce426f
 
ce426f
 	/* BYTE must be in the second word.  Adjust the address
ce426f
-	   again and move the result of cmpb to r10 so we can calculate the
ce426f
+	   again and move the result of cmpb to r3 so we can calculate the
ce426f
 	   pointer.  */
ce426f
 
ce426f
-	mr	r10,r9
ce426f
+	mr	r3,r9
ce426f
 	addi	r8,r8,-8
ce426f
 
ce426f
-	/* r10 has the output of the cmpb instruction, that is, it contains
ce426f
-	   0xff in the same position as the BYTE in the original
ce426f
+	/* r3 has the output of the cmpb instruction, that is, it contains
ce426f
+	   0xff in the same position as BYTE in the original
ce426f
 	   word from the string.  Use that to calculate the pointer.
ce426f
 	   We need to make sure BYTE is *before* the end of the
ce426f
 	   range.  */
ce426f
 L(done):
ce426f
-	cntlzd	r0,r10	      /* Count leading zeroes before the match.  */
ce426f
-	srdi	r6,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
-	addi	r0,r6,1
ce426f
+	cntlzd	r9,r3	      /* Count leading zeros before the match.  */
ce426f
+	cmpld	r8,r0         /* Are we on the last word?  */
ce426f
+	srdi	r6,r9,3	      /* Convert leading zeros to bytes.  */
ce426f
+	addi	r0,r6,-7
ce426f
 	sub	r3,r8,r0
ce426f
-	cmpld	r3,r7
ce426f
-	blt	L(null)
ce426f
+	cmpld	cr7,r3,r10
ce426f
+	bnelr
ce426f
+	bgelr	cr7
ce426f
+	li	r3,0
ce426f
 	blr
ce426f
 
ce426f
 	.align	4
ce426f
@@ -150,30 +162,36 @@
ce426f
 	cmpldi	r5,0
ce426f
 	beq	L(null)
ce426f
 
ce426f
-	ldbrx	r12,r8,r6     /* Load reversed doubleword from memory.  */
ce426f
-	cmpb	r10,r12,r4    /* Check for BYTE in DWORD1.  */
ce426f
-	sld	r10,r10,r0
ce426f
-	srd	r10,r10,r0
ce426f
-	cmpldi	cr7,r10,0
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,0,r8
ce426f
+#else
ce426f
+	ldbrx	r12,0,r8      /* Load reversed doubleword from memory.  */
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4     /* Check for BYTE in DWORD1.  */
ce426f
+	and	r3,r3,r9
ce426f
+	cmpldi	cr7,r3,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
 	/* Are we done already?  */
ce426f
+	cmpld	r8,r0
ce426f
 	addi	r8,r8,-8
ce426f
-	cmpld	r8,r7
ce426f
-	ble	L(null)
ce426f
-	b	L(loop_small)
ce426f
+	beqlr
ce426f
 
ce426f
-	.p2align  5
ce426f
+	.align	5
ce426f
 L(loop_small):
ce426f
-	ldbrx	r12,r8,r6
ce426f
-	cmpb	r10,r12,r4
ce426f
-	cmpldi	cr6,r10,0
ce426f
-	bne	cr6,L(done)
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	ldx	r12,0,r8
ce426f
+#else
ce426f
+	ldbrx	r12,0,r8
ce426f
+#endif
ce426f
+	cmpb	r3,r12,r4
ce426f
+	cmpld	r8,r0
ce426f
+	cmpldi	cr7,r3,0
ce426f
+	bne	cr7,L(done)
ce426f
 	addi	r8,r8,-8
ce426f
-	cmpld	r8,r7
ce426f
-	ble	L(null)
ce426f
-	b	L(loop_small)
ce426f
+	bne	L(loop_small)
ce426f
+	blr
ce426f
 
ce426f
-END (BP_SYM (__memrchr))
ce426f
-weak_alias (BP_SYM (__memrchr), BP_SYM(memrchr))
ce426f
+END (__memrchr)
ce426f
+weak_alias (__memrchr, memrchr)
ce426f
 libc_hidden_builtin_def (memrchr)
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/rawmemchr.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/rawmemchr.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/rawmemchr.S	2014-05-29 13:09:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/rawmemchr.S	2014-05-29 13:09:19.000000000 -0500
ce426f
@@ -29,8 +29,8 @@
ce426f
 	clrrdi	r8,r3,3	      /* Align the address to doubleword boundary.  */
ce426f
 
ce426f
 	/* Replicate byte to doubleword.  */
ce426f
-	rlwimi	r4,r4,8,16,23
ce426f
-	rlwimi	r4,r4,16,0,15
ce426f
+	insrdi	r4,r4,8,48
ce426f
+	insrdi	r4,r4,16,32
ce426f
 	insrdi	r4,r4,32,0
ce426f
 
ce426f
 	/* Now r4 has a doubleword of c bytes.  */
ce426f
@@ -38,8 +38,13 @@
ce426f
 	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
 	ld	r12,0(r8)     /* Load doubleword from memory.  */
ce426f
 	cmpb	r5,r12,r4     /* Compare each byte against c byte.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srd	r5,r5,r6
ce426f
+	sld	r5,r5,r6
ce426f
+#else
ce426f
 	sld	r5,r5,r6      /* Move left to discard ignored bits.  */
ce426f
 	srd	r5,r5,r6      /* Bring the bits back as zeros.  */
ce426f
+#endif
ce426f
 	cmpdi	cr7,r5,0      /* If r5 == 0, no c bytes have been found.  */
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
@@ -93,8 +98,14 @@
ce426f
 	   doubleword from the string.  Use that fact to find out what is
ce426f
 	   the position of the byte inside the string.  */
ce426f
 L(done):
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi    r0,r5,-1
ce426f
+	andc    r0,r0,r5
ce426f
+	popcntd	r0,r0	      /* Count trailing zeros.  */
ce426f
+#else
ce426f
 	cntlzd	r0,r5	      /* Count leading zeros before the match.  */
ce426f
-	srdi	r0,r0,3	      /* Convert leading zeroes to bytes.  */
ce426f
+#endif
ce426f
+	srdi	r0,r0,3	      /* Convert leading zeros to bytes.  */
ce426f
 	add	r3,r8,r0      /* Return address of the matching char.  */
ce426f
 	blr
ce426f
 END (BP_SYM (__rawmemchr))