]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc64/power7/rawmemchr.S
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power7 / rawmemchr.S
CommitLineData
fe2f79db 1/* Optimized rawmemchr implementation for PowerPC64/POWER7 using cmpb insn.
f7a9f785 2 Copyright (C) 2010-2016 Free Software Foundation, Inc.
fe2f79db
LM
3 Contributed by Luis Machado <luisgpm@br.ibm.com>.
4 This file is part of the GNU C Library.
5
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.
10
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.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
fe2f79db
LM
19
20#include <sysdep.h>
fe2f79db
LM
21
22/* int [r3] rawmemchr (void *s [r3], int c [r4]) */
23 .machine power7
2d67d91a 24ENTRY (__rawmemchr)
fe2f79db
LM
25 CALL_MCOUNT 2
26 dcbt 0,r3
27 clrrdi r8,r3,3 /* Align the address to doubleword boundary. */
28
29 /* Replicate byte to doubleword. */
466b0393
AM
30 insrdi r4,r4,8,48
31 insrdi r4,r4,16,32
fe2f79db
LM
32 insrdi r4,r4,32,0
33
34 /* Now r4 has a doubleword of c bytes. */
35
36 rlwinm r6,r3,3,26,28 /* Calculate padding. */
37 ld r12,0(r8) /* Load doubleword from memory. */
38 cmpb r5,r12,r4 /* Compare each byte against c byte. */
466b0393
AM
39#ifdef __LITTLE_ENDIAN__
40 srd r5,r5,r6
41 sld r5,r5,r6
42#else
fe2f79db
LM
43 sld r5,r5,r6 /* Move left to discard ignored bits. */
44 srd r5,r5,r6 /* Bring the bits back as zeros. */
466b0393 45#endif
fe2f79db
LM
46 cmpdi cr7,r5,0 /* If r5 == 0, no c bytes have been found. */
47 bne cr7,L(done)
48
49 mtcrf 0x01,r8
50
51 /* Are we now aligned to a quadword boundary? If so, skip to
52 the main loop. Otherwise, go through the alignment code. */
53
54 bt 28,L(loop)
55
56 /* Handle DWORD2 of pair. */
57 ldu r12,8(r8)
58 cmpb r5,r12,r4
59 cmpdi cr7,r5,0
60 bne cr7,L(done)
61 b L(loop) /* We branch here (rather than falling through)
62 to skip the nops due to heavy alignment
63 of the loop below. */
64
65 /* Main loop to look for the end of the string. Since it's a
66 small loop (< 8 instructions), align it to 32-bytes. */
67 .p2align 5
68L(loop):
69 /* Load two doublewords, compare and merge in a
70 single register for speed. This is an attempt
71 to speed up the byte-checking process for bigger strings. */
72 ld r12,8(r8)
73 ldu r11,16(r8)
74 cmpb r5,r12,r4
75 cmpb r6,r11,r4
76 or r7,r5,r6
77 cmpdi cr7,r7,0
78 beq cr7,L(loop)
79
80 /* OK, one (or both) of the doublewords contains a 'c' byte. Check
81 the first doubleword and decrement the address in case the first
82 doubleword really contains a c byte. */
83
84 cmpdi cr6,r5,0
85 addi r8,r8,-8
86 bne cr6,L(done)
87
88 /* The 'c' byte must be in the second doubleword. Adjust the address
89 again and move the result of cmpb to r10 so we can calculate the
90 pointer. */
91 mr r5,r6
92 addi r8,r8,8
93
94 /* r5 has the output of the cmpb instruction, that is, it contains
95 0xff in the same position as the 'c' byte in the original
96 doubleword from the string. Use that fact to find out what is
97 the position of the byte inside the string. */
98L(done):
466b0393
AM
99#ifdef __LITTLE_ENDIAN__
100 addi r0,r5,-1
101 andc r0,r0,r5
102 popcntd r0,r0 /* Count trailing zeros. */
103#else
fe2f79db 104 cntlzd r0,r5 /* Count leading zeros before the match. */
466b0393
AM
105#endif
106 srdi r0,r0,3 /* Convert leading zeros to bytes. */
fe2f79db
LM
107 add r3,r8,r0 /* Return address of the matching char. */
108 blr
2d67d91a 109END (__rawmemchr)
fe2f79db
LM
110weak_alias (__rawmemchr,rawmemchr)
111libc_hidden_builtin_def (__rawmemchr)