blob: 56bb3b824e7fcfb6edee7eb48b759e404ce9592a [file] [log] [blame]
wdenk40c85552000-07-19 14:09:16 +00001/*
2 * String handling functions for PowerPC.
3 *
4 * Copyright (C) 1996 Paul Mackerras.
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk40c85552000-07-19 14:09:16 +00007 */
8#include <ppc_asm.tmpl>
9#include <asm/errno.h>
10
11 .globl strcpy
12strcpy:
13 addi r5,r3,-1
14 addi r4,r4,-1
151: lbzu r0,1(r4)
16 cmpwi 0,r0,0
17 stbu r0,1(r5)
18 bne 1b
19 blr
20
21 .globl strncpy
22strncpy:
23 cmpwi 0,r5,0
24 beqlr
25 mtctr r5
26 addi r6,r3,-1
27 addi r4,r4,-1
281: lbzu r0,1(r4)
29 cmpwi 0,r0,0
30 stbu r0,1(r6)
31 bdnzf 2,1b /* dec ctr, branch if ctr != 0 && !cr0.eq */
32 blr
33
34 .globl strcat
35strcat:
36 addi r5,r3,-1
37 addi r4,r4,-1
381: lbzu r0,1(r5)
39 cmpwi 0,r0,0
40 bne 1b
41 addi r5,r5,-1
421: lbzu r0,1(r4)
43 cmpwi 0,r0,0
44 stbu r0,1(r5)
45 bne 1b
46 blr
47
48 .globl strcmp
49strcmp:
50 addi r5,r3,-1
51 addi r4,r4,-1
521: lbzu r3,1(r5)
53 cmpwi 1,r3,0
54 lbzu r0,1(r4)
55 subf. r3,r0,r3
56 beqlr 1
57 beq 1b
58 blr
59
60 .globl strlen
61strlen:
62 addi r4,r3,-1
631: lbzu r0,1(r4)
64 cmpwi 0,r0,0
65 bne 1b
66 subf r3,r3,r4
67 blr
68
69 .globl memset
70memset:
71 rlwimi r4,r4,8,16,23
72 rlwimi r4,r4,16,0,15
73 addi r6,r3,-4
74 cmplwi 0,r5,4
75 blt 7f
76 stwu r4,4(r6)
77 beqlr
78 andi. r0,r6,3
79 add r5,r0,r5
80 subf r6,r0,r6
81 rlwinm r0,r5,32-2,2,31
82 mtctr r0
83 bdz 6f
841: stwu r4,4(r6)
85 bdnz 1b
866: andi. r5,r5,3
877: cmpwi 0,r5,0
88 beqlr
89 mtctr r5
90 addi r6,r6,3
918: stbu r4,1(r6)
92 bdnz 8b
93 blr
94
wdenk40c85552000-07-19 14:09:16 +000095 .globl memmove
96memmove:
97 cmplw 0,r3,r4
98 bgt backwards_memcpy
99 /* fall through */
100
101 .globl memcpy
102memcpy:
103 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
104 addi r6,r3,-4
105 addi r4,r4,-4
106 beq 2f /* if less than 8 bytes to do */
107 andi. r0,r6,3 /* get dest word aligned */
108 mtctr r7
109 bne 5f
1101: lwz r7,4(r4)
111 lwzu r8,8(r4)
112 stw r7,4(r6)
113 stwu r8,8(r6)
114 bdnz 1b
115 andi. r5,r5,7
1162: cmplwi 0,r5,4
117 blt 3f
118 lwzu r0,4(r4)
119 addi r5,r5,-4
120 stwu r0,4(r6)
1213: cmpwi 0,r5,0
122 beqlr
123 mtctr r5
124 addi r4,r4,3
125 addi r6,r6,3
1264: lbzu r0,1(r4)
127 stbu r0,1(r6)
128 bdnz 4b
129 blr
1305: subfic r0,r0,4
131 mtctr r0
1326: lbz r7,4(r4)
133 addi r4,r4,1
134 stb r7,4(r6)
135 addi r6,r6,1
136 bdnz 6b
137 subf r5,r0,r5
138 rlwinm. r7,r5,32-3,3,31
139 beq 2b
140 mtctr r7
141 b 1b
142
143 .globl backwards_memcpy
144backwards_memcpy:
145 rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */
146 add r6,r3,r5
147 add r4,r4,r5
148 beq 2f
149 andi. r0,r6,3
150 mtctr r7
151 bne 5f
1521: lwz r7,-4(r4)
153 lwzu r8,-8(r4)
154 stw r7,-4(r6)
155 stwu r8,-8(r6)
156 bdnz 1b
157 andi. r5,r5,7
1582: cmplwi 0,r5,4
159 blt 3f
160 lwzu r0,-4(r4)
161 subi r5,r5,4
162 stwu r0,-4(r6)
1633: cmpwi 0,r5,0
164 beqlr
165 mtctr r5
1664: lbzu r0,-1(r4)
167 stbu r0,-1(r6)
168 bdnz 4b
169 blr
1705: mtctr r0
1716: lbzu r7,-1(r4)
172 stbu r7,-1(r6)
173 bdnz 6b
174 subf r5,r0,r5
175 rlwinm. r7,r5,32-3,3,31
176 beq 2b
177 mtctr r7
178 b 1b
179
180 .globl memcmp
181memcmp:
182 cmpwi 0,r5,0
183 ble- 2f
184 mtctr r5
185 addi r6,r3,-1
186 addi r4,r4,-1
1871: lbzu r3,1(r6)
188 lbzu r0,1(r4)
189 subf. r3,r0,r3
190 bdnzt 2,1b
191 blr
1922: li r3,0
193 blr
194
195 .global memchr
196memchr:
197 cmpwi 0,r5,0
198 ble- 2f
199 mtctr r5
200 addi r3,r3,-1
2011: lbzu r0,1(r3)
202 cmpw 0,r0,r4
203 bdnzf 2,1b
204 beqlr
2052: li r3,0
206 blr