aboutsummaryrefslogtreecommitdiff
path: root/src/mem/ft_memccpy.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-11 21:07:32 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-11 21:23:49 +0100
commitc128213daa677d548bfc2905496257fe4a4faf79 (patch)
treed087ceaeff3124ff539bc05d834d79f8187d5628 /src/mem/ft_memccpy.c
parent3c3f1115f6e9a9b914e2dcbd796501ca7ce85342 (diff)
downloadlibft-c128213daa677d548bfc2905496257fe4a4faf79.tar.gz
libft-c128213daa677d548bfc2905496257fe4a4faf79.tar.bz2
libft-c128213daa677d548bfc2905496257fe4a4faf79.zip
ft_mem* and ft_strlen optimization
Diffstat (limited to 'src/mem/ft_memccpy.c')
-rw-r--r--src/mem/ft_memccpy.c56
1 files changed, 46 insertions, 10 deletions
diff --git a/src/mem/ft_memccpy.c b/src/mem/ft_memccpy.c
index 8ce656a..0f52242 100644
--- a/src/mem/ft_memccpy.c
+++ b/src/mem/ft_memccpy.c
@@ -12,20 +12,56 @@
#include "libft.h"
+#define HIMAGIC 0x8080808080808080L
+#define LOMAGIC 0x0101010101010101L
+
void *ft_memccpy(void *dest, const void *src, int c, size_t n)
{
- size_t i;
- t_ftbyte *cast_dest;
- t_ftbyte *cast_src;
+ uint64_t buf;
+ uint64_t lw;
- cast_dest = (t_ftbyte*)dest;
- cast_src = (t_ftbyte*)src;
- i = -1;
- while (++i < n)
+ if (dest == src)
+ return (dest);
+ c = (uint8_t)c;
+ while ((n & 0b111) != 0)
+ {
+ *(uint8_t*)dest = *(uint8_t*)src;
+ if (*(uint8_t*)dest == c)
+ return ((uint8_t*)dest + 1);
+ src++;
+ dest++;
+ n--;
+ }
+ buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16
+ | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40
+ | (uint64_t)c << 48 | (uint64_t)c << 56;
+ while (n > 0)
{
- cast_dest[i] = cast_src[i];
- if (cast_dest[i] == (unsigned char)c)
- return (cast_dest + i + 1);
+ lw = *(uint64_t*)src ^ buf;
+ if ((lw - LOMAGIC) & ~lw & HIMAGIC)
+ {
+ if ( (((uint8_t*)dest)[0] = ((uint8_t*)src)[0]) == (uint8_t)c)
+ return ((uint8_t*)dest + 1);
+ if ( (((uint8_t*)dest)[1] = ((uint8_t*)src)[1]) == (uint8_t)c)
+ return ((uint8_t*)dest + 2);
+ if ( (((uint8_t*)dest)[2] = ((uint8_t*)src)[2]) == (uint8_t)c)
+ return ((uint8_t*)dest + 3);
+ if ( (((uint8_t*)dest)[3] = ((uint8_t*)src)[3]) == (uint8_t)c)
+ return ((uint8_t*)dest + 4);
+ if ( (((uint8_t*)dest)[4] = ((uint8_t*)src)[4]) == (uint8_t)c)
+ return ((uint8_t*)dest + 5);
+ if ( (((uint8_t*)dest)[5] = ((uint8_t*)src)[5]) == (uint8_t)c)
+ return ((uint8_t*)dest + 6);
+ if ( (((uint8_t*)dest)[6] = ((uint8_t*)src)[6]) == (uint8_t)c)
+ return ((uint8_t*)dest + 7);
+ if ( (((uint8_t*)dest)[7] = ((uint8_t*)src)[7]) == (uint8_t)c)
+ return ((uint8_t*)dest + 8);
+ }
+ else
+ *(uint64_t*)dest = *(uint64_t*)src;
+ n -= 8;
+ dest += 8;
+ src += 8;
}
return (NULL);
}