Skip to content

Commit

Permalink
Faster memcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdev1 committed Feb 11, 2025
1 parent 1006a67 commit 4af2c4e
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions libc/src/string/memcpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@
/**
*
*/
void* memcpy(void* dest, const void* src, size_t num) {
void* memcpy(void* dest, const void* src, size_t num)
{

uint8_t* src_8 = (uint8_t*) src;
uint8_t* dest_8 = (uint8_t*) dest;
while (num--) {
*dest_8++ = *src_8++;
uint8_t* targetPtr = (uint8_t*) dest;
const uint8_t* sourcePtr = (const uint8_t*) src;

while(num >= 4)
{
*(uint32_t*) targetPtr = *(const uint32_t*) sourcePtr;
targetPtr += 4;
sourcePtr += 4;
num -= 4;
}

while(num >= 2)
{
*(uint16_t*) targetPtr = *(const uint16_t*) sourcePtr;
targetPtr += 2;
sourcePtr += 2;
num -= 2;
}

while(num--)
*targetPtr++ = *sourcePtr++;

return dest;
}

0 comments on commit 4af2c4e

Please sign in to comment.