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#include <picolibc.h> 35 36#if !defined (__OPTIMIZE_SIZE__) && !defined (PREFER_SIZE_OVER_SPEED) 37 38#include "asm.h" 39 40/* In order to search for a zero in a W, we calculate 41 X := (W - 0x01010101) & ~W & 0x80808080; 42 In the little endian case: 43 If no byte in W is zero, X will be zero; otherwise, the least significant 44 byte of X which is nonzero indicates the least significant byte of W that 45 is zero. 46 In the big endian case: 47 X will be zero iff no byte in W is zero. 48 If X is nonzero, to find out which is the most significant zero byte 49 in W, we calculate: 50 Y := ~(((W | 0x80808080) - 0x01010101) | W) & 0x80808080; 51 Each byte in Y is 0x80 if the the corresponding byte in 52 W is zero, otherwise that byte of Y is 0. */ 53 54#if defined (__ARC601__) || !defined (__ARC_BARREL_SHIFTER__) 55ENTRY (strchr) 56 bmsk.f r2,r0,1 57 mov_s r3,0x01010101 58 extb_s r1,r1 59 mov r8,0 60 add3 r5,r8,r1 61 add3 r5,r8,r5 62 add2 r5,r1,r5 63 add3 r4,r8,r5 64 add3 r4,r8,r4 65 add3 r4,r8,r4 66 add3 r4,r8,r4 67 beq.d .Laligned 68 add3 r4,r8,r4 69 sub_s r0,r0,r2 70#ifdef __LITTLE_ENDIAN__ 71 add3.f r2,-1,r2 72 bmsk r7,r3,r2 73 rsub.pl r7,r7,r3 74#else 75 mov_s r12,31 76 sub3 r2,r12,r2 77 bmsk r7,r3,r2 78#endif 79 ld_s r2,[r0] 80 add1 r5,r5,r4 81 ror r4,r3 82 sub r12,r2,r7 83 bic_s r12,r12,r2 84 and r12,r12,r4 85 86 brne.d r12,0,.Lfound0_ua 87 xor r6,r2,r5 88 ld.a r2,[r0,4] 89 sub r12,r6,r7 90 bic r12,r12,r6 91#ifdef __LITTLE_ENDIAN__ 92 and.f r7,r12,r4 93 sub r12,r2,r3 94 bic_s r12,r12,r2 95 beq.d .Loop 96 and r12,r12,r4 97 b.d .Lfound_char_ua 98 btst r7,7 99#else 100 and.f r8,r12,r4 101 sub r12,r2,r3 102 bic_s r12,r12,r2 103 beq.d .Loop 104 and r12,r12,r4 105 bic r12,r7,r6 106 bic r2,r3,r12 107 sub1 r2,r3,r2 108 sub_s r0,r0,4 109 b.d .Lfound_char_ua 110 bic.f r2,r8,r2 111#endif 112 113 .balign 4 114.Laligned: 115 ld_s r2,[r0] 116 add1 r5,r5,r4 117 ror r4,r3 118 sub r12,r2,r3 119 bic_s r12,r12,r2 120 and r12,r12,r4 121.Loop: 122 123 brne.d r12,0,.Lfound0 124 xor r6,r2,r5 125 ld.a r2,[r0,4] 126 sub r12,r6,r3 127 bic r12,r12,r6 128 and.f r7,r12,r4 129 sub r12,r2,r3 130 bic_s r12,r12,r2 131 beq.d .Loop 132 and r12,r12,r4 133; Found searched-for character. r0 has already advanced to next word. 134#ifdef __LITTLE_ENDIAN__ 135/* We only need the information about the first matching byte 136 (i.e. the least significant matching byte) to be exact, 137 hence there is no problem with carry effects. */ 138.Lfound_char: 139 btst r7,7 140.Lfound_char_ua: 141 sub_s r0,r0,4 142 add.eq r0,r0,1 143 btst.eq r7,15 144 add.eq r0,r0,1 145 btst.eq r7,23 146 j_s.d [blink] 147 add.eq r0,r0,1 148 149 .balign 4 150.Lfound0_ua: 151 mov_l r3,r7 152.Lfound0: 153 sub r2,r6,r3 154 bic r2,r2,r6 155 and r2,r2,r4 156 or r3,r12,r2 157 sub_s r12,r3,1 158 xor_s r3,r3,r12 159 cmp 0xffff,r3 160 ; cmp uses limm ; ARC600 would do: asl.f 0,r3,9 161 tst_s r2,r3 162 mov.eq r0,0 163 add.mi r0,r0,1 164 btst.ne r3,15 165 j_s.d [blink] 166 adc.ne r0,r0,1 167#else /* BIG ENDIAN */ 168.Lfound_char: 169 and r2,r6,r3 170 sub1 r2,r3,r2 171 sub_s r0,r0,4 172 bic.f r2,r7,r2 173.Lfound_char_ua: 174 add.pl r0,r0,1 175 jmi.d [blink] 176 btst_s r2,23 177 add.eq r0,r0,1 178 btst.eq r2,15 179 j_s.d [blink] 180 add.eq r0,r0,1 181 182; N.B. if we searched for a char zero and found it in the MSB, 183; and ignored matches are identical, we will take the early exit 184; like for an ordinary found zero - except for the extra stalls at jhi - 185; but still compute the right result. 186.Lfound0_ua: 187 mov_s r3,r7 188.Lfound0: 189 and_s r2,r2,r3 190 sub1 r2,r3,r2 191 or r7,r6,r4 192 bic_s r12,r12,r2 193 sub r2,r7,r3 194 or r2,r2,r6 195 bic r2,r4,r2 196 cmp_s r12,r2 197 mov.hi r0,0 198 btst.ls r2,31 199 jhi.d [blink] 200 add.eq r0,r0,1 201 btst.eq r2,23 202 add.eq r0,r0,1 203 btst.eq r2,15 204 j_s.d [blink] 205 add.eq r0,r0,1 206#endif /* ENDIAN */ 207ENDFUNC (strchr) 208#endif /* __ARC601__ || !__ARC_BARREL_SHIFTER__ */ 209 210#endif /* !__OPTIMIZE_SIZE__ && !PREFER_SIZE_OVER_SPEED */ 211