Blame sysdeps/powerpc/powerpc64/power7/rawmemchr.S

Packit 6c4009
/* Optimized rawmemchr implementation for PowerPC64/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] rawmemchr (void *s [r3], int c [r4])  */
Packit 6c4009
Packit 6c4009
#ifndef RAWMEMCHR
Packit 6c4009
# define RAWMEMCHR __rawmemchr
Packit 6c4009
#endif
Packit 6c4009
	.machine  power7
Packit 6c4009
ENTRY_TOCLESS (RAWMEMCHR)
Packit 6c4009
	CALL_MCOUNT 2
Packit 6c4009
	dcbt	0,r3
Packit 6c4009
	clrrdi	r8,r3,3	      /* Align the address to doubleword boundary.  */
Packit 6c4009
Packit 6c4009
	/* Replicate byte to doubleword.  */
Packit 6c4009
	insrdi	r4,r4,8,48
Packit 6c4009
	insrdi	r4,r4,16,32
Packit 6c4009
	insrdi	r4,r4,32,0
Packit 6c4009
Packit 6c4009
	/* Now r4 has a doubleword of c bytes.  */
Packit 6c4009
Packit 6c4009
	rlwinm	r6,r3,3,26,28 /* Calculate padding.  */
Packit 6c4009
	ld	r12,0(r8)     /* Load doubleword from memory.  */
Packit 6c4009
	cmpb	r5,r12,r4     /* Compare each byte against c byte.  */
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	srd	r5,r5,r6
Packit 6c4009
	sld	r5,r5,r6
Packit 6c4009
#else
Packit 6c4009
	sld	r5,r5,r6      /* Move left to discard ignored bits.  */
Packit 6c4009
	srd	r5,r5,r6      /* Bring the bits back as zeros.  */
Packit 6c4009
#endif
Packit 6c4009
	cmpdi	cr7,r5,0      /* If r5 == 0, no c bytes 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 quadword boundary?  If so, skip to
Packit 6c4009
	   the main loop.  Otherwise, go through the alignment code.  */
Packit 6c4009
Packit 6c4009
	bt	28,L(loop)
Packit 6c4009
Packit 6c4009
	/* Handle DWORD2 of pair.  */
Packit 6c4009
	ldu	r12,8(r8)
Packit 6c4009
	cmpb	r5,r12,r4
Packit 6c4009
	cmpdi	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
	/* 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):
Packit 6c4009
	/* Load two doublewords, compare and merge in a
Packit 6c4009
	   single register for speed.  This is an attempt
Packit 6c4009
	   to speed up the byte-checking process for bigger strings.  */
Packit 6c4009
	ld	r12,8(r8)
Packit 6c4009
	ldu     r11,16(r8)
Packit 6c4009
	cmpb	r5,r12,r4
Packit 6c4009
	cmpb	r6,r11,r4
Packit 6c4009
	or	r7,r5,r6
Packit 6c4009
	cmpdi	cr7,r7,0
Packit 6c4009
	beq	cr7,L(loop)
Packit 6c4009
Packit 6c4009
	/* OK, one (or both) of the doublewords contains a 'c' byte.  Check
Packit 6c4009
	   the first doubleword and decrement the address in case the first
Packit 6c4009
	   doubleword really contains a c byte.  */
Packit 6c4009
Packit 6c4009
	cmpdi	cr6,r5,0
Packit 6c4009
	addi	r8,r8,-8
Packit 6c4009
	bne	cr6,L(done)
Packit 6c4009
Packit 6c4009
	/* The 'c' byte must be in the second doubleword.  Adjust the address
Packit 6c4009
	   again and move the result of cmpb to r10 so we can calculate the
Packit 6c4009
	   pointer.  */
Packit 6c4009
	mr	r5,r6
Packit 6c4009
	addi	r8,r8,8
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' byte in the original
Packit 6c4009
	   doubleword from the string.  Use that fact to find out what is
Packit 6c4009
	   the position of the byte inside the string.  */
Packit 6c4009
L(done):
Packit 6c4009
#ifdef __LITTLE_ENDIAN__
Packit 6c4009
	addi    r0,r5,-1
Packit 6c4009
	andc    r0,r0,r5
Packit 6c4009
	popcntd	r0,r0	      /* Count trailing zeros.  */
Packit 6c4009
#else
Packit 6c4009
	cntlzd	r0,r5	      /* Count leading zeros before the match.  */
Packit 6c4009
#endif
Packit 6c4009
	srdi	r0,r0,3	      /* Convert leading zeros to bytes.  */
Packit 6c4009
	add	r3,r8,r0      /* Return address of the matching char.  */
Packit 6c4009
	blr
Packit 6c4009
END (RAWMEMCHR)
Packit 6c4009
weak_alias (__rawmemchr,rawmemchr)
Packit 6c4009
libc_hidden_builtin_def (__rawmemchr)