|
From: <sv...@va...> - 2013-01-18 12:51:25
|
sewardj 2013-01-18 12:51:15 +0000 (Fri, 18 Jan 2013)
New Revision: 13240
Log:
Import from memcheck land, a less ludicrously inefficient
implementation of memcpy, which copies words at a time rather than
just bytes.
Modified files:
trunk/helgrind/hg_intercepts.c
Modified: trunk/helgrind/hg_intercepts.c (+58 -30)
===================================================================
--- trunk/helgrind/hg_intercepts.c 2013-01-18 11:01:53 +00:00 (rev 13239)
+++ trunk/helgrind/hg_intercepts.c 2013-01-18 12:51:15 +00:00 (rev 13240)
@@ -2402,39 +2402,67 @@
void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \
( void *dst, const void *src, SizeT len ) \
{ \
- register char *d; \
- register char *s; \
+ const Addr WS = sizeof(UWord); /* 8 or 4 */ \
+ const Addr WM = WS - 1; /* 7 or 3 */ \
\
- if (len == 0) \
- return dst; \
- \
- if ( dst > src ) { \
- d = (char *)dst + len - 1; \
- s = (char *)src + len - 1; \
- while ( len >= 4 ) { \
- *d-- = *s--; \
- *d-- = *s--; \
- *d-- = *s--; \
- *d-- = *s--; \
- len -= 4; \
+ if (len > 0) { \
+ if (dst < src) { \
+ \
+ /* Copying backwards. */ \
+ SizeT n = len; \
+ Addr d = (Addr)dst; \
+ Addr s = (Addr)src; \
+ \
+ if (((s^d) & WM) == 0) { \
+ /* s and d have same UWord alignment. */ \
+ /* Pull up to a UWord boundary. */ \
+ while ((s & WM) != 0 && n >= 1) \
+ { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
+ /* Copy UWords. */ \
+ while (n >= WS) \
+ { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \
+ if (n == 0) \
+ return dst; \
+ } \
+ if (((s|d) & 1) == 0) { \
+ /* Both are 16-aligned; copy what we can thusly. */ \
+ while (n >= 2) \
+ { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \
+ } \
+ /* Copy leftovers, or everything if misaligned. */ \
+ while (n >= 1) \
+ { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
+ \
+ } else if (dst > src) { \
+ \
+ SizeT n = len; \
+ Addr d = ((Addr)dst) + n; \
+ Addr s = ((Addr)src) + n; \
+ \
+ /* Copying forwards. */ \
+ if (((s^d) & WM) == 0) { \
+ /* s and d have same UWord alignment. */ \
+ /* Back down to a UWord boundary. */ \
+ while ((s & WM) != 0 && n >= 1) \
+ { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
+ /* Copy UWords. */ \
+ while (n >= WS) \
+ { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \
+ if (n == 0) \
+ return dst; \
+ } \
+ if (((s|d) & 1) == 0) { \
+ /* Both are 16-aligned; copy what we can thusly. */ \
+ while (n >= 2) \
+ { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \
+ } \
+ /* Copy leftovers, or everything if misaligned. */ \
+ while (n >= 1) \
+ { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
+ \
} \
- while ( len-- ) { \
- *d-- = *s--; \
- } \
- } else if ( dst < src ) { \
- d = (char *)dst; \
- s = (char *)src; \
- while ( len >= 4 ) { \
- *d++ = *s++; \
- *d++ = *s++; \
- *d++ = *s++; \
- *d++ = *s++; \
- len -= 4; \
- } \
- while ( len-- ) { \
- *d++ = *s++; \
- } \
} \
+ \
return dst; \
}
|