]>
Commit | Line | Data |
---|---|---|
5bfc4252 UD |
1 | /* Optimized version of the standard strchr() function. |
2 | This file is part of the GNU C Library. | |
85dd1003 | 3 | Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc. |
5bfc4252 UD |
4 | Contributed by Dan Pop <Dan.Pop@cern.ch>. |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
5bfc4252 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
5bfc4252 | 15 | |
41bdb6e2 AJ |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
5bfc4252 UD |
20 | |
21 | /* Return: the address of the first occurence of chr in str or NULL | |
22 | ||
23 | Inputs: | |
24 | in0: str | |
25 | in1: chr | |
26 | ||
27 | A modified version of memchr.S, the search ends when the character is | |
28 | found or the terminating null character is encountered. | |
29 | ||
30 | This implementation assumes little endian mode. For big endian mode, | |
31 | the instruction czx1.r should be replaced by czx1.l. */ | |
32 | ||
33 | #include <sysdep.h> | |
34 | #undef ret | |
35 | ||
5bfc4252 UD |
36 | #define saved_lc r18 |
37 | #define poschr r19 | |
38 | #define pos0 r20 | |
39 | #define val1 r21 | |
40 | #define val2 r22 | |
41 | #define tmp r24 | |
42 | #define chrx8 r25 | |
43 | #define loopcnt r30 | |
44 | ||
45 | #define str in0 | |
46 | #define chr in1 | |
47 | ||
48 | ENTRY(strchr) | |
8ee35515 UD |
49 | .prologue |
50 | alloc r2 = ar.pfs, 2, 0, 0, 0 | |
51 | .save ar.lc, saved_lc | |
5bfc4252 | 52 | mov saved_lc = ar.lc // save the loop counter |
8ee35515 | 53 | .body |
5bfc4252 UD |
54 | mov ret0 = str |
55 | and tmp = 7, str // tmp = str % 8 | |
56 | mux1 chrx8 = chr, @brcst | |
57 | extr.u chr = chr, 0, 8 // retain only the last byte | |
58 | cmp.ne p8, p0 = r0, r0 // clear p8 | |
59 | ;; | |
60 | sub loopcnt = 8, tmp // loopcnt = 8 - tmp | |
61 | cmp.eq p6, p0 = tmp, r0 | |
62 | (p6) br.cond.sptk .str_aligned;; | |
63 | adds loopcnt = -1, loopcnt;; | |
64 | mov ar.lc = loopcnt | |
65 | .l1: | |
66 | ld1 val2 = [ret0], 1 | |
67 | ;; | |
68 | cmp.eq p6, p0 = val2, chr | |
69 | cmp.eq p7, p0 = val2, r0 | |
70 | (p6) br.cond.spnt .restore_and_exit | |
71 | (p7) br.cond.spnt .notfound | |
72 | br.cloop.sptk .l1 | |
73 | .str_aligned: | |
74 | ld8 val1 = [ret0], 8;; | |
75 | nop.b 0 | |
76 | nop.b 0 | |
77 | .l2: | |
78 | ld8.s val2 = [ret0], 8 // don't bomb out here | |
79 | czx1.r pos0 = val1 | |
80 | xor tmp = val1, chrx8 // if val1 contains chr, tmp will | |
81 | ;; // contain a zero in its position | |
82 | czx1.r poschr = tmp | |
83 | cmp.ne p6, p0 = 8, pos0 | |
84 | ;; | |
85 | cmp.ne p7, p0 = 8, poschr | |
86 | (p7) br.cond.spnt .foundit | |
87 | (p6) br.cond.spnt .notfound | |
88 | chk.s val2, .recovery | |
89 | .back: | |
90 | mov val1 = val2 | |
91 | br.cond.dptk .l2 | |
92 | .foundit: | |
93 | (p6) cmp.lt p8, p0 = pos0, poschr // we found chr and null in the word | |
94 | (p8) br.cond.spnt .notfound // null was found before chr | |
95 | add ret0 = ret0, poschr ;; | |
96 | adds ret0 = -15, ret0 ;; // should be -16, but we decrement | |
97 | .restore_and_exit: // ret0 in the next instruction | |
98 | adds ret0 = -1, ret0 // ret0 was pointing 1 char too far | |
5bfc4252 UD |
99 | mov ar.lc = saved_lc // restore the loop counter |
100 | br.ret.sptk.many b0 | |
101 | .notfound: | |
102 | mov ret0 = r0 // return NULL if null was found | |
5bfc4252 UD |
103 | mov ar.lc = saved_lc |
104 | br.ret.sptk.many b0 | |
105 | .recovery: | |
106 | adds ret0 = -8, ret0;; | |
107 | ld8 val2 = [ret0], 8 // bomb out here | |
108 | br.cond.sptk .back | |
109 | END(strchr) | |
110 | ||
111 | weak_alias(strchr, index) | |
85dd1003 | 112 | libc_hidden_builtin_def (strchr) |