Blame sysdeps/ia64/strchr.S

Packit 6c4009
/* Optimized version of the standard strchr() function.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   Contributed by Dan Pop <Dan.Pop@cern.ch>.
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
/* Return: the address of the first occurence of chr in str or NULL
Packit 6c4009
Packit 6c4009
   Inputs:
Packit 6c4009
        in0:    str
Packit 6c4009
        in1:    chr
Packit 6c4009
Packit 6c4009
   A modified version of memchr.S, the search ends when the character is
Packit 6c4009
   found or the terminating null character is encountered.
Packit 6c4009
Packit 6c4009
   This implementation assumes little endian mode.  For big endian mode,
Packit 6c4009
   the instruction czx1.r should be replaced by czx1.l.  */
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#undef ret
Packit 6c4009
Packit 6c4009
#define saved_lc	r18
Packit 6c4009
#define poschr		r19
Packit 6c4009
#define pos0		r20
Packit 6c4009
#define val1		r21
Packit 6c4009
#define val2		r22
Packit 6c4009
#define tmp		r24
Packit 6c4009
#define chrx8		r25
Packit 6c4009
#define loopcnt		r30
Packit 6c4009
Packit 6c4009
#define str		in0
Packit 6c4009
#define chr		in1
Packit 6c4009
Packit 6c4009
ENTRY(strchr)
Packit 6c4009
	.prologue
Packit 6c4009
	alloc r2 = ar.pfs, 2, 0, 0, 0
Packit 6c4009
	.save ar.lc, saved_lc
Packit 6c4009
        mov 	saved_lc = ar.lc 	// save the loop counter
Packit 6c4009
	.body
Packit 6c4009
	mov 	ret0 = str
Packit 6c4009
	and 	tmp = 7, str		// tmp = str % 8
Packit 6c4009
	mux1	chrx8 = chr, @brcst
Packit 6c4009
	extr.u	chr = chr, 0, 8		// retain only the last byte
Packit 6c4009
	cmp.ne	p8, p0 = r0, r0		// clear p8
Packit 6c4009
	;;
Packit 6c4009
	sub	loopcnt = 8, tmp	// loopcnt = 8 - tmp
Packit 6c4009
	cmp.eq	p6, p0 = tmp, r0
Packit 6c4009
(p6)	br.cond.sptk	.str_aligned;;
Packit 6c4009
	adds	loopcnt = -1, loopcnt;;
Packit 6c4009
	mov	ar.lc = loopcnt
Packit 6c4009
.l1:
Packit 6c4009
	ld1	val2 = [ret0], 1
Packit 6c4009
	;;
Packit 6c4009
	cmp.eq	p6, p0 = val2, chr
Packit 6c4009
	cmp.eq	p7, p0 = val2, r0
Packit 6c4009
(p6)	br.cond.spnt	.restore_and_exit
Packit 6c4009
(p7)	br.cond.spnt	.notfound
Packit 6c4009
	br.cloop.sptk	.l1
Packit 6c4009
.str_aligned:
Packit 6c4009
	ld8	val1 = [ret0], 8;;
Packit 6c4009
	nop.b	0
Packit 6c4009
	nop.b 	0
Packit 6c4009
.l2:
Packit 6c4009
	ld8.s	val2 = [ret0], 8	// don't bomb out here
Packit 6c4009
	czx1.r	pos0 = val1
Packit 6c4009
	xor	tmp = val1, chrx8	// if val1 contains chr, tmp will
Packit 6c4009
	;;				// contain a zero in its position
Packit 6c4009
	czx1.r	poschr = tmp
Packit 6c4009
	cmp.ne	p6, p0 = 8, pos0
Packit 6c4009
	;;
Packit 6c4009
	cmp.ne	p7, p0 = 8, poschr
Packit 6c4009
(p7)	br.cond.spnt .foundit
Packit 6c4009
(p6)	br.cond.spnt .notfound
Packit 6c4009
	chk.s	val2, .recovery
Packit 6c4009
.back:
Packit 6c4009
	mov	val1 = val2
Packit 6c4009
	br.cond.dptk .l2
Packit 6c4009
.foundit:
Packit 6c4009
(p6)	cmp.lt	p8, p0 = pos0, poschr	// we found chr and null in the word
Packit 6c4009
(p8)	br.cond.spnt .notfound		// null was found before chr
Packit 6c4009
	add	ret0 = ret0, poschr ;;
Packit 6c4009
	adds	ret0 = -15, ret0 ;;	// should be -16, but we decrement
Packit 6c4009
.restore_and_exit:			// ret0 in the next instruction
Packit 6c4009
	adds	ret0 = -1, ret0		// ret0 was pointing 1 char too far
Packit 6c4009
	mov 	ar.lc = saved_lc	// restore the loop counter
Packit 6c4009
	br.ret.sptk.many b0
Packit 6c4009
.notfound:
Packit 6c4009
	mov	ret0 = r0		// return NULL if null was found
Packit 6c4009
	mov 	ar.lc = saved_lc
Packit 6c4009
	br.ret.sptk.many b0
Packit 6c4009
.recovery:
Packit 6c4009
	adds	ret0 = -8, ret0;;
Packit 6c4009
	ld8	val2 = [ret0], 8	// bomb out here
Packit 6c4009
	br.cond.sptk	.back
Packit 6c4009
END(strchr)
Packit 6c4009
Packit 6c4009
weak_alias (strchr, index)
Packit 6c4009
libc_hidden_builtin_def (strchr)