wdenk | 40c8555 | 2000-07-19 14:09:16 +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 | 40c8555 | 2000-07-19 14:09:16 +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 | |
wdenk | 40c8555 | 2000-07-19 14:09:16 +0000 | [diff] [blame] | 95 | .globl memmove |
| 96 | memmove: |
| 97 | cmplw 0,r3,r4 |
| 98 | bgt backwards_memcpy |
| 99 | /* fall through */ |
| 100 | |
| 101 | .globl memcpy |
| 102 | memcpy: |
| 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 |
| 110 | 1: 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 |
| 116 | 2: cmplwi 0,r5,4 |
| 117 | blt 3f |
| 118 | lwzu r0,4(r4) |
| 119 | addi r5,r5,-4 |
| 120 | stwu r0,4(r6) |
| 121 | 3: cmpwi 0,r5,0 |
| 122 | beqlr |
| 123 | mtctr r5 |
| 124 | addi r4,r4,3 |
| 125 | addi r6,r6,3 |
| 126 | 4: lbzu r0,1(r4) |
| 127 | stbu r0,1(r6) |
| 128 | bdnz 4b |
| 129 | blr |
| 130 | 5: subfic r0,r0,4 |
| 131 | mtctr r0 |
| 132 | 6: 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 |
| 144 | backwards_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 |
| 152 | 1: 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 |
| 158 | 2: cmplwi 0,r5,4 |
| 159 | blt 3f |
| 160 | lwzu r0,-4(r4) |
| 161 | subi r5,r5,4 |
| 162 | stwu r0,-4(r6) |
| 163 | 3: cmpwi 0,r5,0 |
| 164 | beqlr |
| 165 | mtctr r5 |
| 166 | 4: lbzu r0,-1(r4) |
| 167 | stbu r0,-1(r6) |
| 168 | bdnz 4b |
| 169 | blr |
| 170 | 5: mtctr r0 |
| 171 | 6: 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 |
| 181 | memcmp: |
| 182 | cmpwi 0,r5,0 |
| 183 | ble- 2f |
| 184 | mtctr r5 |
| 185 | addi r6,r3,-1 |
| 186 | addi r4,r4,-1 |
| 187 | 1: lbzu r3,1(r6) |
| 188 | lbzu r0,1(r4) |
| 189 | subf. r3,r0,r3 |
| 190 | bdnzt 2,1b |
| 191 | blr |
| 192 | 2: li r3,0 |
| 193 | blr |
| 194 | |
| 195 | .global memchr |
| 196 | memchr: |
| 197 | cmpwi 0,r5,0 |
| 198 | ble- 2f |
| 199 | mtctr r5 |
| 200 | addi r3,r3,-1 |
| 201 | 1: lbzu r0,1(r3) |
| 202 | cmpw 0,r0,r4 |
| 203 | bdnzf 2,1b |
| 204 | beqlr |
| 205 | 2: li r3,0 |
| 206 | blr |