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