Skip to content

Instantly share code, notes, and snippets.

@M0nteCarl0
Created November 18, 2024 11:32
Show Gist options
  • Save M0nteCarl0/833bd5b7f87569c280acf47b4aa788a6 to your computer and use it in GitHub Desktop.
Save M0nteCarl0/833bd5b7f87569c280acf47b4aa788a6 to your computer and use it in GitHub Desktop.
memcpy_implenations
void *fast_memcpy(void *dest, const void *src, size_t n) {
size_t *d = (size_t *)dest;
const size_t *s = (const size_t *)src;
size_t block_size = sizeof(size_t);
while (n >= block_size) {
*d++ = *s++;
n -= block_size;
}
char *d_bytes = (char *)d;
const char *s_bytes = (const char *)s;
while (n > 0) {
*d_bytes++ = *s_bytes++;
n--;
}
return dest;
}
void *naive_memcpy(void *dest, const void *src, size_t n) {
char *d = (char *)dest;
const char *s = (const char *)src;
for (size_t i = 0; i < n; i++) {
*d++ = *s++;
}
return dest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment