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

Packit 6c4009
/* Optimized strchrnul 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] strchrnul (char *s [r3], int c [r4])  */
Packit 6c4009
	.machine  power7
Packit 6c4009
ENTRY (__strchrnul)
Packit 6c4009
	CALL_MCOUNT
Packit 6c4009
	dcbt	0,r3
Packit 6c4009
	clrrwi	r8,r3,2	      /* Align the address to word boundary.  */
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
	rlwinm	r6,r3,3,27,28 /* Calculate padding.  */
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
	/* 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,r0    /* Compare each byte against c byte.  */
Packit 6c4009
	cmpb	r9,r12,r4     /* 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 bring them back as zeros.  */
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	srw	r10,r10,r6
Packit 6c4009
	srw	r9,r9,r6
Packit 6c4009
	slw	r10,r10,r6
Packit 6c4009
	slw	r9,r9,r6
Packit 6c4009
#else
Packit 6c4009
	slw	r10,r10,r6
Packit 6c4009
	slw	r9,r9,r6
Packit 6c4009
	srw	r10,r10,r6
Packit 6c4009
	srw	r9,r9,r6
Packit 6c4009
#endif
Packit 6c4009
	or	r5,r9,r10     /* 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,r0
Packit 6c4009
	cmpb	r9,r12,r4
Packit 6c4009
	or	r5,r9,r10
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	r11,8(r8)
Packit 6c4009
	cmpb	r10,r12,r0
Packit 6c4009
	cmpb	r9,r12,r4
Packit 6c4009
	cmpb	r6,r11,r0
Packit 6c4009
	cmpb	r7,r11,r4
Packit 6c4009
	or	r5,r9,r10
Packit 6c4009
	or	r10,r6,r7
Packit 6c4009
	or	r11,r5,r10
Packit 6c4009
	cmpwi	cr7,r11,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,r5,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 r5 so we can calculate the
Packit 6c4009
	   pointer.  */
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 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    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 matching c/null byte.  */
Packit 6c4009
	blr
Packit 6c4009
END (__strchrnul)
Packit 6c4009
weak_alias (__strchrnul,strchrnul)
Packit 6c4009
libc_hidden_builtin_def (__strchrnul)