1/* Optimized rawmemchr implementation for PowerPC64/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 23#ifndef RAWMEMCHR 24# define RAWMEMCHR __rawmemchr 25#endif 26 .machine power7 27ENTRY_TOCLESS (RAWMEMCHR) 28 CALL_MCOUNT 2 29 dcbt 0,r3 30 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */ 31 32 /* Replicate byte to doubleword. */ 33 insrdi r4,r4,8,48 34 insrdi r4,r4,16,32 35 insrdi r4,r4,32,0 36 37 /* Now r4 has a doubleword of c bytes. */ 38 39 rlwinm r6,r3,3,26,28 /* Calculate padding. */ 40 ld r12,0(r8) /* Load doubleword from memory. */ 41 cmpb r5,r12,r4 /* Compare each byte against c byte. */ 42#ifdef __LITTLE_ENDIAN__ 43 srd r5,r5,r6 44 sld r5,r5,r6 45#else 46 sld r5,r5,r6 /* Move left to discard ignored bits. */ 47 srd r5,r5,r6 /* Bring the bits back as zeros. */ 48#endif 49 cmpdi cr7,r5,0 /* If r5 == 0, no c bytes have been found. */ 50 bne cr7,L(done) 51 52 mtcrf 0x01,r8 53 54 /* Are we now aligned to a quadword boundary? If so, skip to 55 the main loop. Otherwise, go through the alignment code. */ 56 57 bt 28,L(loop) 58 59 /* Handle DWORD2 of pair. */ 60 ldu r12,8(r8) 61 cmpb r5,r12,r4 62 cmpdi cr7,r5,0 63 bne cr7,L(done) 64 b L(loop) /* We branch here (rather than falling through) 65 to skip the nops due to heavy alignment 66 of the loop below. */ 67 68 /* Main loop to look for the end of the string. Since it's a 69 small loop (< 8 instructions), align it to 32-bytes. */ 70 .p2align 5 71L(loop): 72 /* Load two doublewords, compare and merge in a 73 single register for speed. This is an attempt 74 to speed up the byte-checking process for bigger strings. */ 75 ld r12,8(r8) 76 ldu r11,16(r8) 77 cmpb r5,r12,r4 78 cmpb r6,r11,r4 79 or r7,r5,r6 80 cmpdi cr7,r7,0 81 beq cr7,L(loop) 82 83 /* OK, one (or both) of the doublewords contains a 'c' byte. Check 84 the first doubleword and decrement the address in case the first 85 doubleword really contains a c byte. */ 86 87 cmpdi cr6,r5,0 88 addi r8,r8,-8 89 bne cr6,L(done) 90 91 /* The 'c' byte must be in the second doubleword. Adjust the address 92 again and move the result of cmpb to r10 so we can calculate the 93 pointer. */ 94 mr r5,r6 95 addi r8,r8,8 96 97 /* r5 has the output of the cmpb instruction, that is, it contains 98 0xff in the same position as the 'c' byte in the original 99 doubleword from the string. Use that fact to find out what is 100 the position of the byte inside the string. */ 101L(done): 102#ifdef __LITTLE_ENDIAN__ 103 addi r0,r5,-1 104 andc r0,r0,r5 105 popcntd r0,r0 /* Count trailing zeros. */ 106#else 107 cntlzd r0,r5 /* Count leading zeros before the match. */ 108#endif 109 srdi r0,r0,3 /* Convert leading zeros to bytes. */ 110 add r3,r8,r0 /* Return address of the matching char. */ 111 blr 112END (RAWMEMCHR) 113weak_alias (__rawmemchr,rawmemchr) 114libc_hidden_builtin_def (__rawmemchr) 115