1/* 2 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> 3 * Copyright (C) 2008-2009 PetaLogix 4 * Copyright (C) 2007 John Williams 5 * 6 * Reasonably optimised generic C-code for memcpy on Microblaze 7 * This is generic C code to do efficient, alignment-aware memmove. 8 * 9 * It is based on demo code originally Copyright 2001 by Intel Corp, taken from 10 * http://www.embedded.com/showArticle.jhtml?articleID=19205567 11 * 12 * Attempts were made, unsuccessfully, to contact the original 13 * author of this code (Michael Morrow, Intel). Below is the original 14 * copyright notice. 15 * 16 * This software has been developed by Intel Corporation. 17 * Intel specifically disclaims all warranties, express or 18 * implied, and all liability, including consequential and 19 * other indirect damages, for the use of this program, including 20 * liability for infringement of any proprietary rights, 21 * and including the warranties of merchantability and fitness 22 * for a particular purpose. Intel does not assume any 23 * responsibility for and errors which may appear in this program 24 * not any responsibility to update it. 25 */ 26 27#include <linux/types.h> 28#include <linux/stddef.h> 29#include <linux/compiler.h> 30#include <linux/module.h> 31#include <linux/string.h> 32 33#ifdef __HAVE_ARCH_MEMMOVE 34void *memmove(void *v_dst, const void *v_src, __kernel_size_t c) 35{ 36 const char *src = v_src; 37 char *dst = v_dst; 38 39#ifdef CONFIG_OPT_LIB_FUNCTION 40 const uint32_t *i_src; 41 uint32_t *i_dst; 42#endif 43 44 if (!c) 45 return v_dst; 46 47 /* Use memcpy when source is higher than dest */ 48 if (v_dst <= v_src) 49 return memcpy(v_dst, v_src, c); 50 51#ifndef CONFIG_OPT_LIB_FUNCTION 52 /* copy backwards, from end to beginning */ 53 src += c; 54 dst += c; 55 56 /* Simple, byte oriented memmove. */ 57 while (c--) 58 *--dst = *--src; 59 60 return v_dst; 61#else 62 /* The following code tries to optimize the copy by using unsigned 63 * alignment. This will work fine if both source and destination are 64 * aligned on the same boundary. However, if they are aligned on 65 * different boundaries shifts will be necessary. This might result in 66 * bad performance on MicroBlaze systems without a barrel shifter. 67 */ 68 /* Do a descending copy - this is a bit trickier! */ 69 dst += c; 70 src += c; 71 72 if (c >= 4) { 73 unsigned value, buf_hold; 74 75 /* Align the destination to a word boundry. */ 76 /* This is done in an endian independant manner. */ 77 78 switch ((unsigned long)dst & 3) { 79 case 3: 80 *--dst = *--src; 81 --c; 82 case 2: 83 *--dst = *--src; 84 --c; 85 case 1: 86 *--dst = *--src; 87 --c; 88 } 89 90 i_dst = (void *)dst; 91 /* Choose a copy scheme based on the source */ 92 /* alignment relative to dstination. */ 93 switch ((unsigned long)src & 3) { 94 case 0x0: /* Both byte offsets are aligned */ 95 96 i_src = (const void *)src; 97 98 for (; c >= 4; c -= 4) 99 *--i_dst = *--i_src; 100 101 src = (const void *)i_src; 102 break; 103 case 0x1: /* Unaligned - Off by 1 */ 104 /* Word align the source */ 105 i_src = (const void *) (((unsigned)src + 4) & ~3); 106 107 /* Load the holding buffer */ 108 buf_hold = *--i_src >> 24; 109 110 for (; c >= 4; c -= 4) { 111 value = *--i_src; 112 *--i_dst = buf_hold << 8 | value; 113 buf_hold = value >> 24; 114 } 115 116 /* Realign the source */ 117 src = (const void *)i_src; 118 src += 1; 119 break; 120 case 0x2: /* Unaligned - Off by 2 */ 121 /* Word align the source */ 122 i_src = (const void *) (((unsigned)src + 4) & ~3); 123 124 /* Load the holding buffer */ 125 buf_hold = *--i_src >> 16; 126 127 for (; c >= 4; c -= 4) { 128 value = *--i_src; 129 *--i_dst = buf_hold << 16 | value; 130 buf_hold = value >> 16; 131 } 132 133 /* Realign the source */ 134 src = (const void *)i_src; 135 src += 2; 136 break; 137 case 0x3: /* Unaligned - Off by 3 */ 138 /* Word align the source */ 139 i_src = (const void *) (((unsigned)src + 4) & ~3); 140 141 /* Load the holding buffer */ 142 buf_hold = *--i_src >> 8; 143 144 for (; c >= 4; c -= 4) { 145 value = *--i_src; 146 *--i_dst = buf_hold << 24 | value; 147 buf_hold = value >> 8; 148 } 149 150 /* Realign the source */ 151 src = (const void *)i_src; 152 src += 3; 153 break; 154 } 155 dst = (void *)i_dst; 156 } 157 158 /* simple fast copy, ... unless a cache boundry is crossed */ 159 /* Finish off any remaining bytes */ 160 switch (c) { 161 case 4: 162 *--dst = *--src; 163 case 3: 164 *--dst = *--src; 165 case 2: 166 *--dst = *--src; 167 case 1: 168 *--dst = *--src; 169 } 170 return v_dst; 171#endif 172} 173EXPORT_SYMBOL(memmove); 174#endif /* __HAVE_ARCH_MEMMOVE */ 175