1bbbf1280Sopenharmony_ci/* 2bbbf1280Sopenharmony_ci * memchr - scan memory for a character 3bbbf1280Sopenharmony_ci * 4bbbf1280Sopenharmony_ci * Copyright (c) 2010-2021, Arm Limited. 5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT 6bbbf1280Sopenharmony_ci */ 7bbbf1280Sopenharmony_ci 8bbbf1280Sopenharmony_ci/* 9bbbf1280Sopenharmony_ci Written by Dave Gilbert <david.gilbert@linaro.org> 10bbbf1280Sopenharmony_ci 11bbbf1280Sopenharmony_ci This __memchr_arm routine is optimised on a Cortex-A9 and should work on 12bbbf1280Sopenharmony_ci all ARMv7 processors. It has a fast past for short sizes, and has 13bbbf1280Sopenharmony_ci an optimised path for large data sets; the worst case is finding the 14bbbf1280Sopenharmony_ci match early in a large data set. 15bbbf1280Sopenharmony_ci 16bbbf1280Sopenharmony_ci */ 17bbbf1280Sopenharmony_ci 18bbbf1280Sopenharmony_ci@ 2011-02-07 david.gilbert@linaro.org 19bbbf1280Sopenharmony_ci@ Extracted from local git a5b438d861 20bbbf1280Sopenharmony_ci@ 2011-07-14 david.gilbert@linaro.org 21bbbf1280Sopenharmony_ci@ Import endianness fix from local git ea786f1b 22bbbf1280Sopenharmony_ci@ 2011-12-07 david.gilbert@linaro.org 23bbbf1280Sopenharmony_ci@ Removed unneeded cbz from align loop 24bbbf1280Sopenharmony_ci 25bbbf1280Sopenharmony_ci .syntax unified 26bbbf1280Sopenharmony_ci .arch armv7-a 27bbbf1280Sopenharmony_ci 28bbbf1280Sopenharmony_ci@ this lets us check a flag in a 00/ff byte easily in either endianness 29bbbf1280Sopenharmony_ci#ifdef __ARMEB__ 30bbbf1280Sopenharmony_ci#define CHARTSTMASK(c) 1<<(31-(c*8)) 31bbbf1280Sopenharmony_ci#else 32bbbf1280Sopenharmony_ci#define CHARTSTMASK(c) 1<<(c*8) 33bbbf1280Sopenharmony_ci#endif 34bbbf1280Sopenharmony_ci .thumb 35bbbf1280Sopenharmony_ci 36bbbf1280Sopenharmony_ci@ --------------------------------------------------------------------------- 37bbbf1280Sopenharmony_ci .thumb_func 38bbbf1280Sopenharmony_ci .align 2 39bbbf1280Sopenharmony_ci .p2align 4,,15 40bbbf1280Sopenharmony_ci .global __memchr_arm 41bbbf1280Sopenharmony_ci .type __memchr_arm,%function 42bbbf1280Sopenharmony_ci__memchr_arm: 43bbbf1280Sopenharmony_ci @ r0 = start of memory to scan 44bbbf1280Sopenharmony_ci @ r1 = character to look for 45bbbf1280Sopenharmony_ci @ r2 = length 46bbbf1280Sopenharmony_ci @ returns r0 = pointer to character or NULL if not found 47bbbf1280Sopenharmony_ci and r1,r1,#0xff @ Don't think we can trust the caller to actually pass a char 48bbbf1280Sopenharmony_ci 49bbbf1280Sopenharmony_ci cmp r2,#16 @ If it's short don't bother with anything clever 50bbbf1280Sopenharmony_ci blt 20f 51bbbf1280Sopenharmony_ci 52bbbf1280Sopenharmony_ci tst r0, #7 @ If it's already aligned skip the next bit 53bbbf1280Sopenharmony_ci beq 10f 54bbbf1280Sopenharmony_ci 55bbbf1280Sopenharmony_ci @ Work up to an aligned point 56bbbf1280Sopenharmony_ci5: 57bbbf1280Sopenharmony_ci ldrb r3, [r0],#1 58bbbf1280Sopenharmony_ci subs r2, r2, #1 59bbbf1280Sopenharmony_ci cmp r3, r1 60bbbf1280Sopenharmony_ci beq 50f @ If it matches exit found 61bbbf1280Sopenharmony_ci tst r0, #7 62bbbf1280Sopenharmony_ci bne 5b @ If not aligned yet then do next byte 63bbbf1280Sopenharmony_ci 64bbbf1280Sopenharmony_ci10: 65bbbf1280Sopenharmony_ci @ At this point, we are aligned, we know we have at least 8 bytes to work with 66bbbf1280Sopenharmony_ci push {r4,r5,r6,r7} 67bbbf1280Sopenharmony_ci orr r1, r1, r1, lsl #8 @ expand the match word across to all bytes 68bbbf1280Sopenharmony_ci orr r1, r1, r1, lsl #16 69bbbf1280Sopenharmony_ci bic r4, r2, #7 @ Number of double words to work with 70bbbf1280Sopenharmony_ci mvns r7, #0 @ all F's 71bbbf1280Sopenharmony_ci movs r3, #0 72bbbf1280Sopenharmony_ci 73bbbf1280Sopenharmony_ci15: 74bbbf1280Sopenharmony_ci ldmia r0!,{r5,r6} 75bbbf1280Sopenharmony_ci subs r4, r4, #8 76bbbf1280Sopenharmony_ci eor r5,r5, r1 @ Get it so that r5,r6 have 00's where the bytes match the target 77bbbf1280Sopenharmony_ci eor r6,r6, r1 78bbbf1280Sopenharmony_ci uadd8 r5, r5, r7 @ Parallel add 0xff - sets the GE bits for anything that wasn't 0 79bbbf1280Sopenharmony_ci sel r5, r3, r7 @ bytes are 00 for none-00 bytes, or ff for 00 bytes - NOTE INVERSION 80bbbf1280Sopenharmony_ci uadd8 r6, r6, r7 @ Parallel add 0xff - sets the GE bits for anything that wasn't 0 81bbbf1280Sopenharmony_ci sel r6, r5, r7 @ chained....bytes are 00 for none-00 bytes, or ff for 00 bytes - NOTE INVERSION 82bbbf1280Sopenharmony_ci cbnz r6, 60f 83bbbf1280Sopenharmony_ci bne 15b @ (Flags from the subs above) If not run out of bytes then go around again 84bbbf1280Sopenharmony_ci 85bbbf1280Sopenharmony_ci pop {r4,r5,r6,r7} 86bbbf1280Sopenharmony_ci and r1,r1,#0xff @ Get r1 back to a single character from the expansion above 87bbbf1280Sopenharmony_ci and r2,r2,#7 @ Leave the count remaining as the number after the double words have been done 88bbbf1280Sopenharmony_ci 89bbbf1280Sopenharmony_ci20: 90bbbf1280Sopenharmony_ci cbz r2, 40f @ 0 length or hit the end already then not found 91bbbf1280Sopenharmony_ci 92bbbf1280Sopenharmony_ci21: @ Post aligned section, or just a short call 93bbbf1280Sopenharmony_ci ldrb r3,[r0],#1 94bbbf1280Sopenharmony_ci subs r2,r2,#1 95bbbf1280Sopenharmony_ci eor r3,r3,r1 @ r3 = 0 if match - doesn't break flags from sub 96bbbf1280Sopenharmony_ci cbz r3, 50f 97bbbf1280Sopenharmony_ci bne 21b @ on r2 flags 98bbbf1280Sopenharmony_ci 99bbbf1280Sopenharmony_ci40: 100bbbf1280Sopenharmony_ci movs r0,#0 @ not found 101bbbf1280Sopenharmony_ci bx lr 102bbbf1280Sopenharmony_ci 103bbbf1280Sopenharmony_ci50: 104bbbf1280Sopenharmony_ci subs r0,r0,#1 @ found 105bbbf1280Sopenharmony_ci bx lr 106bbbf1280Sopenharmony_ci 107bbbf1280Sopenharmony_ci60: @ We're here because the fast path found a hit - now we have to track down exactly which word it was 108bbbf1280Sopenharmony_ci @ r0 points to the start of the double word after the one that was tested 109bbbf1280Sopenharmony_ci @ r5 has the 00/ff pattern for the first word, r6 has the chained value 110bbbf1280Sopenharmony_ci cmp r5, #0 111bbbf1280Sopenharmony_ci itte eq 112bbbf1280Sopenharmony_ci moveq r5, r6 @ the end is in the 2nd word 113bbbf1280Sopenharmony_ci subeq r0,r0,#3 @ Points to 2nd byte of 2nd word 114bbbf1280Sopenharmony_ci subne r0,r0,#7 @ or 2nd byte of 1st word 115bbbf1280Sopenharmony_ci 116bbbf1280Sopenharmony_ci @ r0 currently points to the 3rd byte of the word containing the hit 117bbbf1280Sopenharmony_ci tst r5, # CHARTSTMASK(0) @ 1st character 118bbbf1280Sopenharmony_ci bne 61f 119bbbf1280Sopenharmony_ci adds r0,r0,#1 120bbbf1280Sopenharmony_ci tst r5, # CHARTSTMASK(1) @ 2nd character 121bbbf1280Sopenharmony_ci ittt eq 122bbbf1280Sopenharmony_ci addeq r0,r0,#1 123bbbf1280Sopenharmony_ci tsteq r5, # (3<<15) @ 2nd & 3rd character 124bbbf1280Sopenharmony_ci @ If not the 3rd must be the last one 125bbbf1280Sopenharmony_ci addeq r0,r0,#1 126bbbf1280Sopenharmony_ci 127bbbf1280Sopenharmony_ci61: 128bbbf1280Sopenharmony_ci pop {r4,r5,r6,r7} 129bbbf1280Sopenharmony_ci subs r0,r0,#1 130bbbf1280Sopenharmony_ci bx lr 131bbbf1280Sopenharmony_ci 132bbbf1280Sopenharmony_ci .size __memchr_arm, . - __memchr_arm 133