wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * String handling functions for PowerPC. |
| 3 | * |
| 4 | * Copyright (C) 1996 Paul Mackerras. |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c7de829 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 7 | */ |
| 8 | #include <ppc_asm.tmpl> |
| 9 | #include <asm/errno.h> |
| 10 | |
| 11 | .globl strcpy |
| 12 | strcpy: |
| 13 | addi r5,r3,-1 |
| 14 | addi r4,r4,-1 |
| 15 | 1: lbzu r0,1(r4) |
| 16 | cmpwi 0,r0,0 |
| 17 | stbu r0,1(r5) |
| 18 | bne 1b |
| 19 | blr |
| 20 | |
| 21 | .globl strncpy |
| 22 | strncpy: |
| 23 | cmpwi 0,r5,0 |
| 24 | beqlr |
| 25 | mtctr r5 |
| 26 | addi r6,r3,-1 |
| 27 | addi r4,r4,-1 |
| 28 | 1: 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 |
| 35 | strcat: |
| 36 | addi r5,r3,-1 |
| 37 | addi r4,r4,-1 |
| 38 | 1: lbzu r0,1(r5) |
| 39 | cmpwi 0,r0,0 |
| 40 | bne 1b |
| 41 | addi r5,r5,-1 |
| 42 | 1: lbzu r0,1(r4) |
| 43 | cmpwi 0,r0,0 |
| 44 | stbu r0,1(r5) |
| 45 | bne 1b |
| 46 | blr |
| 47 | |
| 48 | .globl strcmp |
| 49 | strcmp: |
| 50 | addi r5,r3,-1 |
| 51 | addi r4,r4,-1 |
| 52 | 1: 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 |
| 61 | strlen: |
| 62 | addi r4,r3,-1 |
| 63 | 1: lbzu r0,1(r4) |
| 64 | cmpwi 0,r0,0 |
| 65 | bne 1b |
| 66 | subf r3,r3,r4 |
| 67 | blr |
| 68 | |
| 69 | .globl memset |
| 70 | memset: |
| 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 |
| 84 | 1: stwu r4,4(r6) |
| 85 | bdnz 1b |
| 86 | 6: andi. r5,r5,3 |
| 87 | 7: cmpwi 0,r5,0 |
| 88 | beqlr |
| 89 | mtctr r5 |
| 90 | addi r6,r6,3 |
| 91 | 8: stbu r4,1(r6) |
| 92 | bdnz 8b |
| 93 | blr |
| 94 | |
| 95 | .globl bcopy |
| 96 | bcopy: |
| 97 | mr r6,r3 |
| 98 | mr r3,r4 |
| 99 | mr r4,r6 |
| 100 | b memcpy |
| 101 | |
| 102 | .globl memmove |
| 103 | memmove: |
| 104 | cmplw 0,r3,r4 |
| 105 | bgt backwards_memcpy |
| 106 | /* fall through */ |
| 107 | |
| 108 | .globl memcpy |
| 109 | memcpy: |
| 110 | rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */ |
| 111 | addi r6,r3,-4 |
| 112 | addi r4,r4,-4 |
| 113 | beq 2f /* if less than 8 bytes to do */ |
| 114 | andi. r0,r6,3 /* get dest word aligned */ |
| 115 | mtctr r7 |
| 116 | bne 5f |
| 117 | 1: lwz r7,4(r4) |
| 118 | lwzu r8,8(r4) |
| 119 | stw r7,4(r6) |
| 120 | stwu r8,8(r6) |
| 121 | bdnz 1b |
| 122 | andi. r5,r5,7 |
| 123 | 2: cmplwi 0,r5,4 |
| 124 | blt 3f |
| 125 | lwzu r0,4(r4) |
| 126 | addi r5,r5,-4 |
| 127 | stwu r0,4(r6) |
| 128 | 3: cmpwi 0,r5,0 |
| 129 | beqlr |
| 130 | mtctr r5 |
| 131 | addi r4,r4,3 |
| 132 | addi r6,r6,3 |
| 133 | 4: lbzu r0,1(r4) |
| 134 | stbu r0,1(r6) |
| 135 | bdnz 4b |
| 136 | blr |
| 137 | 5: subfic r0,r0,4 |
| 138 | mtctr r0 |
| 139 | 6: lbz r7,4(r4) |
| 140 | addi r4,r4,1 |
| 141 | stb r7,4(r6) |
| 142 | addi r6,r6,1 |
| 143 | bdnz 6b |
| 144 | subf r5,r0,r5 |
| 145 | rlwinm. r7,r5,32-3,3,31 |
| 146 | beq 2b |
| 147 | mtctr r7 |
| 148 | b 1b |
| 149 | |
| 150 | .globl backwards_memcpy |
| 151 | backwards_memcpy: |
| 152 | rlwinm. r7,r5,32-3,3,31 /* r0 = r5 >> 3 */ |
| 153 | add r6,r3,r5 |
| 154 | add r4,r4,r5 |
| 155 | beq 2f |
| 156 | andi. r0,r6,3 |
| 157 | mtctr r7 |
| 158 | bne 5f |
| 159 | 1: lwz r7,-4(r4) |
| 160 | lwzu r8,-8(r4) |
| 161 | stw r7,-4(r6) |
| 162 | stwu r8,-8(r6) |
| 163 | bdnz 1b |
| 164 | andi. r5,r5,7 |
| 165 | 2: cmplwi 0,r5,4 |
| 166 | blt 3f |
| 167 | lwzu r0,-4(r4) |
| 168 | subi r5,r5,4 |
| 169 | stwu r0,-4(r6) |
| 170 | 3: cmpwi 0,r5,0 |
| 171 | beqlr |
| 172 | mtctr r5 |
| 173 | 4: lbzu r0,-1(r4) |
| 174 | stbu r0,-1(r6) |
| 175 | bdnz 4b |
| 176 | blr |
| 177 | 5: mtctr r0 |
| 178 | 6: lbzu r7,-1(r4) |
| 179 | stbu r7,-1(r6) |
| 180 | bdnz 6b |
| 181 | subf r5,r0,r5 |
| 182 | rlwinm. r7,r5,32-3,3,31 |
| 183 | beq 2b |
| 184 | mtctr r7 |
| 185 | b 1b |
| 186 | |
| 187 | .globl memcmp |
| 188 | memcmp: |
| 189 | cmpwi 0,r5,0 |
| 190 | ble- 2f |
| 191 | mtctr r5 |
| 192 | addi r6,r3,-1 |
| 193 | addi r4,r4,-1 |
| 194 | 1: lbzu r3,1(r6) |
| 195 | lbzu r0,1(r4) |
| 196 | subf. r3,r0,r3 |
| 197 | bdnzt 2,1b |
| 198 | blr |
| 199 | 2: li r3,0 |
| 200 | blr |
| 201 | |
| 202 | .global memchr |
| 203 | memchr: |
| 204 | cmpwi 0,r5,0 |
| 205 | ble- 2f |
| 206 | mtctr r5 |
| 207 | addi r3,r3,-1 |
| 208 | 1: lbzu r0,1(r3) |
| 209 | cmpw 0,r0,r4 |
| 210 | bdnzf 2,1b |
| 211 | beqlr |
| 212 | 2: li r3,0 |
| 213 | blr |