1/* 2 Copyright (c) 2015, Synopsys, Inc. All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are met: 6 7 1) Redistributions of source code must retain the above copyright notice, 8 this list of conditions and the following disclaimer. 9 10 2) Redistributions in binary form must reproduce the above copyright notice, 11 this list of conditions and the following disclaimer in the documentation 12 and/or other materials provided with the distribution. 13 14 3) Neither the name of the Synopsys, Inc., nor the names of its contributors 15 may be used to endorse or promote products derived from this software 16 without specific prior written permission. 17 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 POSSIBILITY OF SUCH DAMAGE. 29*/ 30 31/* This implementation is optimized for performance. For code size a generic 32 implementation of this function from newlib/libc/string/strchr.c will be 33 used. */ 34#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED) 35 36#include "asm.h" 37 38/* In order to search for a zero in a W, we calculate 39 X := (W - 0x01010101) & ~W & 0x80808080; 40 In the little endian case: 41 If no byte in W is zero, X will be zero; otherwise, the least significant 42 byte of X which is nonzero indicates the least significant byte of W that 43 is zero. 44 In the big endian case: 45 X will be zero iff no byte in W is zero. 46 If X is nonzero, to find out which is the most significant zero byte 47 in W, we calculate: 48 Y := ~(((W | 0x80808080) - 0x01010101) | W) & 0x80808080; 49 Each byte in Y is 0x80 if the the corresponding byte in 50 W is zero, otherwise that byte of Y is 0. */ 51 52#if defined (__ARC601__) || !defined (__ARC_BARREL_SHIFTER__) 53ENTRY (strchr) 54 bmsk.f r2,r0,1 55 mov_s r3,0x01010101 56 extb_s r1,r1 57 mov r8,0 58 add3 r5,r8,r1 59 add3 r5,r8,r5 60 add2 r5,r1,r5 61 add3 r4,r8,r5 62 add3 r4,r8,r4 63 add3 r4,r8,r4 64 add3 r4,r8,r4 65 beq.d .Laligned 66 add3 r4,r8,r4 67 sub_s r0,r0,r2 68#ifdef __LITTLE_ENDIAN__ 69 add3.f r2,-1,r2 70 bmsk r7,r3,r2 71 rsub.pl r7,r7,r3 72#else 73 mov_s r12,31 74 sub3 r2,r12,r2 75 bmsk r7,r3,r2 76#endif 77 ld_s r2,[r0] 78 add1 r5,r5,r4 79 ror r4,r3 80 sub r12,r2,r7 81 bic_s r12,r12,r2 82 and r12,r12,r4 83 84 brne.d r12,0,.Lfound0_ua 85 xor r6,r2,r5 86 ld.a r2,[r0,4] 87 sub r12,r6,r7 88 bic r12,r12,r6 89#ifdef __LITTLE_ENDIAN__ 90 and.f r7,r12,r4 91 sub r12,r2,r3 92 bic_s r12,r12,r2 93 beq.d .Loop 94 and r12,r12,r4 95 b.d .Lfound_char_ua 96 btst r7,7 97#else 98 and.f r8,r12,r4 99 sub r12,r2,r3 100 bic_s r12,r12,r2 101 beq.d .Loop 102 and r12,r12,r4 103 bic r12,r7,r6 104 bic r2,r3,r12 105 sub1 r2,r3,r2 106 sub_s r0,r0,4 107 b.d .Lfound_char_ua 108 bic.f r2,r8,r2 109#endif 110 111 .balign 4 112.Laligned: 113 ld_s r2,[r0] 114 add1 r5,r5,r4 115 ror r4,r3 116 sub r12,r2,r3 117 bic_s r12,r12,r2 118 and r12,r12,r4 119.Loop: 120 121 brne.d r12,0,.Lfound0 122 xor r6,r2,r5 123 ld.a r2,[r0,4] 124 sub r12,r6,r3 125 bic r12,r12,r6 126 and.f r7,r12,r4 127 sub r12,r2,r3 128 bic_s r12,r12,r2 129 beq.d .Loop 130 and r12,r12,r4 131; Found searched-for character. r0 has already advanced to next word. 132#ifdef __LITTLE_ENDIAN__ 133/* We only need the information about the first matching byte 134 (i.e. the least significant matching byte) to be exact, 135 hence there is no problem with carry effects. */ 136.Lfound_char: 137 btst r7,7 138.Lfound_char_ua: 139 sub_s r0,r0,4 140 add.eq r0,r0,1 141 btst.eq r7,15 142 add.eq r0,r0,1 143 btst.eq r7,23 144 j_s.d [blink] 145 add.eq r0,r0,1 146 147 .balign 4 148.Lfound0_ua: 149 mov_l r3,r7 150.Lfound0: 151 sub r2,r6,r3 152 bic r2,r2,r6 153 and r2,r2,r4 154 or r3,r12,r2 155 sub_s r12,r3,1 156 xor_s r3,r3,r12 157 cmp 0xffff,r3 158 ; cmp uses limm ; ARC600 would do: asl.f 0,r3,9 159 tst_s r2,r3 160 mov.eq r0,0 161 add.mi r0,r0,1 162 btst.ne r3,15 163 j_s.d [blink] 164 adc.ne r0,r0,1 165#else /* BIG ENDIAN */ 166.Lfound_char: 167 and r2,r6,r3 168 sub1 r2,r3,r2 169 sub_s r0,r0,4 170 bic.f r2,r7,r2 171.Lfound_char_ua: 172 add.pl r0,r0,1 173 jmi.d [blink] 174 btst_s r2,23 175 add.eq r0,r0,1 176 btst.eq r2,15 177 j_s.d [blink] 178 add.eq r0,r0,1 179 180; N.B. if we searched for a char zero and found it in the MSB, 181; and ignored matches are identical, we will take the early exit 182; like for an ordinary found zero - except for the extra stalls at jhi - 183; but still compute the right result. 184.Lfound0_ua: 185 mov_s r3,r7 186.Lfound0: 187 and_s r2,r2,r3 188 sub1 r2,r3,r2 189 or r7,r6,r4 190 bic_s r12,r12,r2 191 sub r2,r7,r3 192 or r2,r2,r6 193 bic r2,r4,r2 194 cmp_s r12,r2 195 mov.hi r0,0 196 btst.ls r2,31 197 jhi.d [blink] 198 add.eq r0,r0,1 199 btst.eq r2,23 200 add.eq r0,r0,1 201 btst.eq r2,15 202 j_s.d [blink] 203 add.eq r0,r0,1 204#endif /* ENDIAN */ 205ENDFUNC (strchr) 206#endif /* __ARC601__ || !__ARC_BARREL_SHIFTER__ */ 207 208#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */ 209