1 /* Optimized rawmemchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 /* int [r3] rawmemchr (void *s [r3], int c [r4]) */
27 clrrwi r8,r3,2 /* Align the address to word boundary. */
29 /* Replicate byte to word. */
33 /* Now r4 has a word of c bytes. */
35 rlwinm r6,r3,3,27,28 /* Calculate padding. */
36 lwz r12,0(r8) /* Load word from memory. */
37 cmpb r5,r12,r4 /* Compare each byte against c byte. */
38 #ifdef __LITTLE_ENDIAN__
42 slw r5,r5,r6 /* Move left to discard ignored bits. */
43 srw r5,r5,r6 /* Bring the bits back as zeros. */
45 cmpwi cr7,r5,0 /* If r5 == 0, no c bytes have been found. */
50 /* Are we now aligned to a doubleword boundary? If so, skip to
51 the main loop. Otherwise, go through the alignment code. */
55 /* Handle WORD2 of pair. */
60 b L(loop) /* We branch here (rather than falling through)
61 to skip the nops due to heavy alignment
64 /* Main loop to look for the end of the string. Since it's a
65 small loop (< 8 instructions), align it to 32-bytes. */
68 /* Load two words, compare and merge in a
69 single register for speed. This is an attempt
70 to speed up the byte-checking process for bigger strings. */
79 /* OK, one (or both) of the words contains a 'c' byte. Check
80 the first word and decrement the address in case the first
81 word really contains a c byte. */
87 /* The 'c' byte must be in the second word. Adjust the address
88 again and move the result of cmpb to r10 so we can calculate the
93 /* r5 has the output of the cmpb instruction, that is, it contains
94 0xff in the same position as the 'c' byte in the original
95 word from the string. Use that fact to find out what is
96 the position of the byte inside the string. */
98 #ifdef __LITTLE_ENDIAN__
103 cntlzw r0,r5 /* Count leading zeros before the match. */
105 srwi r0,r0,3 /* Convert leading zeros to bytes. */
106 add r3,r8,r0 /* Return address of the matching char. */
109 weak_alias (__rawmemchr,rawmemchr)
110 libc_hidden_builtin_def (__rawmemchr)