|
Packit |
6c4009 |
/* Copyright (C) 1996-2018 Free Software Foundation, Inc.
|
|
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 |
/* Return the address of the last occurrence of a given character
|
|
Packit |
6c4009 |
within a null-terminated string, or null if it is not found.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
This is generally scheduled for the EV5 (got to look out for my own
|
|
Packit |
6c4009 |
interests :-), but with EV4 needs in mind. There are, in fact, fewer
|
|
Packit |
6c4009 |
stalls on the EV4 than there are on the EV5.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
.set noreorder
|
|
Packit |
6c4009 |
.set noat
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY(strrchr)
|
|
Packit |
6c4009 |
#ifdef PROF
|
|
Packit |
6c4009 |
ldgp gp, 0(pv)
|
|
Packit |
6c4009 |
lda AT, _mcount
|
|
Packit |
6c4009 |
jsr AT, (AT), _mcount
|
|
Packit |
6c4009 |
.prologue 1
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
.prologue 0
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
and a1, 0xff, a1 # e0 : zero extend our test character
|
|
Packit |
6c4009 |
mov zero, t6 # .. e1 : t6 is last match aligned addr
|
|
Packit |
6c4009 |
sll a1, 8, t5 # e0 : replicate our test character
|
|
Packit |
6c4009 |
mov zero, t7 # .. e1 : t7 is last match byte compare mask
|
|
Packit |
6c4009 |
or t5, a1, a1 # e0 :
|
|
Packit |
6c4009 |
ldq_u t0, 0(a0) # .. e1 : load first quadword
|
|
Packit |
6c4009 |
sll a1, 16, t5 # e0 :
|
|
Packit |
6c4009 |
andnot a0, 7, v0 # .. e1 : align source addr
|
|
Packit |
6c4009 |
or t5, a1, a1 # e0 :
|
|
Packit |
6c4009 |
lda t4, -1 # .. e1 : build garbage mask
|
|
Packit |
6c4009 |
sll a1, 32, t5 # e0 :
|
|
Packit |
6c4009 |
cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
|
|
Packit |
6c4009 |
mskqh t4, a0, t4 # e0 :
|
|
Packit |
6c4009 |
or t5, a1, a1 # .. e1 : character replication complete
|
|
Packit |
6c4009 |
xor t0, a1, t2 # e0 : make bytes == c zero
|
|
Packit |
6c4009 |
cmpbge zero, t4, t4 # .. e1 : bits set iff byte is garbage
|
|
Packit |
6c4009 |
cmpbge zero, t2, t3 # e0 : bits set iff byte == c
|
|
Packit |
6c4009 |
andnot t1, t4, t1 # .. e1 : clear garbage from null test
|
|
Packit |
6c4009 |
andnot t3, t4, t3 # e0 : clear garbage from char test
|
|
Packit |
6c4009 |
bne t1, $eos # .. e1 : did we already hit the terminator?
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Character search main loop */
|
|
Packit |
6c4009 |
$loop:
|
|
Packit |
6c4009 |
ldq t0, 8(v0) # e0 : load next quadword
|
|
Packit |
6c4009 |
cmovne t3, v0, t6 # .. e1 : save previous comparisons match
|
|
Packit |
6c4009 |
cmovne t3, t3, t7 # e0 :
|
|
Packit |
6c4009 |
addq v0, 8, v0 # .. e1 :
|
|
Packit |
6c4009 |
xor t0, a1, t2 # e0 :
|
|
Packit |
6c4009 |
cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
|
|
Packit |
6c4009 |
cmpbge zero, t2, t3 # e0 : bits set iff byte == c
|
|
Packit |
6c4009 |
beq t1, $loop # .. e1 : if we havnt seen a null, loop
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Mask out character matches after terminator */
|
|
Packit |
6c4009 |
$eos:
|
|
Packit |
6c4009 |
negq t1, t4 # e0 : isolate first null byte match
|
|
Packit |
6c4009 |
and t1, t4, t4 # e1 :
|
|
Packit |
6c4009 |
subq t4, 1, t5 # e0 : build a mask of the bytes upto...
|
|
Packit |
6c4009 |
or t4, t5, t4 # e1 : ... and including the null
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
and t3, t4, t3 # e0 : mask out char matches after null
|
|
Packit |
6c4009 |
cmovne t3, t3, t7 # .. e1 : save it, if match found
|
|
Packit |
6c4009 |
cmovne t3, v0, t6 # e0 :
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Locate the address of the last matched character */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Retain the early exit for the ev4 -- the ev5 mispredict penalty
|
|
Packit |
6c4009 |
is 5 cycles -- the same as just falling through. */
|
|
Packit |
6c4009 |
beq t7, $retnull # .. e1 :
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
and t7, 0xf0, t2 # e0 : binary search for the high bit set
|
|
Packit |
6c4009 |
cmovne t2, t2, t7 # .. e1 (zdb)
|
|
Packit |
6c4009 |
cmovne t2, 4, t2 # e0 :
|
|
Packit |
6c4009 |
and t7, 0xcc, t1 # .. e1 :
|
|
Packit |
6c4009 |
cmovne t1, t1, t7 # e0 :
|
|
Packit |
6c4009 |
cmovne t1, 2, t1 # .. e1 :
|
|
Packit |
6c4009 |
and t7, 0xaa, t0 # e0 :
|
|
Packit |
6c4009 |
cmovne t0, 1, t0 # .. e1 (zdb)
|
|
Packit |
6c4009 |
addq t2, t1, t1 # e0 :
|
|
Packit |
6c4009 |
addq t6, t0, v0 # .. e1 : add our aligned base ptr to the mix
|
|
Packit |
6c4009 |
addq v0, t1, v0 # e0 :
|
|
Packit |
6c4009 |
ret # .. e1 :
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
$retnull:
|
|
Packit |
6c4009 |
mov zero, v0 # e0 :
|
|
Packit |
6c4009 |
ret # .. e1 :
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
END(strrchr)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
weak_alias (strrchr, rindex)
|
|
Packit |
6c4009 |
libc_hidden_builtin_def (strrchr)
|