memcpy原代码

jiashao606 2006-11-29 02:17:45
有谁有空 用C写个,里面不要调用其他函数和头文件
...全文
736 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
pyrophile 2006-11-30
  • 打赏
  • 举报
回复
void memcpy (unsigned char* output, unsigned char* input,unsigned int len)
{
unsigned int i;

for (i = 0; i < len; i++)
output[i] = input[i];
}
FBIq 2006-11-30
  • 打赏
  • 举报
回复
mark!
shawnwan 2006-11-30
  • 打赏
  • 举报
回复
直接用C的标准实现的就OK
25xxx25 2006-11-30
  • 打赏
  • 举报
回复
a
wskyo 2006-11-29
  • 打赏
  • 举报
回复
啊.为什么我的没有
HappyTree 2006-11-29
  • 打赏
  • 举报
回复
to jiashao606()
呵呵,验证应该不是很难吧?
MS的标准代码,就这么几行,看看就清楚了
greenteanet 2006-11-29
  • 打赏
  • 举报
回复
/*
* linux/arch/alpha/lib/memcpy.c
*
* Copyright (C) 1995 Linus Torvalds
*/

/*
* This is a reasonably optimized memcpy() routine.
*/

/*
* Note that the C code is written to be optimized into good assembly. However,
* at this point gcc is unable to sanely compile "if (n >= 0)", resulting in a
* explicit compare against 0 (instead of just using the proper "blt reg, xx" or
* "bge reg, xx"). I hope alpha-gcc will be fixed to notice this eventually..
*/

#include <linux/types.h>

/*
* This should be done in one go with ldq_u*2/mask/stq_u. Do it
* with a macro so that we can fix it up later..
*/
#define ALIGN_DEST_TO8_UP(d,s,n) \
while (d & 7) { \
if (n <= 0) return; \
n--; \
*(char *) d = *(char *) s; \
d++; s++; \
}
#define ALIGN_DEST_TO8_DN(d,s,n) \
while (d & 7) { \
if (n <= 0) return; \
n--; \
d--; s--; \
*(char *) d = *(char *) s; \
}

/*
* This should similarly be done with ldq_u*2/mask/stq. The destination
* is aligned, but we don't fill in a full quad-word
*/
#define DO_REST_UP(d,s,n) \
while (n > 0) { \
n--; \
*(char *) d = *(char *) s; \
d++; s++; \
}
#define DO_REST_DN(d,s,n) \
while (n > 0) { \
n--; \
d--; s--; \
*(char *) d = *(char *) s; \
}

/*
* This should be done with ldq/mask/stq. The source and destination are
* aligned, but we don't fill in a full quad-word
*/
#define DO_REST_ALIGNED_UP(d,s,n) DO_REST_UP(d,s,n)
#define DO_REST_ALIGNED_DN(d,s,n) DO_REST_DN(d,s,n)

/*
* This does unaligned memory copies. We want to avoid storing to
* an unaligned address, as that would do a read-modify-write cycle.
* We also want to avoid double-reading the unaligned reads.
*
* Note the ordering to try to avoid load (and address generation) latencies.
*/
static inline void __memcpy_unaligned_up (unsigned long d, unsigned long s,
long n)
{
ALIGN_DEST_TO8_UP(d,s,n);
n -= 8; /* to avoid compare against 8 in the loop */
if (n >= 0) {
unsigned long low_word, high_word;
__asm__("ldq_u %0,%1":"=r" (low_word):"m" (*(unsigned long *) s));
do {
unsigned long tmp;
__asm__("ldq_u %0,%1":"=r" (high_word):"m" (*(unsigned long *)(s+8)));
n -= 8;
__asm__("extql %1,%2,%0"
:"=r" (low_word)
:"r" (low_word), "r" (s));
__asm__("extqh %1,%2,%0"
:"=r" (tmp)
:"r" (high_word), "r" (s));
s += 8;
*(unsigned long *) d = low_word | tmp;
d += 8;
low_word = high_word;
} while (n >= 0);
}
n += 8;
DO_REST_UP(d,s,n);
}

static inline void __memcpy_unaligned_dn (unsigned long d, unsigned long s,
long n)
{
/* I don't understand AXP assembler well enough for this. -Tim */
s += n;
d += n;
while (n--)
* (char *) --d = * (char *) --s;
}

/*
* Hmm.. Strange. The __asm__ here is there to make gcc use an integer register
* for the load-store. I don't know why, but it would seem that using a floating
* point register for the move seems to slow things down (very small difference,
* though).
*
* Note the ordering to try to avoid load (and address generation) latencies.
*/
static inline void __memcpy_aligned_up (unsigned long d, unsigned long s,
long n)
{
ALIGN_DEST_TO8_UP(d,s,n);
n -= 8;
while (n >= 0) {
unsigned long tmp;
__asm__("ldq %0,%1":"=r" (tmp):"m" (*(unsigned long *) s));
n -= 8;
s += 8;
*(unsigned long *) d = tmp;
d += 8;
}
n += 8;
DO_REST_ALIGNED_UP(d,s,n);
}
static inline void __memcpy_aligned_dn (unsigned long d, unsigned long s,
long n)
{
s += n;
d += n;
ALIGN_DEST_TO8_DN(d,s,n);
n -= 8;
while (n >= 0) {
unsigned long tmp;
s -= 8;
__asm__("ldq %0,%1":"=r" (tmp):"m" (*(unsigned long *) s));
n -= 8;
d -= 8;
*(unsigned long *) d = tmp;
}
n += 8;
DO_REST_ALIGNED_DN(d,s,n);
}

void * memcpy(void * dest, const void *src, size_t n)
{
if (!(((unsigned long) dest ^ (unsigned long) src) & 7)) {
__memcpy_aligned_up ((unsigned long) dest, (unsigned long) src,
n);
return dest;
}
__memcpy_unaligned_up ((unsigned long) dest, (unsigned long) src, n);
return dest;
}

/* For backward modules compatibility, define __memcpy. */
asm("__memcpy = memcpy; .globl __memcpy");

HappyTree 2006-11-29
  • 打赏
  • 举报
回复
to egxsun()
一般都是以函数名来作文件名的,很好找的
jiashao606 2006-11-29
  • 打赏
  • 举报
回复
验证过了没 ,不会是不能用把?
bill1118qq 2006-11-29
  • 打赏
  • 举报
回复
void *memcpy(void* pca, void *pcb, int len)
{
while(len--)
{
*((char *)pca + len)= *((char *)pcb + len);
}
return pca;
}
egxsun 2006-11-29
  • 打赏
  • 举报
回复
to:HappyTree(天行健,君子以自强不息)
C:\Program Files\Microsoft Visual Studio 8\VC\crt\src下面有很多源代码哦,怎么知道哪些函数在那里
HappyTree 2006-11-29
  • 打赏
  • 举报
回复
我的代码都抄自
C:\Program Files\Microsoft Visual Studio 8\VC\crt\src
希望楼主也去抄一抄,呵呵
HappyTree 2006-11-29
  • 打赏
  • 举报
回复
void * __cdecl _memcpy_(void * dst, const void * src, size_t count)
{
void * ret = dst;

while (count--)
{
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}

return(ret);
}
KUCHIBUE 2006-11-29
  • 打赏
  • 举报
回复
VC的memcpy是asm地..要不我粘过来怎样..
neosu 2006-11-29
  • 打赏
  • 举报
回复

实际上memcpy并不是很简单.
比如说你的源区域和目的区域重叠.目的区域靠后一些.
memcpy的实际中都考虑到了.
jiashao606 2006-11-29
  • 打赏
  • 举报
回复
时间紧迫!!
速度 !散分走人
楼主
healer_kx 2006-11-29
  • 打赏
  • 举报
回复
不是到处都能找到吗?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧