1/* $OpenBSD: memmove.S,v 1.5 2022/06/10 01:56:02 guenther 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 "DEFS.h" 43 44 .text 45 46/* void *memcpy(void *to, const void *from, size_t len) */ 47#if 0 48ENTRY(memcpy) 49 RETGUARD_SETUP(memmove, %r11, %r12) 50 mr %r8, %r3 /* Save dst (return value) */ 51 b .Lfwd 52#endif 53 54/* void bcopy(void *, void *, size_t) */ 55ENTRY_NB(bcopy) 56 mr %r6, %r3 /* swap src/dst */ 57 mr %r3, %r4 58 mr %r4, %r6 59 60/* void *memmove(void *, const void *, size_t) */ 61ENTRY(memmove) 62 RETGUARD_SETUP(memmove, %r11, %r12) 63 mr %r8, %r3 /* Save dst (return value) */ 64 65 cmpw %r4, %r8 /* Branch to reverse if */ 66 blt .Lreverse /* src < dest. Don't want to */ 67 /* overwrite end of src with */ 68 /* start of dest */ 69 70.Lfwd: 71 addi %r4, %r4, -4 /* Back up src and dst pointers */ 72 addi %r8, %r8, -4 /* due to auto-update of 'load' */ 73 74 srwi. %r9,%r5,2 /* How many words in total cnt */ 75 beq- .Llast1 /* Handle byte by byte if < 4 */ 76 /* bytes total */ 77 mtctr %r9 /* Count of words for loop */ 78 lwzu %r7, 4(%r4) /* Preload first word */ 79 80 b .Lg1 81 82.Lg0: /* Main loop */ 83 84 lwzu %r7, 4(%r4) /* Load a new word */ 85 stwu %r6, 4(%r8) /* Store previous word */ 86 87.Lg1: 88 89 bdz- .Llast /* Dec cnt, and branch if just */ 90 /* one word to store */ 91 lwzu %r6, 4(%r4) /* Load another word */ 92 stwu %r7, 4(%r8) /* Store previous word */ 93 bdnz+ .Lg0 /* Dec cnt, and loop again if */ 94 /* more words */ 95 mr %r7, %r6 /* If word count -> 0, then... */ 96 97.Llast: 98 99 stwu %r7, 4(%r8) /* ... store last word */ 100 101.Llast1: /* Byte-by-byte copy */ 102 103 clrlwi. %r5,%r5,30 /* If count -> 0, then ... */ 104 beq .Ldone /* we're done */ 105 106 mtctr %r5 /* else load count for loop */ 107 108 lbzu %r6, 4(%r4) /* 1st byte: update addr by 4 */ 109 stbu %r6, 4(%r8) /* since we pre-adjusted by 4 */ 110 bdz- .Ldone /* in anticipation of main loop */ 111 112.Llast2: 113 114 lbzu %r6, 1(%r4) /* But handle the rest by */ 115 stbu %r6, 1(%r8) /* updating addr by 1 */ 116 bdnz+ .Llast2 117 b .Ldone 118 119 /* We're here since src < dest. Don't want to overwrite end of */ 120 /* src with start of dest */ 121 122.Lreverse: 123 124 add %r4, %r4, %r5 /* Work from end to beginning */ 125 add %r8, %r8, %r5 /* so add count to string ptrs */ 126 srwi. %r9,%r5,2 /* Words in total count */ 127 beq- .Lrlast1 /* Handle byte by byte if < 4 */ 128 /* bytes total */ 129 130 mtctr %r9 /* Count of words for loop */ 131 132 lwzu %r7, -4(%r4) /* Preload first word */ 133 b .Lrg1 134 135.Lrg0: /* Main loop */ 136 137 lwzu %r7, -4(%r4) /* Load a new word */ 138 stwu %r6, -4(%r8) /* Store previous word */ 139 140.Lrg1: 141 142 bdz- .Lrlast /* Dec cnt, and branch if just */ 143 /* one word to store */ 144 145 lwzu %r6, -4(%r4) /* Load another word */ 146 stwu %r7, -4(%r8) /* Store previous word */ 147 148 bdnz+ .Lrg0 /* Dec cnt, and loop again if */ 149 /* more words */ 150 151 mr %r7, %r6 /* If word count -> 0, then... */ 152 153.Lrlast: 154 155 stwu %r7, -4(%r8) /* ... store last word */ 156 157.Lrlast1: /* Byte-by-byte copy */ 158 159 clrlwi. %r5,%r5,30 /* If count -> 0, then... */ 160 beq .Ldone /* ... we're done */ 161 162 mtctr %r5 /* else load count for loop */ 163 164.Lrlast2: 165 166 lbzu %r6, -1(%r4) /* Handle the rest, byte by */ 167 stbu %r6, -1(%r8) /* byte */ 168 169 bdnz+ .Lrlast2 /* Dec ctr, and branch if more */ 170 /* bytes left */ 171.Ldone: 172 RETGUARD_CHECK(memmove, %r11, %r12) 173 blr 174END_STRONG(memmove) 175END_WEAK(bcopy) 176