1 /* Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 /* Return the address of the last occurrance of a given character
21 within a null-terminated string, or null if it is not found.
23 This is generally scheduled for the EV5 (got to look out for my own
24 interests :-), but with EV4 needs in mind. There are, in fact, fewer
25 stalls on the EV4 than there are on the EV5.
36 zapnot a1, 1, a1 # e0 : zero extend our test character
37 mov zero, t6 # .. e1 : t6 is last match aligned addr
38 sll a1, 8, t5 # e0 : replicate our test character
39 mov zero, t7 # .. e1 : t7 is last match byte compare mask
41 ldq_u t0, 0(a0) # .. e1 : load first quadword
43 andnot a0, 7, v0 # .. e1 : align source addr
45 lda t4, -1 # .. e1 : build garbage mask
47 cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
48 mskqh t4, a0, t4 # e0 :
49 or t5, a1, a1 # .. e1 : character replication complete
50 xor t0, a1, t2 # e0 : make bytes == c zero
51 cmpbge zero, t4, t4 # .. e1 : bits set iff byte is garbage
52 cmpbge zero, t2, t3 # e0 : bits set iff byte == c
53 andnot t1, t4, t1 # .. e1 : clear garbage from null test
54 andnot t3, t4, t3 # e0 : clear garbage from char test
55 bne t1, $eos # .. e1 : did we already hit the terminator?
57 /* Character search main loop */
59 ldq t0, 8(v0) # e0 : load next quadword
60 cmovne t3, v0, t6 # .. e1 : save previous comparisons match
61 cmovne t3, t3, t7 # e0 :
62 addq v0, 8, v0 # .. e1 :
64 cmpbge zero, t0, t1 # .. e1 : bits set iff byte == zero
65 cmpbge zero, t2, t3 # e0 : bits set iff byte == c
66 beq t1, $loop # .. e1 : if we havnt seen a null, loop
68 /* Mask out character matches after terminator */
70 negq t1, t4 # e0 : isolate first null byte match
72 subq t4, 1, t5 # e0 : build a mask of the bytes upto...
73 or t4, t5, t4 # e1 : ... and including the null
75 and t3, t4, t3 # e0 : mask out char matches after null
76 cmovne t3, t3, t7 # .. e1 : save it, if match found
77 cmovne t3, v0, t6 # e0 :
79 /* Locate the address of the last matched character */
81 /* Retain the early exit for the ev4 -- the ev5 mispredict penalty
82 is 5 cycles -- the same as just falling through. */
83 beq t7, $retnull # .. e1 :
85 and t7, 0xf0, t2 # e0 : binary search for the high bit set
86 cmovne t2, t2, t7 # .. e1 (zdb)
87 cmovne t2, 4, t2 # e0 :
88 and t7, 0xcc, t1 # .. e1 :
89 cmovne t1, t1, t7 # e0 :
90 cmovne t1, 2, t1 # .. e1 :
91 and t7, 0xaa, t0 # e0 :
92 cmovne t0, 1, t0 # .. e1 (zdb)
93 addq t2, t1, t1 # e0 :
94 addq t6, t0, v0 # .. e1 : add our aligned base ptr to the mix
95 addq v0, t1, v0 # e0 :
104 weak_alias (strrchr, rindex)