Blame sysdeps/powerpc/powerpc32/power7/strchr.S

Packit 6c4009
/* Optimized strchr implementation for PowerPC32/POWER7 using cmpb insn.
Packit 6c4009
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit 6c4009
   Contributed by Luis Machado <luisgpm@br.ibm.com>.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
/* int [r3] strchr (char *s [r3], int c [r4])  */
Packit 6c4009
	.machine  power7
Packit 6c4009
ENTRY (strchr)
Packit 6c4009
	CALL_MCOUNT
Packit 6c4009
	dcbt	0,r3
Packit 6c4009
	clrrwi	r8,r3,2	      /* Align the address to word boundary.  */
Packit 6c4009
	cmpwi	cr7,r4,0
Packit 6c4009
	lwz	r12,0(r8)     /* Load word from memory.  */
Packit 6c4009
	li	r0,0	      /* Word with null chars to use
Packit 6c4009
				 with cmpb.  */
Packit 6c4009
Packit 6c4009
	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
Packit 6c4009
Packit 6c4009
	beq	cr7,L(null_match)
Packit 6c4009
Packit 6c4009
	/* Replicate byte to word.  */
Packit 6c4009
	insrwi	r4,r4,8,16
Packit 6c4009
	insrwi	r4,r4,16,0
Packit 6c4009
Packit 6c4009
	/* Now r4 has a word of c bytes and r0 has
Packit 6c4009
	   a word of null bytes.  */
Packit 6c4009
Packit 6c4009
	cmpb	r10,r12,r4     /* Compare each byte against c byte.  */
Packit 6c4009
	cmpb	r11,r12,r0     /* Compare each byte against null byte.  */
Packit 6c4009
Packit 6c4009
	/* Move the words left and right to discard the bits that are
Packit 6c4009
	   not part of the string and to bring them back as zeros.  */
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	srw	r10,r10,r6
Packit 6c4009
	srw	r11,r11,r6
Packit 6c4009
	slw	r10,r10,r6
Packit 6c4009
	slw	r11,r11,r6
Packit 6c4009
#else
Packit 6c4009
	slw	r10,r10,r6
Packit 6c4009
	slw	r11,r11,r6
Packit 6c4009
	srw	r10,r10,r6
Packit 6c4009
	srw	r11,r11,r6
Packit 6c4009
#endif
Packit 6c4009
	or	r5,r10,r11    /* OR the results to speed things up.  */
Packit 6c4009
	cmpwi	cr7,r5,0      /* If r5 == 0, no c or null bytes
Packit 6c4009
				 have been found.  */
Packit 6c4009
	bne	cr7,L(done)
Packit 6c4009
Packit 6c4009
	mtcrf   0x01,r8
Packit 6c4009
Packit 6c4009
	/* Are we now aligned to a doubleword boundary?  If so, skip to
Packit 6c4009
	   the main loop.  Otherwise, go through the alignment code.  */
Packit 6c4009
Packit 6c4009
	bt	29,L(loop)
Packit 6c4009
Packit 6c4009
	/* Handle WORD2 of pair.  */
Packit 6c4009
	lwzu	r12,4(r8)
Packit 6c4009
	cmpb	r10,r12,r4
Packit 6c4009
	cmpb	r11,r12,r0
Packit 6c4009
	or	r5,r10,r11
Packit 6c4009
	cmpwi	cr7,r5,0
Packit 6c4009
	bne	cr7,L(done)
Packit 6c4009
	b	L(loop)	      /* We branch here (rather than falling through)
Packit 6c4009
				 to skip the nops due to heavy alignment
Packit 6c4009
				 of the loop below.  */
Packit 6c4009
Packit 6c4009
	.p2align  5
Packit 6c4009
L(loop):
Packit 6c4009
	/* Load two words, compare and merge in a
Packit 6c4009
	   single register for speed.  This is an attempt
Packit 6c4009
	   to speed up the null-checking process for bigger strings.  */
Packit 6c4009
	lwz	r12,4(r8)
Packit 6c4009
	lwzu	r9,8(r8)
Packit 6c4009
	cmpb	r10,r12,r4
Packit 6c4009
	cmpb	r11,r12,r0
Packit 6c4009
	cmpb	r6,r9,r4
Packit 6c4009
	cmpb	r7,r9,r0
Packit 6c4009
	or	r12,r10,r11
Packit 6c4009
	or	r9,r6,r7
Packit 6c4009
	or	r5,r12,r9
Packit 6c4009
	cmpwi	cr7,r5,0
Packit 6c4009
	beq	cr7,L(loop)
Packit 6c4009
Packit 6c4009
	/* OK, one (or both) of the words contains a c/null byte.  Check
Packit 6c4009
	   the first word and decrement the address in case the first
Packit 6c4009
	   word really contains a c/null byte.  */
Packit 6c4009
Packit 6c4009
	cmpwi	cr6,r12,0
Packit 6c4009
	addi	r8,r8,-4
Packit 6c4009
	bne	cr6,L(done)
Packit 6c4009
Packit 6c4009
	/* The c/null byte must be in the second word.  Adjust the address
Packit 6c4009
	   again and move the result of cmpb to r10/r11 so we can calculate
Packit 6c4009
	   the pointer.  */
Packit 6c4009
Packit 6c4009
	mr	r10,r6
Packit 6c4009
	mr	r11,r7
Packit 6c4009
	addi	r8,r8,4
Packit 6c4009
Packit 6c4009
	/* r10/r11 have the output of the cmpb instructions, that is,
Packit 6c4009
	   0xff in the same position as the c/null byte in the original
Packit 6c4009
	   word from the string.  Use that to calculate the pointer.  */
Packit 6c4009
L(done):
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	addi    r3,r10,-1
Packit 6c4009
	andc    r3,r3,r10
Packit 6c4009
	popcntw	r0,r3
Packit 6c4009
	addi    r4,r11,-1
Packit 6c4009
	andc    r4,r4,r11
Packit 6c4009
	cmplw	cr7,r3,r4
Packit 6c4009
	bgt	cr7,L(no_match)
Packit 6c4009
#else
Packit 6c4009
	cntlzw	r0,r10	      /* Count leading zeros before c matches.  */
Packit 6c4009
	cmplw	cr7,r11,r10
Packit 6c4009
	bgt	cr7,L(no_match)
Packit 6c4009
#endif
Packit 6c4009
	srwi	r0,r0,3	      /* Convert leading zeros to bytes.  */
Packit 6c4009
	add	r3,r8,r0      /* Return address of the matching c byte
Packit 6c4009
				 or null in case c was not found.  */
Packit 6c4009
	blr
Packit 6c4009
Packit 6c4009
	.align	4
Packit 6c4009
L(no_match):
Packit 6c4009
	li	r3,0
Packit 6c4009
	blr
Packit 6c4009
Packit 6c4009
/* We are here because strchr was called with a null byte.  */
Packit 6c4009
	.align	4
Packit 6c4009
L(null_match):
Packit 6c4009
	/* r0 has a word of null bytes.  */
Packit 6c4009
Packit 6c4009
	cmpb	r5,r12,r0     /* Compare each byte against null bytes.  */
Packit 6c4009
Packit 6c4009
	/* Move the words left and right to discard the bits that are
Packit 6c4009
	   not part of the string and bring them back as zeros.  */
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	srw	r5,r5,r6
Packit 6c4009
	slw	r5,r5,r6
Packit 6c4009
#else
Packit 6c4009
	slw	r5,r5,r6
Packit 6c4009
	srw	r5,r5,r6
Packit 6c4009
#endif
Packit 6c4009
	cmpwi	cr7,r5,0      /* If r10 == 0, no c or null bytes
Packit 6c4009
				 have been found.  */
Packit 6c4009
	bne	cr7,L(done_null)
Packit 6c4009
Packit 6c4009
	mtcrf   0x01,r8
Packit 6c4009
Packit 6c4009
	/* Are we now aligned to a doubleword boundary?  If so, skip to
Packit 6c4009
	   the main loop.  Otherwise, go through the alignment code.  */
Packit 6c4009
Packit 6c4009
	bt	29,L(loop_null)
Packit 6c4009
Packit 6c4009
	/* Handle WORD2 of pair.  */
Packit 6c4009
	lwzu	r12,4(r8)
Packit 6c4009
	cmpb    r5,r12,r0
Packit 6c4009
	cmpwi	cr7,r5,0
Packit 6c4009
	bne	cr7,L(done_null)
Packit 6c4009
	b	L(loop_null)  /* We branch here (rather than falling through)
Packit 6c4009
				 to skip the nops due to heavy alignment
Packit 6c4009
				 of the loop below.  */
Packit 6c4009
Packit 6c4009
	/* Main loop to look for the end of the string.  Since it's a
Packit 6c4009
	   small loop (< 8 instructions), align it to 32-bytes.  */
Packit 6c4009
	.p2align  5
Packit 6c4009
L(loop_null):
Packit 6c4009
	/* Load two words, compare and merge in a
Packit 6c4009
	   single register for speed.  This is an attempt
Packit 6c4009
	   to speed up the null-checking process for bigger strings.  */
Packit 6c4009
	lwz	r12,4(r8)
Packit 6c4009
	lwzu    r11,8(r8)
Packit 6c4009
	cmpb	r5,r12,r0
Packit 6c4009
	cmpb	r10,r11,r0
Packit 6c4009
	or	r6,r5,r10
Packit 6c4009
	cmpwi	cr7,r6,0
Packit 6c4009
	beq	cr7,L(loop_null)
Packit 6c4009
Packit 6c4009
	/* OK, one (or both) of the words contains a null byte.  Check
Packit 6c4009
	   the first word and decrement the address in case the first
Packit 6c4009
	   word really contains a null byte.  */
Packit 6c4009
Packit 6c4009
	cmpwi	cr6,r5,0
Packit 6c4009
	addi	r8,r8,-4
Packit 6c4009
	bne	cr6,L(done_null)
Packit 6c4009
Packit 6c4009
	/* The null byte must be in the second word.  Adjust the address
Packit 6c4009
	   again and move the result of cmpb to r10 so we can calculate the
Packit 6c4009
	   pointer.  */
Packit 6c4009
Packit 6c4009
	mr	r5,r10
Packit 6c4009
	addi	r8,r8,4
Packit 6c4009
Packit 6c4009
	/* r5 has the output of the cmpb instruction, that is, it contains
Packit 6c4009
	   0xff in the same position as the null byte in the original
Packit 6c4009
	   word from the string.  Use that to calculate the pointer.  */
Packit 6c4009
L(done_null):
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	addi    r0,r5,-1
Packit 6c4009
	andc    r0,r0,r5
Packit 6c4009
	popcntw	r0,r0
Packit 6c4009
#else
Packit 6c4009
	cntlzw	r0,r5	      /* Count leading zeros before the match.  */
Packit 6c4009
#endif
Packit 6c4009
	srwi	r0,r0,3	      /* Convert leading zeros to bytes.  */
Packit 6c4009
	add	r3,r8,r0      /* Return address of the matching null byte.  */
Packit 6c4009
	blr
Packit 6c4009
END (strchr)
Packit 6c4009
weak_alias (strchr, index)
Packit 6c4009
libc_hidden_builtin_def (strchr)