1/* Optimized strchr 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#ifndef STRCHR 22# define STRCHR strchr 23#endif 24 25/* int [r3] strchr (char *s [r3], int c [r4]) */ 26 .machine power7 27ENTRY_TOCLESS (STRCHR) 28 CALL_MCOUNT 2 29 dcbt 0,r3 30 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */ 31 cmpdi cr7,r4,0 32 ld r12,0(r8) /* Load doubleword from memory. */ 33 li r0,0 /* Doubleword with null chars to use 34 with cmpb. */ 35 36 rlwinm r6,r3,3,26,28 /* Calculate padding. */ 37 38 beq cr7,L(null_match) 39 40 /* Replicate byte to doubleword. */ 41 insrdi r4,r4,8,48 42 insrdi r4,r4,16,32 43 insrdi r4,r4,32,0 44 45 /* Now r4 has a doubleword of c bytes and r0 has 46 a doubleword of null bytes. */ 47 48 cmpb r10,r12,r4 /* Compare each byte against c byte. */ 49 cmpb r11,r12,r0 /* Compare each byte against null byte. */ 50 51 /* Move the doublewords left and right to discard the bits that are 52 not part of the string and bring them back as zeros. */ 53#ifdef __LITTLE_ENDIAN__ 54 srd r10,r10,r6 55 srd r11,r11,r6 56 sld r10,r10,r6 57 sld r11,r11,r6 58#else 59 sld r10,r10,r6 60 sld r11,r11,r6 61 srd r10,r10,r6 62 srd r11,r11,r6 63#endif 64 or r5,r10,r11 /* OR the results to speed things up. */ 65 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes 66 have been found. */ 67 bne cr7,L(done) 68 69 mtcrf 0x01,r8 70 71 /* Are we now aligned to a doubleword boundary? If so, skip to 72 the main loop. Otherwise, go through the alignment code. */ 73 74 bt 28,L(loop) 75 76 /* Handle WORD2 of pair. */ 77 ldu r12,8(r8) 78 cmpb r10,r12,r4 79 cmpb r11,r12,r0 80 or r5,r10,r11 81 cmpdi cr7,r5,0 82 bne cr7,L(done) 83 b L(loop) /* We branch here (rather than falling through) 84 to skip the nops due to heavy alignment 85 of the loop below. */ 86 87 .p2align 5 88L(loop): 89 /* Load two doublewords, compare and merge in a 90 single register for speed. This is an attempt 91 to speed up the null-checking process for bigger strings. */ 92 ld r12,8(r8) 93 ldu r9,16(r8) 94 cmpb r10,r12,r4 95 cmpb r11,r12,r0 96 cmpb r6,r9,r4 97 cmpb r7,r9,r0 98 or r12,r10,r11 99 or r9,r6,r7 100 or r5,r12,r9 101 cmpdi cr7,r5,0 102 beq cr7,L(loop) 103 104 /* OK, one (or both) of the doublewords contains a c/null byte. Check 105 the first doubleword and decrement the address in case the first 106 doubleword really contains a c/null byte. */ 107 108 cmpdi cr6,r12,0 109 addi r8,r8,-8 110 bne cr6,L(done) 111 112 /* The c/null byte must be in the second doubleword. Adjust the 113 address again and move the result of cmpb to r10 so we can calculate 114 the pointer. */ 115 116 mr r10,r6 117 mr r11,r7 118 addi r8,r8,8 119 120 /* r10/r11 have the output of the cmpb instructions, that is, 121 0xff in the same position as the c/null byte in the original 122 doubleword from the string. Use that to calculate the pointer. */ 123L(done): 124#ifdef __LITTLE_ENDIAN__ 125 addi r3,r10,-1 126 andc r3,r3,r10 127 popcntd r0,r3 128 addi r4,r11,-1 129 andc r4,r4,r11 130 cmpld cr7,r3,r4 131 bgt cr7,L(no_match) 132#else 133 cntlzd r0,r10 /* Count leading zeros before c matches. */ 134 cmpld cr7,r11,r10 135 bgt cr7,L(no_match) 136#endif 137 srdi r0,r0,3 /* Convert leading zeros to bytes. */ 138 add r3,r8,r0 /* Return address of the matching c byte 139 or null in case c was not found. */ 140 blr 141 142 .align 4 143L(no_match): 144 li r3,0 145 blr 146 147/* We are here because strchr was called with a null byte. */ 148 .align 4 149L(null_match): 150 /* r0 has a doubleword of null bytes. */ 151 152 cmpb r5,r12,r0 /* Compare each byte against null bytes. */ 153 154 /* Move the doublewords left and right to discard the bits that are 155 not part of the string and bring them back as zeros. */ 156#ifdef __LITTLE_ENDIAN__ 157 srd r5,r5,r6 158 sld r5,r5,r6 159#else 160 sld r5,r5,r6 161 srd r5,r5,r6 162#endif 163 cmpdi cr7,r5,0 /* If r10 == 0, no c or null bytes 164 have been found. */ 165 bne cr7,L(done_null) 166 167 mtcrf 0x01,r8 168 169 /* Are we now aligned to a quadword boundary? If so, skip to 170 the main loop. Otherwise, go through the alignment code. */ 171 172 bt 28,L(loop_null) 173 174 /* Handle WORD2 of pair. */ 175 ldu r12,8(r8) 176 cmpb r5,r12,r0 177 cmpdi cr7,r5,0 178 bne cr7,L(done_null) 179 b L(loop_null) /* We branch here (rather than falling through) 180 to skip the nops due to heavy alignment 181 of the loop below. */ 182 183 /* Main loop to look for the end of the string. Since it's a 184 small loop (< 8 instructions), align it to 32-bytes. */ 185 .p2align 5 186L(loop_null): 187 /* Load two doublewords, compare and merge in a 188 single register for speed. This is an attempt 189 to speed up the null-checking process for bigger strings. */ 190 ld r12,8(r8) 191 ldu r11,16(r8) 192 cmpb r5,r12,r0 193 cmpb r10,r11,r0 194 or r6,r5,r10 195 cmpdi cr7,r6,0 196 beq cr7,L(loop_null) 197 198 /* OK, one (or both) of the doublewords contains a null byte. Check 199 the first doubleword and decrement the address in case the first 200 doubleword really contains a null byte. */ 201 202 cmpdi cr6,r5,0 203 addi r8,r8,-8 204 bne cr6,L(done_null) 205 206 /* The null byte must be in the second doubleword. Adjust the address 207 again and move the result of cmpb to r10 so we can calculate the 208 pointer. */ 209 210 mr r5,r10 211 addi r8,r8,8 212 213 /* r5 has the output of the cmpb instruction, that is, it contains 214 0xff in the same position as the null byte in the original 215 doubleword from the string. Use that to calculate the pointer. */ 216L(done_null): 217#ifdef __LITTLE_ENDIAN__ 218 addi r0,r5,-1 219 andc r0,r0,r5 220 popcntd r0,r0 221#else 222 cntlzd r0,r5 /* Count leading zeros before the match. */ 223#endif 224 srdi r0,r0,3 /* Convert leading zeros to bytes. */ 225 add r3,r8,r0 /* Return address of the matching null byte. */ 226 blr 227END (STRCHR) 228weak_alias (strchr, index) 229libc_hidden_builtin_def (strchr) 230