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