I want to write a memcpy code which does word by word copy instead of byte by byte to increase speed. (Though I need to do some byte by byte copy for the last or few bytes). So I want my source and destination address to be aligned properly. I saw the implementation of memcpy in glibc https://fossies.org/dox/glibc-2.22/string_2memcpy_8c_source.html it does alignment only for destination address. But even if source address is not properly aligned it will result bus error (consider Alignment Checking is enabled in my cpu) I'm not sure how to make both source and destination to be aligned properly. Because if I try to align source by copying few bytes by byte by byte, it will also change the destination address, so at first the destination address which was aligned at first properly might not be aligned properly now. So is there any way to align both?. Please help me.
void memcpy(void *dst, void *src,int size)
{
if(size >= 8)
{
while(size/8) /* code will give sigbus error if src = 0x10003 and dst = 0x100000 */
{
*((double*)dst)++ = *((double*)src)++;
size = size - 8;
}
}
while(size--)
{
*((char*)dst)++ = *((char*)src)++;
}
}