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