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