1/* $OpenBSD: memmove.S,v 1.2 2015/08/31 02:53:57 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 "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 mr %r8, %r3 /* Save dst (return value) */ 60 61 cmpw %r4, %r8 /* Branch to reverse if */ 62 blt reverse /* src < dest. Don't want to */ 63 /* overwrite end of src with */ 64 /* start of dest */ 65 66fwd: 67 addi %r4, %r4, -4 /* Back up src and dst pointers */ 68 addi %r8, %r8, -4 /* due to auto-update of 'load' */ 69 70 srwi. %r9,%r5,2 /* How many words in total cnt */ 71 beq- last1 /* Handle byte by byte if < 4 */ 72 /* bytes total */ 73 mtctr %r9 /* Count of words for loop */ 74 lwzu %r7, 4(%r4) /* Preload first word */ 75 76 b g1 77 78g0: /* Main loop */ 79 80 lwzu %r7, 4(%r4) /* Load a new word */ 81 stwu %r6, 4(%r8) /* Store previous word */ 82 83g1: 84 85 bdz- last /* Dec cnt, and branch if just */ 86 /* one word to store */ 87 lwzu %r6, 4(%r4) /* Load another word */ 88 stwu %r7, 4(%r8) /* Store previous word */ 89 bdnz+ g0 /* Dec cnt, and loop again if */ 90 /* more words */ 91 mr %r7, %r6 /* If word count -> 0, then... */ 92 93last: 94 95 stwu %r7, 4(%r8) /* ... store last word */ 96 97last1: /* Byte-by-byte copy */ 98 99 clrlwi. %r5,%r5,30 /* If count -> 0, then ... */ 100 beqlr /* we're done */ 101 102 mtctr %r5 /* else load count for loop */ 103 104 lbzu %r6, 4(%r4) /* 1st byte: update addr by 4 */ 105 stbu %r6, 4(%r8) /* since we pre-adjusted by 4 */ 106 bdzlr- /* in anticipation of main loop */ 107 108last2: 109 110 lbzu %r6, 1(%r4) /* But handle the rest by */ 111 stbu %r6, 1(%r8) /* updating addr by 1 */ 112 bdnz+ last2 113 114 blr 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 */ 168 blr 169END_STRONG(memmove) 170END_WEAK(bcopy) 171