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