1/* Optimized rawmemchr implementation for PowerPC32/POWER7 using cmpb insn. 2 Copyright (C) 2010-2022 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, see 17 <https://www.gnu.org/licenses/>. */ 18 19#include <sysdep.h> 20 21/* int [r3] rawmemchr (void *s [r3], int c [r4]) */ 22 .machine power7 23ENTRY (__rawmemchr) 24 CALL_MCOUNT 25 dcbt 0,r3 26 clrrwi r8,r3,2 /* Align the address to word boundary. */ 27 28 /* Replicate byte to word. */ 29 insrwi r4,r4,8,16 30 insrwi r4,r4,16,0 31 32 /* Now r4 has a word of c bytes. */ 33 34 rlwinm r6,r3,3,27,28 /* Calculate padding. */ 35 lwz r12,0(r8) /* Load word from memory. */ 36 cmpb r5,r12,r4 /* Compare each byte against c byte. */ 37#ifdef __LITTLE_ENDIAN__ 38 srw r5,r5,r6 39 slw r5,r5,r6 40#else 41 slw r5,r5,r6 /* Move left to discard ignored bits. */ 42 srw r5,r5,r6 /* Bring the bits back as zeros. */ 43#endif 44 cmpwi cr7,r5,0 /* If r5 == 0, no c bytes have been found. */ 45 bne cr7,L(done) 46 47 mtcrf 0x01,r8 48 49 /* Are we now aligned to a doubleword boundary? If so, skip to 50 the main loop. Otherwise, go through the alignment code. */ 51 52 bt 29,L(loop) 53 54 /* Handle WORD2 of pair. */ 55 lwzu r12,4(r8) 56 cmpb r5,r12,r4 57 cmpwi cr7,r5,0 58 bne cr7,L(done) 59 b L(loop) /* We branch here (rather than falling through) 60 to skip the nops due to heavy alignment 61 of the loop below. */ 62 63 /* Main loop to look for the end of the string. Since it's a 64 small loop (< 8 instructions), align it to 32-bytes. */ 65 .p2align 5 66L(loop): 67 /* Load two words, compare and merge in a 68 single register for speed. This is an attempt 69 to speed up the byte-checking process for bigger strings. */ 70 lwz r12,4(r8) 71 lwzu r11,8(r8) 72 cmpb r5,r12,r4 73 cmpb r6,r11,r4 74 or r7,r5,r6 75 cmpwi cr7,r7,0 76 beq cr7,L(loop) 77 78 /* OK, one (or both) of the words contains a 'c' byte. Check 79 the first word and decrement the address in case the first 80 word really contains a c byte. */ 81 82 cmpwi cr6,r5,0 83 addi r8,r8,-4 84 bne cr6,L(done) 85 86 /* The 'c' byte must be in the second word. Adjust the address 87 again and move the result of cmpb to r10 so we can calculate the 88 pointer. */ 89 mr r5,r6 90 addi r8,r8,4 91 92 /* r5 has the output of the cmpb instruction, that is, it contains 93 0xff in the same position as the 'c' byte in the original 94 word from the string. Use that fact to find out what is 95 the position of the byte inside the string. */ 96L(done): 97#ifdef __LITTLE_ENDIAN__ 98 addi r0,r5,-1 99 andc r0,r0,r5 100 popcntw r0,r0 101#else 102 cntlzw r0,r5 /* Count leading zeros before the match. */ 103#endif 104 srwi r0,r0,3 /* Convert leading zeros to bytes. */ 105 add r3,r8,r0 /* Return address of the matching char. */ 106 blr 107END (__rawmemchr) 108weak_alias (__rawmemchr,rawmemchr) 109libc_hidden_builtin_def (__rawmemchr) 110