1 /* Optimized strchr implementation for PowerPC64.
2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
21 /* See strlen.s for comments on how this works. */
23 /* char * [r3] strchr (const char *s [r3] , int c [r4] ) */
29 #define rRTN r3 /* outgoing result */
30 #define rSTR r8 /* current word pointer */
31 #define rCHR r4 /* byte we're looking for, spread over the whole word */
32 #define rWORD r5 /* the current word */
33 #define rCLZB rCHR /* leading zero byte count */
34 #define rFEFE r6 /* constant 0xfefefefefefefeff (-0x0101010101010101) */
35 #define r7F7F r7 /* constant 0x7f7f7f7f7f7f7f7f */
37 #define rIGN r10 /* number of bits we should ignore in the first word */
38 #define rMASK r11 /* mask with the bits to ignore set to 0 */
42 rlwimi rCHR, rCHR, 8, 16, 23
44 rlwimi rCHR, rCHR, 16, 0, 15
45 rlwinm rIGN, rRTN, 3, 26, 28
46 insrdi rCHR, rCHR, 32, 0
50 addi rFEFE, rFEFE, -0x101
51 addi r7F7F, r7F7F, 0x7f7f
53 insrdi r7F7F, r7F7F, 32, 0
54 add rFEFE, rFEFE, rTMP1
55 /* Test the first (partial?) word. */
57 srd rMASK, rMASK, rIGN
58 orc rWORD, rWORD, rMASK
59 add rTMP1, rFEFE, rWORD
60 nor rTMP2, r7F7F, rWORD
61 and. rTMP1, rTMP1, rTMP2
62 xor rTMP3, rCHR, rWORD
63 orc rTMP3, rTMP3, rMASK
68 L(loop):ldu rWORD, 8(rSTR)
69 and. rTMP1, rTMP1, rTMP2
71 add rTMP1, rFEFE, rWORD
72 nor rTMP2, r7F7F, rWORD
74 and. rTMP1, rTMP1, rTMP2
75 /* Start test for the bytes we're looking for. */
76 xor rTMP3, rCHR, rWORD
78 add rTMP1, rFEFE, rTMP3
79 nor rTMP2, r7F7F, rTMP3
81 /* There is a zero byte in the word, but may also be a matching byte (either
82 before or after the zero byte). In fact, we may be looking for a
83 zero byte, in which case we return a match. We guess that this hasn't
86 and. rTMP1, rTMP1, rTMP2
89 /* It did happen. Decide which one was first...
90 I'm not sure if this is actually faster than a sequence of
91 rotates, compares, and branches (we use it anyway because it's shorter). */
92 and rFEFE, r7F7F, rWORD
93 or rMASK, r7F7F, rWORD
94 and rTMP1, r7F7F, rTMP3
96 add rFEFE, rFEFE, r7F7F
97 add rTMP1, rTMP1, r7F7F
98 nor rWORD, rMASK, rFEFE
99 nor rTMP2, rIGN, rTMP1
104 add rRTN, rSTR, rCLZB
108 and rTMP1, r7F7F, rTMP3
109 or rIGN, r7F7F, rTMP3
110 add rTMP1, rTMP1, r7F7F
111 nor rTMP2, rIGN, rTMP1
115 add rRTN, rSTR, rCLZB
119 weak_alias (strchr, index)
120 libc_hidden_builtin_def (strchr)