From 8025e6b92a09fcf584c13fea3f04f2a0be9cbe64 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 Dec 2019 11:05:33 +0100 Subject: add memcpy --- strings.c | 16 ++++++++++++++-- strings.h | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/strings.c b/strings.c index 4164467..94cb6ac 100644 --- a/strings.c +++ b/strings.c @@ -1,5 +1,3 @@ -#include - #include "strings.h" void uint32_to_dec(uint32_t number, char buf[10]) @@ -79,3 +77,17 @@ size_t strlen(char string[]) return len; } + +void memcpy(void *dst, void *src, size_t nbytes) +{ + size_t iter; + + // copying by word is faster than by byte + // copy by word as much as possible + for (iter = 0; iter < nbytes / 4; iter++) + ((volatile uint32_t*) dst)[iter] = ((uint32_t*) src)[iter]; + + // copy the remaining 1, 2 or 3 bytes by byte + for (iter *= 4; iter < nbytes; iter++) + ((volatile uint8_t*) dst)[iter] = ((uint8_t*) src)[iter]; +} diff --git a/strings.h b/strings.h index 7e772d3..1eb291c 100644 --- a/strings.h +++ b/strings.h @@ -1,6 +1,7 @@ #ifndef STRINGS_H #define STRINGS_H +#include #include void uint32_to_dec(uint32_t number, char buf[10]); @@ -23,4 +24,6 @@ void uint32_to_hexstringt(uint32_t number, char buf[9]); size_t strlen(char string[]); +void memcpy(void *dst, void *src, size_t nbytes); + #endif // STRINGS_H -- cgit v1.2.3