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