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