00db10
From 2ffa8b8660a7a17572ae5a398171c8be59985eb3 Mon Sep 17 00:00:00 2001
00db10
From: "Gabriel F. T. Gomes" <gftg@linux.vnet.ibm.com>
00db10
Date: Mon, 25 Jan 2016 10:50:34 -0500
00db10
Subject: [PATCH] powerpc: Zero pad using memset in strncpy/stpncpy
00db10
00db10
Call __memset_power8 to pad, with zeros, the remaining bytes in the
00db10
dest string on __strncpy_power8 and __stpncpy_power8.  This improves
00db10
performance when n is larger than the input string, giving ~30% gain for
00db10
larger strings without impacting much shorter strings.
00db10
00db10
(cherry picked from commit 72c11b353ede72931cc474c9071d143d9a05c0d7)
00db10
---
00db10
 ChangeLog                                  |   5 ++
00db10
 sysdeps/powerpc/powerpc64/power8/strncpy.S | 123 +++++++++++++----------------
00db10
 2 files changed, 61 insertions(+), 67 deletions(-)
00db10
00db10
diff --git a/ChangeLog b/ChangeLog
00db10
index 5537fc6..8d0e296 100644
00db10
diff --git a/sysdeps/powerpc/powerpc64/power8/strncpy.S b/sysdeps/powerpc/powerpc64/power8/strncpy.S
00db10
index 5fda953..80136cc 100644
00db10
--- a/sysdeps/powerpc/powerpc64/power8/strncpy.S
00db10
+++ b/sysdeps/powerpc/powerpc64/power8/strncpy.S
00db10
@@ -24,6 +24,8 @@
00db10
 # define FUNC_NAME strncpy
00db10
 #endif
00db10
 
00db10
+#define FRAMESIZE (FRAME_MIN_SIZE+48)
00db10
+
00db10
 /* Implements the function
00db10
 
00db10
    char * [r3] strncpy (char *dest [r3], const char *src [r4], size_t n [r5])
00db10
@@ -54,8 +56,7 @@ EALIGN (FUNC_NAME, 4, 0)
00db10
 	addi	r10,r4,16
00db10
 	rlwinm	r9,r4,0,19,19
00db10
 
00db10
-	/* Since it is a leaf function, save some non-volatile registers on the
00db10
-	   protected/red zone.  */
00db10
+	/* Save some non-volatile registers on the stack.  */
00db10
 	std	r26,-48(r1)
00db10
 	std	r27,-40(r1)
00db10
 
00db10
@@ -69,6 +70,14 @@ EALIGN (FUNC_NAME, 4, 0)
00db10
 	std	r30,-16(r1)
00db10
 	std	r31,-8(r1)
00db10
 
00db10
+	/* Update CFI.  */
00db10
+	cfi_offset(r26, -48)
00db10
+	cfi_offset(r27, -40)
00db10
+	cfi_offset(r28, -32)
00db10
+	cfi_offset(r29, -24)
00db10
+	cfi_offset(r30, -16)
00db10
+	cfi_offset(r31, -8)
00db10
+
00db10
 	beq	cr7,L(unaligned_lt_16)
00db10
 	rldicl	r9,r4,0,61
00db10
 	subfic	r8,r9,8
00db10
@@ -144,74 +153,58 @@ L(short_path_loop_end):
00db10
 	ld	r31,-8(r1)
00db10
 	blr
00db10
 
00db10
-	/* This code pads the remainder dest with NULL bytes.  The algorithm
00db10
-	   calculate the remanining size and issues a doubleword unrolled
00db10
-	   loops followed by a byte a byte set.  */
00db10
+	/* This code pads the remainder of dest with NULL bytes.  The algorithm
00db10
+	   calculates the remaining size and calls memset.  */
00db10
 	.align	4
00db10
 L(zero_pad_start):
00db10
 	mr	r5,r10
00db10
 	mr	r9,r6
00db10
 L(zero_pad_start_1):
00db10
-	srdi.	r8,r5,r3
00db10
-	mr	r10,r9
00db10
-#ifdef USE_AS_STPNCPY
00db10
-	mr	r3,r9
00db10
+	/* At this point:
00db10
+	     - r5 holds the number of bytes that still have to be written to
00db10
+	       dest.
00db10
+	     - r9 points to the position, in dest, where the first null byte
00db10
+	       will be written.
00db10
+	   The above statements are true both when control reaches this label
00db10
+	   from a branch or when falling through the previous lines.  */
00db10
+#ifndef USE_AS_STPNCPY
00db10
+	mr	r30,r3       /* Save the return value of strncpy.  */
00db10
+#endif
00db10
+	/* Prepare the call to memset.  */
00db10
+	mr	r3,r9        /* Pointer to the area to be zero-filled.  */
00db10
+	li	r4,0         /* Byte to be written (zero).  */
00db10
+
00db10
+	/* We delayed the creation of the stack frame, as well as the saving of
00db10
+	   the link register, because only at this point, we are sure that
00db10
+	   doing so is actually needed.  */
00db10
+
00db10
+	/* Save the link register.  */
00db10
+	mflr	r0
00db10
+	std	r0,16(r1)
00db10
+	cfi_offset(lr, 16)
00db10
+
00db10
+	/* Create the stack frame.  */
00db10
+	stdu	r1,-FRAMESIZE(r1)
00db10
+	cfi_adjust_cfa_offset(FRAMESIZE)
00db10
+
00db10
+	bl	__memset_power8
00db10
+	nop
00db10
+
00db10
+	/* Restore the stack frame.  */
00db10
+	addi	r1,r1,FRAMESIZE
00db10
+	cfi_adjust_cfa_offset(-FRAMESIZE)
00db10
+	/* Restore the link register.  */
00db10
+	ld	r0,16(r1)
00db10
+	mtlr	r0
00db10
+
00db10
+#ifndef USE_AS_STPNCPY
00db10
+	mr	r3,r30       /* Restore the return value of strncpy, i.e.:
00db10
+				dest.  For stpncpy, the return value is the
00db10
+				same as return value of memset.  */
00db10
 #endif
00db10
-	beq-	cr0,L(zero_pad_loop_b_start)
00db10
-	cmpldi	cr7,r8,1
00db10
-	li	cr7,0
00db10
-	std	r7,0(r9)
00db10
-	beq	cr7,L(zero_pad_loop_b_prepare)
00db10
-	addic.	r8,r8,-2
00db10
-	addi	r10,r9,r16
00db10
-	std	r7,8(r9)
00db10
-	beq	cr0,L(zero_pad_loop_dw_2)
00db10
-	std	r7,16(r9)
00db10
-	li	r9,0
00db10
-	b	L(zero_pad_loop_dw_1)
00db10
-
00db10
-	.align	4
00db10
-L(zero_pad_loop_dw):
00db10
-	addi	r10,r10,16
00db10
-	std	r9,-8(r10)
00db10
-	beq	cr0,L(zero_pad_loop_dw_2)
00db10
-	std	r9,0(r10)
00db10
-L(zero_pad_loop_dw_1):
00db10
-	cmpldi	cr7,r8,1
00db10
-	std	r9,0(r10)
00db10
-	addic.	r8,r8,-2
00db10
-	bne	cr7,L(zero_pad_loop_dw)
00db10
-	addi	r10,r10,8
00db10
-L(zero_pad_loop_dw_2):
00db10
-	rldicl	r5,r5,0,61
00db10
-L(zero_pad_loop_b_start):
00db10
-	cmpdi	cr7,r5,0
00db10
-	addi	r5,r5,-1
00db10
-	addi	r9,r10,-1
00db10
-	add	r10,r10,5
00db10
-	subf	r10,r9,r10
00db10
-	li	r8,0
00db10
-	beq-	cr7,L(short_path_loop_end)
00db10
-
00db10
-	/* Write remaining 1-8 bytes.  */
00db10
-        .align  4
00db10
-	addi	r9,r9,1
00db10
-	mtocrf	0x1,r10
00db10
-	bf	29,4f
00db10
-        stw     r8,0(r9)
00db10
-        addi	r9,r9,4
00db10
-
00db10
-        .align  4
00db10
-4:      bf      30,2f
00db10
-        sth     r8,0(r9)
00db10
-        addi	r9,r9,2
00db10
-
00db10
-        .align  4
00db10
-2:      bf	31,1f
00db10
-        stb	r8,0(r9)
00db10
 
00db10
-	/* Restore non-volatile registers.  */
00db10
-1:	ld	r26,-48(r1)
00db10
+	/* Restore non-volatile registers and return.  */
00db10
+	ld	r26,-48(r1)
00db10
 	ld	r27,-40(r1)
00db10
 	ld	r28,-32(r1)
00db10
 	ld	r29,-24(r1)
00db10
@@ -407,10 +400,6 @@ L(short_path_prepare_2_3):
00db10
 	mr	r4,r28
00db10
 	mr	r9,r29
00db10
 	b	L(short_path_2)
00db10
-L(zero_pad_loop_b_prepare):
00db10
-	addi	r10,r9,8
00db10
-	rldicl	r5,r5,0,61
00db10
-	b	L(zero_pad_loop_b_start)
00db10
 L(zero_pad_start_prepare_1):
00db10
 	mr	r5,r6
00db10
 	mr	r9,r8
00db10
-- 
00db10
2.1.0
00db10