1/* $OpenBSD: memmove.S,v 1.6 2020/11/28 19:49:30 gkoehler Exp $ */ 2/* $NetBSD: memmove.S,v 1.3 2011/01/15 07:31:12 matt Exp $ */ 3 4/* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37 5 * ========================================================================== 6 * Optimized memmove implementation for IBM PowerPC 405/440. 7 * 8 * Copyright (c) 2003, IBM Corporation 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * * Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * * Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * * Neither the name of IBM nor the names of its contributors 23 * may be used to endorse or promote products derived from this 24 * software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 27 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 28 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 32 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 35 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39 * ========================================================================== 40 */ 41 42#include <machine/asm.h> 43 44 .text 45 46/* void *memcpy(void *to, const void *from, size_t len) */ 47ENTRY(memcpy) 48 RETGUARD_SETUP(memmove, %r11, %r12) 49 mr %r8, %r3 /* Save dst (return value) */ 50 b fwd 51 52/* void bcopy(void *, void *, size_t) */ 53ENTRY(bcopy) 54 mr %r6, %r3 /* swap src/dst */ 55 mr %r3, %r4 56 mr %r4, %r6 57 58/* void *memmove(void *, const void *, size_t) */ 59ENTRY(memmove) 60 RETGUARD_SETUP(memmove, %r11, %r12) 61 mr %r8, %r3 /* Save dst (return value) */ 62 63 cmpw %r4, %r8 /* Branch to reverse if */ 64 blt reverse /* src < dest. Don't want to */ 65 /* overwrite end of src with */ 66 /* start of dest */ 67 68fwd: 69 addi %r4, %r4, -4 /* Back up src and dst pointers */ 70 addi %r8, %r8, -4 /* due to auto-update of 'load' */ 71 72 srwi. %r9,%r5,2 /* How many words in total cnt */ 73 beq- last1 /* Handle byte by byte if < 4 */ 74 /* bytes total */ 75 mtctr %r9 /* Count of words for loop */ 76 lwzu %r7, 4(%r4) /* Preload first word */ 77 78 b g1 79 80g0: /* Main loop */ 81 82 lwzu %r7, 4(%r4) /* Load a new word */ 83 stwu %r6, 4(%r8) /* Store previous word */ 84 85g1: 86 87 bdz- last /* Dec cnt, and branch if just */ 88 /* one word to store */ 89 lwzu %r6, 4(%r4) /* Load another word */ 90 stwu %r7, 4(%r8) /* Store previous word */ 91 bdnz+ g0 /* Dec cnt, and loop again if */ 92 /* more words */ 93 mr %r7, %r6 /* If word count -> 0, then... */ 94 95last: 96 97 stwu %r7, 4(%r8) /* ... store last word */ 98 99last1: /* Byte-by-byte copy */ 100 101 clrlwi. %r5,%r5,30 /* If count -> 0, then ... */ 102 beq done /* we're done */ 103 104 mtctr %r5 /* else load count for loop */ 105 106 lbzu %r6, 4(%r4) /* 1st byte: update addr by 4 */ 107 stbu %r6, 4(%r8) /* since we pre-adjusted by 4 */ 108 bdz- done /* in anticipation of main loop */ 109 110last2: 111 112 lbzu %r6, 1(%r4) /* But handle the rest by */ 113 stbu %r6, 1(%r8) /* updating addr by 1 */ 114 bdnz+ last2 115 b done 116 117 /* We're here since src < dest. Don't want to overwrite end of */ 118 /* src with start of dest */ 119 120reverse: 121 122 add %r4, %r4, %r5 /* Work from end to beginning */ 123 add %r8, %r8, %r5 /* so add count to string ptrs */ 124 srwi. %r9,%r5,2 /* Words in total count */ 125 beq- rlast1 /* Handle byte by byte if < 4 */ 126 /* bytes total */ 127 128 mtctr %r9 /* Count of words for loop */ 129 130 lwzu %r7, -4(%r4) /* Preload first word */ 131 b rg1 132 133rg0: /* Main loop */ 134 135 lwzu %r7, -4(%r4) /* Load a new word */ 136 stwu %r6, -4(%r8) /* Store previous word */ 137 138rg1: 139 140 bdz- rlast /* Dec cnt, and branch if just */ 141 /* one word to store */ 142 143 lwzu %r6, -4(%r4) /* Load another word */ 144 stwu %r7, -4(%r8) /* Store previous word */ 145 146 bdnz+ rg0 /* Dec cnt, and loop again if */ 147 /* more words */ 148 149 mr %r7, %r6 /* If word count -> 0, then... */ 150 151rlast: 152 153 stwu %r7, -4(%r8) /* ... store last word */ 154 155rlast1: /* Byte-by-byte copy */ 156 157 clrlwi. %r5,%r5,30 /* If count -> 0, then... */ 158 beq done /* ... we're done */ 159 160 mtctr %r5 /* else load count for loop */ 161 162rlast2: 163 164 lbzu %r6, -1(%r4) /* Handle the rest, byte by */ 165 stbu %r6, -1(%r8) /* byte */ 166 167 bdnz+ rlast2 /* Dec ctr, and branch if more */ 168 /* bytes left */ 169done: 170 RETGUARD_CHECK(memmove, %r11, %r12) 171 blr 172