1/* Optimized strchrnul 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] strchrnul (char *s [r3], int c [r4]) */ 22 .machine power7 23ENTRY (__strchrnul) 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 rlwinm r6,r3,3,27,28 /* Calculate padding. */ 33 lwz r12,0(r8) /* Load word from memory. */ 34 li r0,0 /* Word with null chars to use 35 with cmpb. */ 36 37 /* Now r4 has a word of c bytes and r0 has 38 a word of null bytes. */ 39 40 cmpb r10,r12,r0 /* Compare each byte against c byte. */ 41 cmpb r9,r12,r4 /* Compare each byte against null byte. */ 42 43 /* Move the words left and right to discard the bits that are 44 not part of the string and bring them back as zeros. */ 45#ifdef __LITTLE_ENDIAN__ 46 srw r10,r10,r6 47 srw r9,r9,r6 48 slw r10,r10,r6 49 slw r9,r9,r6 50#else 51 slw r10,r10,r6 52 slw r9,r9,r6 53 srw r10,r10,r6 54 srw r9,r9,r6 55#endif 56 or r5,r9,r10 /* OR the results to speed things up. */ 57 cmpwi cr7,r5,0 /* If r5 == 0, no c or null bytes 58 have been found. */ 59 bne cr7,L(done) 60 61 mtcrf 0x01,r8 62 63 /* Are we now aligned to a doubleword boundary? If so, skip to 64 the main loop. Otherwise, go through the alignment code. */ 65 66 bt 29,L(loop) 67 68 /* Handle WORD2 of pair. */ 69 lwzu r12,4(r8) 70 cmpb r10,r12,r0 71 cmpb r9,r12,r4 72 or r5,r9,r10 73 cmpwi cr7,r5,0 74 bne cr7,L(done) 75 b L(loop) /* We branch here (rather than falling through) 76 to skip the nops due to heavy alignment 77 of the loop below. */ 78 79 .p2align 5 80L(loop): 81 /* Load two words, compare and merge in a 82 single register for speed. This is an attempt 83 to speed up the null-checking process for bigger strings. */ 84 lwz r12,4(r8) 85 lwzu r11,8(r8) 86 cmpb r10,r12,r0 87 cmpb r9,r12,r4 88 cmpb r6,r11,r0 89 cmpb r7,r11,r4 90 or r5,r9,r10 91 or r10,r6,r7 92 or r11,r5,r10 93 cmpwi cr7,r11,0 94 beq cr7,L(loop) 95 96 /* OK, one (or both) of the words contains a c/null byte. Check 97 the first word and decrement the address in case the first 98 word really contains a c/null byte. */ 99 100 cmpwi cr6,r5,0 101 addi r8,r8,-4 102 bne cr6,L(done) 103 104 /* The c/null byte must be in the second word. Adjust the address 105 again and move the result of cmpb to r5 so we can calculate the 106 pointer. */ 107 mr r5,r10 108 addi r8,r8,4 109 110 /* r5 has the output of the cmpb instruction, that is, it contains 111 0xff in the same position as the c/null byte in the original 112 word from the string. Use that to calculate the pointer. */ 113L(done): 114#ifdef __LITTLE_ENDIAN__ 115 addi r0,r5,-1 116 andc r0,r0,r5 117 popcntw r0,r0 118#else 119 cntlzw r0,r5 /* Count leading zeros before the match. */ 120#endif 121 srwi r0,r0,3 /* Convert leading zeros to bytes. */ 122 add r3,r8,r0 /* Return address of matching c/null byte. */ 123 blr 124END (__strchrnul) 125weak_alias (__strchrnul,strchrnul) 126libc_hidden_builtin_def (__strchrnul) 127