]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc64/power7/strchrnul.S
586c76950ac855122a4cd3952ef208ee570aa959
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc64 / power7 / strchrnul.S
1 /* Optimized strchrnul implementation for PowerPC64/POWER7 using cmpb insn.
2 Copyright (C) 2010-2014 Free Software Foundation, Inc.
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
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <sysdep.h>
21
22 /* int [r3] strchrnul (char *s [r3], int c [r4]) */
23 .machine power7
24 ENTRY (__strchrnul)
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. */
30 insrdi r4,r4,8,48
31 insrdi r4,r4,16,32
32 insrdi r4,r4,32,0
33
34 rlwinm r6,r3,3,26,28 /* Calculate padding. */
35 ld r12,0(r8) /* Load doubleword from memory. */
36 li r0,0 /* Doubleword with null chars to use
37 with cmpb. */
38
39 /* Now r4 has a doubleword of c bytes and r0 has
40 a doubleword of null bytes. */
41
42 cmpb r10,r12,r0 /* Compare each byte against c byte. */
43 cmpb r9,r12,r4 /* Compare each byte against null byte. */
44
45 /* Move the doublewords left and right to discard the bits that are
46 not part of the string and to bring them back as zeros. */
47 #ifdef __LITTLE_ENDIAN__
48 srd r10,r10,r6
49 srd r9,r9,r6
50 sld r10,r10,r6
51 sld r9,r9,r6
52 #else
53 sld r10,r10,r6
54 sld r9,r9,r6
55 srd r10,r10,r6
56 srd r9,r9,r6
57 #endif
58 or r5,r9,r10 /* OR the results to speed things up. */
59 cmpdi cr7,r5,0 /* If r5 == 0, no c or null bytes
60 have been found. */
61 bne cr7,L(done)
62
63 mtcrf 0x01,r8
64
65 /* Are we now aligned to a quadword boundary? If so, skip to
66 the main loop. Otherwise, go through the alignment code. */
67
68 bt 28,L(loop)
69
70 /* Handle DWORD2 of pair. */
71 ldu r12,8(r8)
72 cmpb r10,r12,r0
73 cmpb r9,r12,r4
74 or r5,r9,r10
75 cmpdi cr7,r5,0
76 bne cr7,L(done)
77 b L(loop) /* We branch here (rather than falling through)
78 to skip the nops due to heavy alignment
79 of the loop below. */
80
81 .p2align 5
82 L(loop):
83 /* Load two doublewords, compare and merge in a
84 single register for speed. This is an attempt
85 to speed up the null-checking process for bigger strings. */
86 ld r12,8(r8)
87 ldu r11,16(r8)
88 cmpb r10,r12,r0
89 cmpb r9,r12,r4
90 cmpb r6,r11,r0
91 cmpb r7,r11,r4
92 or r5,r9,r10
93 or r10,r6,r7
94 or r11,r5,r10
95 cmpdi cr7,r11,0
96 beq cr7,L(loop)
97
98 /* OK, one (or both) of the doublewords contains a c/null byte. Check
99 the first doubleword and decrement the address in case the first
100 doubleword really contains a c/null byte. */
101
102 cmpdi cr6,r5,0
103 addi r8,r8,-8
104 bne cr6,L(done)
105
106 /* The c/null byte must be in the second doubleword. Adjust the
107 address again and move the result of cmpb to r5 so we can calculate
108 the pointer. */
109 mr r5,r10
110 addi r8,r8,8
111
112 /* r5 has the output of the cmpb instruction, that is, it contains
113 0xff in the same position as the c/null byte in the original
114 doubleword from the string. Use that to calculate the pointer. */
115 L(done):
116 #ifdef __LITTLE_ENDIAN__
117 addi r0,r5,-1
118 andc r0,r0,r5
119 popcntd r0,r0
120 #else
121 cntlzd r0,r5 /* Count leading zeros before the match. */
122 #endif
123 srdi r0,r0,3 /* Convert leading zeros to bytes. */
124 add r3,r8,r0 /* Return address of matching c/null byte. */
125 blr
126 END (__strchrnul)
127 weak_alias (__strchrnul,strchrnul)
128 libc_hidden_builtin_def (__strchrnul)