c - Aligning both source and destination address in memcpy -
i want write memcpy code word word copy instead of byte byte increase speed. (though need byte byte copy last or few bytes). want source , destination address aligned properly. saw implementation of memcpy in glibc https://fossies.org/dox/glibc-2.22/string_2memcpy_8c_source.html alignment destination address. if source address not aligned result bus error (consider alignment checking enabled in cpu) i'm not sure how make both source , destination aligned properly. because if try align source copying few bytes byte byte, change destination address, @ first destination address aligned @ first might not aligned now. there way align both?. please me.
void memcpy(void *dst, void *src,int size) { if(size >= 8) { while(size/8) /* code give sigbus error if src = 0x10003 , dst = 0x100000 */ { *((double*)dst)++ = *((double*)src)++; size = size - 8; } } while(size--) { *((char*)dst)++ = *((char*)src)++; } }
...so @ first destination address aligned @ first might not aligned now. there way align both?
i found article on memcpy optimization believe discusses trying in length... (see link below code example)
modified-gnu algorithm:
void * memcpy(void * dst, void const * src, size_t len) { long * pldst = (long *) dst; long const * plsrc = (long const *) src; if (!(src & 0xfffffffc) && !(dst & 0xfffffffc)) { while (len >= 4) { *pldst++ = *plsrc++; len -= 4; } } char * pcdst = (char *) pldst; char const * pcdst = (char const *) plsrc; while (len--) { *pcdst++ = *pcsrc++; } return (dst); }
this variation of modified-gnu algorithm uses computation adjust address misalignment.
Comments
Post a Comment