aboutsummaryrefslogtreecommitdiff
path: root/src/mem
diff options
context:
space:
mode:
Diffstat (limited to 'src/mem')
-rw-r--r--src/mem/ft_memccpy.c56
-rw-r--r--src/mem/ft_memchr.c44
-rw-r--r--src/mem/ft_memcmp.c46
-rw-r--r--src/mem/ft_memcpy.c7
-rw-r--r--src/mem/ft_memmove.c13
-rw-r--r--src/mem/ft_memset.c32
6 files changed, 148 insertions, 50 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);
}
diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c
index 4fd8689..54780fe 100644
--- a/src/mem/ft_memchr.c
+++ b/src/mem/ft_memchr.c
@@ -12,15 +12,45 @@
#include "libft.h"
+/*
+** Determining if a long word contain byte n
+**
+** xor all bytes with n, then check for zero byte like in ft_strlen.
+*/
+
+#define HIMAGIC 0x8080808080808080L
+#define LOMAGIC 0x0101010101010101L
+
void *ft_memchr(const void *s, int c, size_t n)
{
- size_t i;
- t_ftbyte *cast_s;
+ uint64_t buf;
+ uint64_t lw;
- cast_s = (t_ftbyte*)s;
- i = -1;
- while (++i < n)
- if (cast_s[i] == (unsigned char)c)
- return (cast_s + i);
+ c = (uint8_t)c;
+ while (((uint64_t)s & 0b111) != 0)
+ {
+ n--;
+ if (*(uint8_t*)s == (uint8_t)c)
+ return ((uint8_t*)s);
+ s++;
+ }
+ 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 >= 8)
+ {
+ lw = *(uint64_t*)s ^ buf;
+ if ((lw - LOMAGIC) & ~lw & HIMAGIC)
+ break;
+ n -= 8;
+ s += 8;
+ }
+ while (n > 0)
+ {
+ if (*(uint8_t*)s == (uint8_t)c)
+ return ((uint8_t*)s);
+ n--;
+ s++;
+ }
return (NULL);
}
diff --git a/src/mem/ft_memcmp.c b/src/mem/ft_memcmp.c
index 233d796..c61ca9a 100644
--- a/src/mem/ft_memcmp.c
+++ b/src/mem/ft_memcmp.c
@@ -11,20 +11,42 @@
/* ************************************************************************** */
#include "libft.h"
+#include "libft_mem.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
- size_t i;
- t_ftbyte *cast_s1;
- t_ftbyte *cast_s2;
-
- cast_s1 = (t_ftbyte*)s1;
- cast_s2 = (t_ftbyte*)s2;
- if (n == 0)
- return (0);
- i = -1;
- while (++i < n)
- if (cast_s1[i] != cast_s2[i])
- return (cast_s1[i] - cast_s2[i]);
+ while ((n & 0b111) != 0)
+ {
+ n--;
+ if (*(uint8_t*)s1 != *(uint8_t*)s2)
+ return (*(uint8_t*)s1 - *(uint8_t*)s2);
+ s1++;
+ s2++;
+ }
+ while (n > 0)
+ {
+ if (*(uint64_t*)s1 != *(uint64_t*)s2)
+ {
+ if (((uint8_t*)s1)[0] != ((uint8_t*)s2)[0])
+ return (((uint8_t*)s1)[0] - ((uint8_t*)s2)[0]);
+ if (((uint8_t*)s1)[1] != ((uint8_t*)s2)[1])
+ return (((uint8_t*)s1)[1] - ((uint8_t*)s2)[1]);
+ if (((uint8_t*)s1)[2] != ((uint8_t*)s2)[2])
+ return (((uint8_t*)s1)[2] - ((uint8_t*)s2)[2]);
+ if (((uint8_t*)s1)[3] != ((uint8_t*)s2)[3])
+ return (((uint8_t*)s1)[3] - ((uint8_t*)s2)[3]);
+ if (((uint8_t*)s1)[4] != ((uint8_t*)s2)[4])
+ return (((uint8_t*)s1)[4] - ((uint8_t*)s2)[4]);
+ if (((uint8_t*)s1)[5] != ((uint8_t*)s2)[5])
+ return (((uint8_t*)s1)[5] - ((uint8_t*)s2)[5]);
+ if (((uint8_t*)s1)[6] != ((uint8_t*)s2)[6])
+ return (((uint8_t*)s1)[6] - ((uint8_t*)s2)[6]);
+ if (((uint8_t*)s1)[7] != ((uint8_t*)s2)[7])
+ return (((uint8_t*)s1)[7] - ((uint8_t*)s2)[7]);
+ }
+ n -= 8;
+ s1 += 8;
+ s2 += 8;
+ }
return (0);
}
diff --git a/src/mem/ft_memcpy.c b/src/mem/ft_memcpy.c
index d0ef008..1f84bfd 100644
--- a/src/mem/ft_memcpy.c
+++ b/src/mem/ft_memcpy.c
@@ -11,18 +11,19 @@
/* ************************************************************************** */
#include "libft.h"
+#include "libft_mem.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
- long int *long_dest;
- const long int *long_src;
+ uint64_t *long_dest;
+ const uint64_t *long_src;
if (dest == src)
return (dest);
while (n % 8 > 0)
{
n--;
- ((t_ftbyte*)dest)[n] = ((t_ftbyte*)src)[n];
+ ((uint8_t*)dest)[n] = ((uint8_t*)src)[n];
}
long_dest = dest;
long_src = src;
diff --git a/src/mem/ft_memmove.c b/src/mem/ft_memmove.c
index 2f794fd..142b761 100644
--- a/src/mem/ft_memmove.c
+++ b/src/mem/ft_memmove.c
@@ -11,11 +11,12 @@
/* ************************************************************************** */
#include "libft.h"
+#include "libft_mem.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
- long int *long_dst;
- const long int *long_src;
+ uint64_t *dst64;
+ const uint64_t *src64;
void *dst_copy;
if (dst >= src)
@@ -24,12 +25,12 @@ void *ft_memmove(void *dst, const void *src, size_t len)
while (len % 8 > 0)
{
len--;
- *(t_ftbyte*)dst++ = *(t_ftbyte*)src++;
+ *(uint8_t*)dst++ = *(uint8_t*)src++;
}
- long_dst = dst;
- long_src = src;
+ dst64 = dst;
+ src64 = src;
len /= 8;
while (len-- > 0)
- *long_dst++ = *long_src++;
+ *dst64++ = *src64++;
return (dst_copy);
}
diff --git a/src/mem/ft_memset.c b/src/mem/ft_memset.c
index 89f53ff..ce471a5 100644
--- a/src/mem/ft_memset.c
+++ b/src/mem/ft_memset.c
@@ -11,21 +11,29 @@
/* ************************************************************************** */
#include "libft.h"
+#include "libft_mem.h"
void *ft_memset(void *s, int c, size_t n)
{
- long int buf;
- long int *long_s;
+ uint64_t buf;
+ void *cpy;
- c = (unsigned char)c;
- while (n % 8 > 0)
- *((t_ftbyte*)s + --n) = c;
- buf = (long int)c | (long int)c << 8 | (long int)c << 16
- | (long int)c << 24 | (long int)c << 32 | (long int)c << 40
- | (long int)c << 48 | (long int)c << 56;
- n /= 8;
- long_s = s;
+ cpy = s;
+ c = (uint8_t)c;
+ 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 > 8)
+ {
+ *(uint64_t*)s = buf;
+ n -= 8;
+ s += 8;
+ }
while (n > 0)
- long_s[--n] = buf;
- return (s);
+ {
+ *(uint8_t*)s = c;
+ s++;
+ n--;
+ }
+ return (cpy);
}