Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / powerpc / powerpc32 / power7 / rawmemchr.S
blob95252343e1332de48ae6f174dc6c7c0457bd69b1
1 /* Optimized rawmemchr implementation for PowerPC32/POWER7 using cmpb insn.
2    Copyright (C) 2010 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/>.  */
20 #include <sysdep.h>
21 #include <bp-sym.h>
22 #include <bp-asm.h>
24 /* int [r3] rawmemchr (void *s [r3], int c [r4])  */
25         .machine  power7
26 ENTRY (BP_SYM(__rawmemchr))
27         CALL_MCOUNT
28         dcbt    0,r3
29         clrrwi  r8,r3,2       /* Align the address to word boundary.  */
31         /* Replicate byte to word.  */
32         rlwimi  r4,r4,8,16,23
33         rlwimi  r4,r4,16,0,15
35         /* Now r4 has a word of c bytes.  */
37         rlwinm  r6,r3,3,27,28 /* Calculate padding.  */
38         lwz     r12,0(r8)     /* Load word from memory.  */
39         cmpb    r5,r12,r4     /* Compare each byte against c byte.  */
40         slw     r5,r5,r6      /* Move left to discard ignored bits.  */
41         srw     r5,r5,r6      /* Bring the bits back as zeros.  */
42         cmpwi   cr7,r5,0      /* If r5 == 0, no c bytes have been found.  */
43         bne     cr7,L(done)
45         mtcrf   0x01,r8
47         /* Are we now aligned to a doubleword boundary?  If so, skip to
48            the main loop.  Otherwise, go through the alignment code.  */
50         bt      29,L(loop)
52         /* Handle WORD2 of pair.  */
53         lwzu    r12,4(r8)
54         cmpb    r5,r12,r4
55         cmpwi   cr7,r5,0
56         bne     cr7,L(done)
57         b       L(loop)       /* We branch here (rather than falling through)
58                                  to skip the nops due to heavy alignment
59                                  of the loop below.  */
61         /* Main loop to look for the end of the string.  Since it's a
62            small loop (< 8 instructions), align it to 32-bytes.  */
63         .p2align  5
64 L(loop):
65         /* Load two words, compare and merge in a
66            single register for speed.  This is an attempt
67            to speed up the byte-checking process for bigger strings.  */
68         lwz     r12,4(r8)
69         lwzu    r11,8(r8)
70         cmpb    r5,r12,r4
71         cmpb    r6,r11,r4
72         or      r7,r5,r6
73         cmpwi   cr7,r7,0
74         beq     cr7,L(loop)
76         /* OK, one (or both) of the words contains a 'c' byte.  Check
77            the first word and decrement the address in case the first
78            word really contains a c byte.  */
80         cmpwi   cr6,r5,0
81         addi    r8,r8,-4
82         bne     cr6,L(done)
84         /* The 'c' byte must be in the second word.  Adjust the address
85            again and move the result of cmpb to r10 so we can calculate the
86            pointer.  */
87         mr      r5,r6
88         addi    r8,r8,4
90         /* r5 has the output of the cmpb instruction, that is, it contains
91            0xff in the same position as the 'c' byte in the original
92            word from the string.  Use that fact to find out what is
93            the position of the byte inside the string.  */
94 L(done):
95         cntlzw  r0,r5         /* Count leading zeros before the match.  */
96         srwi    r0,r0,3       /* Convert leading zeroes to bytes.  */
97         add     r3,r8,r0      /* Return address of the matching char.  */
98         blr
99 END (BP_SYM (__rawmemchr))
100 weak_alias (__rawmemchr,rawmemchr)
101 libc_hidden_builtin_def (__rawmemchr)