ce426f
# commit 33ee81de05e83ce12f32a491270bb4c1611399c7
ce426f
# Author: Alan Modra <amodra@gmail.com>
ce426f
# Date:   Sat Aug 17 18:40:48 2013 +0930
ce426f
# 
ce426f
#     PowerPC LE strnlen
ce426f
#     http://sourceware.org/ml/libc-alpha/2013-08/msg00098.html
ce426f
#     
ce426f
#     The existing strnlen code has a number of defects, so this patch is more
ce426f
#     than just adding little-endian support.  The changes here are similar to
ce426f
#     those for memchr.
ce426f
#     
ce426f
#         * sysdeps/powerpc/powerpc64/power7/strnlen.S (strnlen): Add
ce426f
#         little-endian support.  Remove unnecessary "are we done" tests.
ce426f
#         Handle "s" wrapping around zero and extremely large "size".
ce426f
#         Correct main loop count.  Handle single left-over word from main
ce426f
#         loop inline rather than by using small_loop.  Correct comments.
ce426f
#         Delete "zero" tail, use "end_max" instead.
ce426f
#         * sysdeps/powerpc/powerpc32/power7/strnlen.S: Likewise.
ce426f
# 
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/strnlen.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/strnlen.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/strnlen.S	2014-05-28 12:40:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/power7/strnlen.S	2014-05-28 12:44:52.000000000 -0500
ce426f
@@ -30,51 +30,47 @@
ce426f
 	add	r7,r3,r4      /* Calculate the last acceptable address.  */
ce426f
 	cmplwi	r4,16
ce426f
 	li	r0,0	      /* Word with null chars.  */
ce426f
+	addi	r7,r7,-1
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	cmplw	cr7,r3,r7     /* Is the address equal or less than r3?  If
ce426f
-				 it's equal or less, it means size is either 0
ce426f
-				 or a negative number.  */
ce426f
-	ble	cr7,L(proceed)
ce426f
-
ce426f
-	li	r7,-1	      /* Make r11 the biggest if r4 <= 0.  */
ce426f
-L(proceed):
ce426f
 	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
 	lwz	r12,0(r8)     /* Load word from memory.  */
ce426f
 	cmpb	r10,r12,r0    /* Check for null bytes in DWORD1.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srw	r10,r10,r6
ce426f
+	slw	r10,r10,r6
ce426f
+#else
ce426f
 	slw	r10,r10,r6
ce426f
 	srw	r10,r10,r6
ce426f
+#endif
ce426f
 	cmplwi	cr7,r10,0     /* If r10 == 0, no null's 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(end_max)
ce426f
-
ce426f
+	clrrwi	r7,r7,2       /* Address of last word.  */
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 DWORD2 of pair.  */
ce426f
+	/* Handle WORD2 of pair.  */
ce426f
 	lwzu	r12,4(r8)
ce426f
 	cmpb	r10,r12,r0
ce426f
 	cmplwi	cr7,r10,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(end_max)
ce426f
-
ce426f
 L(loop_setup):
ce426f
-	sub	r5,r7,r9
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	r5,r7,r8
ce426f
 	srwi	r6,r5,3	      /* Number of loop iterations.  */
ce426f
 	mtctr	r6	      /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for the null byte backwards in the string.  Since
ce426f
+
ce426f
+	/* Main loop to look for the null byte in the string.  Since
ce426f
 	   it's a small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
 	.p2align  5
ce426f
 L(loop):
ce426f
@@ -90,15 +86,18 @@
ce426f
 	cmplwi	cr7,r5,0
ce426f
 	bne	cr7,L(found)
ce426f
 	bdnz	L(loop)
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for null in the whole range.  Just return
ce426f
-	   the original size.  */
ce426f
-	addi	r9,r8,4
ce426f
-	cmplw	cr6,r9,r7
ce426f
-	blt	cr6,L(loop_small)
ce426f
+
ce426f
+	/* We may have one more word to read.  */
ce426f
+	cmplw	cr6,r8,r7
ce426f
+	beq	cr6,L(end_max)
ce426f
+
ce426f
+	lwzu	r12,4(r8)
ce426f
+	cmpb	r10,r12,r0
ce426f
+	cmplwi	cr6,r10,0
ce426f
+	bne	cr6,L(done)
ce426f
 
ce426f
 L(end_max):
ce426f
-	sub	r3,r7,r3
ce426f
+	mr	r3,r4
ce426f
 	blr
ce426f
 
ce426f
 	/* OK, one (or both) of the words contains a null byte.  Check
ce426f
@@ -123,49 +122,56 @@
ce426f
 	   We need to make sure the null char is *before* the end of the
ce426f
 	   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
-	add	r9,r8,r0
ce426f
-	sub	r6,r9,r3      /* Length until the match.  */
ce426f
-	cmplw	r9,r7
ce426f
-	bgt	L(end_max)
ce426f
-	mr	r3,r6
ce426f
-	blr
ce426f
-
ce426f
-	.align	4
ce426f
-L(zero):
ce426f
-	li	r3,0
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi	r0,r10,-1
ce426f
+	andc	r0,r0,r10
ce426f
+	popcntw	r0,r0
ce426f
+#else
ce426f
+	cntlzw	r0,r10	      /* Count leading zeros before the match.  */
ce426f
+#endif
ce426f
+	sub	r3,r8,r3
ce426f
+	srwi	r0,r0,3	      /* Convert leading/trailing zeros to bytes.  */
ce426f
+	add	r3,r3,r0      /* Length until the match.  */
ce426f
+	cmplw	r3,r4
ce426f
+	blelr
ce426f
+	mr	r3,r4
ce426f
 	blr
ce426f
 
ce426f
-/* Deals with size <= 32.  */
ce426f
+/* Deals with size <= 16.  */
ce426f
 	.align	4
ce426f
 L(small_range):
ce426f
 	cmplwi	r4,0
ce426f
-	beq	L(zero)
ce426f
+	beq	L(end_max)
ce426f
+
ce426f
+	clrrwi	r7,r7,2       /* Address of last word.  */
ce426f
 
ce426f
 	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
ce426f
 	lwz	r12,0(r8)     /* Load word from memory.  */
ce426f
 	cmpb	r10,r12,r0    /* Check for null bytes in WORD1.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srw	r10,r10,r6
ce426f
+	slw	r10,r10,r6
ce426f
+#else
ce426f
 	slw	r10,r10,r6
ce426f
 	srw	r10,r10,r6
ce426f
+#endif
ce426f
 	cmplwi	cr7,r10,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	addi    r9,r8,4
ce426f
-	cmplw	r9,r7
ce426f
-	bge	L(end_max)
ce426f
-	b	L(loop_small)
ce426f
+	cmplw	r8,r7
ce426f
+	beq	L(end_max)
ce426f
 
ce426f
 	.p2align  5
ce426f
 L(loop_small):
ce426f
 	lwzu	r12,4(r8)
ce426f
 	cmpb	r10,r12,r0
ce426f
-	addi	r9,r8,4
ce426f
 	cmplwi	cr6,r10,0
ce426f
 	bne	cr6,L(done)
ce426f
-	cmplw	r9,r7
ce426f
-	bge	L(end_max)
ce426f
-	b	L(loop_small)
ce426f
+	cmplw	r8,r7
ce426f
+	bne	L(loop_small)
ce426f
+	mr	r3,r4
ce426f
+	blr
ce426f
+
ce426f
 END (BP_SYM (__strnlen))
ce426f
 weak_alias (BP_SYM (__strnlen), BP_SYM(strnlen))
ce426f
 libc_hidden_builtin_def (strnlen)
ce426f
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/strnlen.S glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/strnlen.S
ce426f
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/strnlen.S	2014-05-28 12:40:17.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/power7/strnlen.S	2014-05-28 13:24:41.000000000 -0500
ce426f
@@ -26,33 +26,29 @@
ce426f
 ENTRY (BP_SYM (__strnlen))
ce426f
 	CALL_MCOUNT 2
ce426f
 	dcbt	0,r3
ce426f
-	clrrdi  r8,r3,3
ce426f
+	clrrdi	r8,r3,3
ce426f
 	add	r7,r3,r4      /* Calculate the last acceptable address.  */
ce426f
 	cmpldi	r4,32
ce426f
 	li	r0,0	      /* Doubleword with null chars.  */
ce426f
+	addi	r7,r7,-1
ce426f
+
ce426f
 	/* If we have less than 33 bytes to search, skip to a faster code.  */
ce426f
 	ble	L(small_range)
ce426f
 
ce426f
-	cmpld	cr7,r3,r7    /* Is the address equal or less than r3?  If
ce426f
-				it's equal or less, it means size is either 0
ce426f
-				or a negative number.  */
ce426f
-	ble	cr7,L(proceed)
ce426f
-
ce426f
-	li	r7,-1	      /* Make r11 the biggest if r4 <= 0.  */
ce426f
-L(proceed):
ce426f
 	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
 	ld	r12,0(r8)     /* Load doubleword from memory.  */
ce426f
 	cmpb	r10,r12,r0    /* Check for null bytes in DWORD1.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srd	r10,r10,r6
ce426f
+	sld	r10,r10,r6
ce426f
+#else
ce426f
 	sld	r10,r10,r6
ce426f
 	srd	r10,r10,r6
ce426f
+#endif
ce426f
 	cmpldi	cr7,r10,0     /* If r10 == 0, no null's 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
-	bge	cr6,L(end_max)
ce426f
-
ce426f
+	clrrdi	r7,r7,3       /* Address of last doubleword.  */
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
@@ -65,17 +61,18 @@
ce426f
 	cmpldi	cr7,r10,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(end_max)
ce426f
-
ce426f
 L(loop_setup):
ce426f
-	sub	r5,r7,r9
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	r5,r7,r8
ce426f
 	srdi	r6,r5,4	      /* Number of loop iterations.  */
ce426f
 	mtctr	r6	      /* Setup the counter.  */
ce426f
-	b	L(loop)
ce426f
-	/* Main loop to look for the null byte backwards in the string.  Since
ce426f
+
ce426f
+	/* Main loop to look for the null byte in the string.  Since
ce426f
 	   it's a small loop (< 8 instructions), align it to 32-bytes.  */
ce426f
 	.p2align  5
ce426f
 L(loop):
ce426f
@@ -91,15 +88,18 @@
ce426f
 	cmpldi	cr7,r5,0
ce426f
 	bne	cr7,L(found)
ce426f
 	bdnz	L(loop)
ce426f
-	/* We're here because the counter reached 0, and that means we
ce426f
-	   didn't have any matches for null in the whole range.  Just return
ce426f
-	   the original size.  */
ce426f
-	addi	r9,r8,8
ce426f
-	cmpld	cr6,r9,r7
ce426f
-	blt	cr6,L(loop_small)
ce426f
+
ce426f
+	/* We may have one more dword to read.  */
ce426f
+	cmpld	cr6,r8,r7
ce426f
+	beq	cr6,L(end_max)
ce426f
+
ce426f
+	ldu	r12,8(r8)
ce426f
+	cmpb	r10,r12,r0
ce426f
+	cmpldi	cr6,r10,0
ce426f
+	bne	cr6,L(done)
ce426f
 
ce426f
 L(end_max):
ce426f
-	sub	r3,r7,r3
ce426f
+	mr	r3,r4
ce426f
 	blr
ce426f
 
ce426f
 	/* OK, one (or both) of the doublewords contains a null byte.  Check
ce426f
@@ -121,52 +121,59 @@
ce426f
 	/* r10 has the output of the cmpb instruction, that is, it contains
ce426f
 	   0xff in the same position as the null byte in the original
ce426f
 	   doubleword from the string.  Use that to calculate the length.
ce426f
-	   We need to make sure the null char is *before* the start of the
ce426f
-	   range (since we're going backwards).  */
ce426f
+	   We need to make sure the null char is *before* the end of the
ce426f
+	   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
-	add	r9,r8,r0
ce426f
-	sub	r6,r9,r3      /* Length until the match.  */
ce426f
-	cmpld	r9,r7
ce426f
-	bgt	L(end_max)
ce426f
-	mr	r3,r6
ce426f
-	blr
ce426f
-
ce426f
-	.align	4
ce426f
-L(zero):
ce426f
-	li	r3,0
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	addi	r0,r10,-1
ce426f
+	andc	r0,r0,r10
ce426f
+	popcntd	r0,r0
ce426f
+#else
ce426f
+	cntlzd	r0,r10	      /* Count leading zeros before the match.  */
ce426f
+#endif
ce426f
+	sub	r3,r8,r3
ce426f
+	srdi	r0,r0,3	      /* Convert leading/trailing zeros to bytes.  */
ce426f
+	add	r3,r3,r0      /* Length until the match.  */
ce426f
+	cmpld	r3,r4
ce426f
+	blelr
ce426f
+	mr	r3,r4
ce426f
 	blr
ce426f
 
ce426f
 /* Deals with size <= 32.  */
ce426f
 	.align	4
ce426f
 L(small_range):
ce426f
 	cmpldi	r4,0
ce426f
-	beq	L(zero)
ce426f
+	beq	L(end_max)
ce426f
+
ce426f
+	clrrdi	r7,r7,3       /* Address of last doubleword.  */
ce426f
 
ce426f
 	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
ce426f
-	ld	r12,0(r8)     /* Load word from memory.  */
ce426f
+	ld	r12,0(r8)     /* Load doubleword from memory.  */
ce426f
 	cmpb	r10,r12,r0    /* Check for null bytes in DWORD1.  */
ce426f
+#ifdef __LITTLE_ENDIAN__
ce426f
+	srd	r10,r10,r6
ce426f
+	sld	r10,r10,r6
ce426f
+#else
ce426f
 	sld	r10,r10,r6
ce426f
 	srd	r10,r10,r6
ce426f
+#endif
ce426f
 	cmpldi	cr7,r10,0
ce426f
 	bne	cr7,L(done)
ce426f
 
ce426f
-	addi    r9,r8,8
ce426f
-	cmpld	r9,r7
ce426f
-	bge	L(end_max)
ce426f
-	b	L(loop_small)
ce426f
+	cmpld	r8,r7
ce426f
+	beq	L(end_max)
ce426f
 
ce426f
 	.p2align  5
ce426f
 L(loop_small):
ce426f
 	ldu	r12,8(r8)
ce426f
 	cmpb	r10,r12,r0
ce426f
-	addi	r9,r8,8
ce426f
 	cmpldi	cr6,r10,0
ce426f
 	bne	cr6,L(done)
ce426f
-	cmpld	r9,r7
ce426f
-	bge	L(end_max)
ce426f
-	b	L(loop_small)
ce426f
+	cmpld	r8,r7
ce426f
+	bne	L(loop_small)
ce426f
+	mr	r3,r4
ce426f
+	blr
ce426f
+
ce426f
 END (BP_SYM (__strnlen))
ce426f
 weak_alias (BP_SYM (__strnlen), BP_SYM(strnlen))
ce426f
 libc_hidden_builtin_def (strnlen)