1 /* Optimized memchr implementation for PowerPC32/POWER7 using cmpb insn.
2 Copyright (C) 2010-2016 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] memchr (char *s [r3], int byte [r4], int size [r5]) */
28 insrwi r4,r4,8,16 /* Replicate byte to word. */
30 /* Calculate the last acceptable address and check for possible
31 addition overflow by using satured math:
42 rlwinm r6,r3,3,27,28 /* Calculate padding. */
44 #ifdef __LITTLE_ENDIAN__
51 lwz r12,0(r8) /* Load word from memory. */
52 cmpb r3,r12,r4 /* Check for BYTEs in WORD1. */
54 clrlwi r5,r7,30 /* Byte count - 1 in last word. */
55 clrrwi r7,r7,2 /* Address of last word. */
56 cmplwi cr7,r3,0 /* If r3 == 0, no BYTEs have been found. */
60 /* Are we now aligned to a doubleword boundary? If so, skip to
61 the main loop. Otherwise, go through the alignment code. */
64 /* Handle WORD2 of pair. */
71 /* The last word we want to read in the loop below is the one
72 containing the last byte of the string, ie. the word at
73 (s + size - 1) & ~3, or r7. The first word read is at
74 r8 + 4, we read 2 * cnt words, so the last word read will
75 be at r8 + 4 + 8 * cnt - 4. Solving for cnt gives
76 cnt = (r7 - r8) / 8 */
78 srwi r6,r6,3 /* Number of loop iterations. */
79 mtctr r6 /* Setup the counter. */
81 /* Main loop to look for BYTE in the string. Since
82 it's a small loop (8 instructions), align it to 32-bytes. */
85 /* Load two words, compare and merge in a
86 single register for speed. This is an attempt
87 to speed up the byte-checking process for bigger strings. */
92 or r6,r9,r3 /* Merge everything in one word. */
97 /* We may have one more dword to read. */
109 /* OK, one (or both) of the words contains BYTE. Check
110 the first word and decrement the address in case the first
111 word really contains BYTE. */
116 /* BYTE must be in the second word. Adjust the address
117 again and move the result of cmpb to r3 so we can calculate the
123 /* r3 has the output of the cmpb instruction, that is, it contains
124 0xff in the same position as BYTE in the original
125 word from the string. Use that to calculate the pointer.
126 We need to make sure BYTE is *before* the end of the range. */
128 #ifdef __LITTLE_ENDIAN__
131 popcntw r0,r0 /* Count trailing zeros. */
133 cntlzw r0,r3 /* Count leading zeros before the match. */
135 cmplw r8,r7 /* Are we on the last word? */
136 srwi r0,r0,3 /* Convert leading/trailing zeros to bytes. */
138 cmplw cr7,r0,r5 /* If on the last dword, check byte offset. */
149 /* Deals with size <= 16. */
154 lwz r12,0(r8) /* Load word from memory. */
155 cmpb r3,r12,r4 /* Check for BYTE in DWORD1. */
158 clrlwi r5,r7,30 /* Byte count - 1 in last word. */
159 clrrwi r7,r7,2 /* Address of last word. */
160 cmplw r8,r7 /* Are we done already? */
192 weak_alias (__memchr, memchr)
193 libc_hidden_builtin_def (memchr)