From c128213daa677d548bfc2905496257fe4a4faf79 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 11 Mar 2020 21:07:32 +0100 Subject: ft_mem* and ft_strlen optimization --- src/mem/ft_memccpy.c | 56 ++++++++++++++++++++++++++++++++++++++++++---------- src/mem/ft_memchr.c | 44 ++++++++++++++++++++++++++++++++++------- src/mem/ft_memcmp.c | 46 +++++++++++++++++++++++++++++++----------- src/mem/ft_memcpy.c | 7 ++++--- src/mem/ft_memmove.c | 13 ++++++------ src/mem/ft_memset.c | 32 +++++++++++++++++++----------- 6 files changed, 148 insertions(+), 50 deletions(-) (limited to 'src/mem') 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); } -- cgit From b9f000a80cbba38b8f21c9737a42f07573ec7b91 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 11 May 2020 16:14:38 +0200 Subject: Moved util/ft_split* in str, Added ft_memjoin and ft_memjoinf1, Modified ft_getfile so that it can read non-ascii file --- src/mem/ft_memjoin.c | 24 ++++++++++++++++++++++++ src/mem/ft_memjoinf1.c | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/mem/ft_memjoin.c create mode 100644 src/mem/ft_memjoinf1.c (limited to 'src/mem') diff --git a/src/mem/ft_memjoin.c b/src/mem/ft_memjoin.c new file mode 100644 index 0000000..a31a812 --- /dev/null +++ b/src/mem/ft_memjoin.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 15:09:47 by charles #+# #+# */ +/* Updated: 2020/05/11 15:14:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +void *ft_memjoin(void *m1, size_t m1_size, void *m2, size_t m2_size) +{ + void *joined; + + if ((joined = malloc(m1_size + m2_size)) == NULL) + return (NULL); + ft_memcpy(joined, m1, m1_size); + ft_memcpy(joined + m1_size, m2, m2_size); + return (joined); +} diff --git a/src/mem/ft_memjoinf1.c b/src/mem/ft_memjoinf1.c new file mode 100644 index 0000000..8b4db92 --- /dev/null +++ b/src/mem/ft_memjoinf1.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memjoinf1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 15:18:57 by charles #+# #+# */ +/* Updated: 2020/05/11 15:20:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +void *ft_memjoinf1(void *m1, size_t m1_size, void *m2, size_t m2_size) +{ + void *joined; + + joined = ft_memjoin(m1, m1_size, m2, m2_size); + free(m1); + return (joined); +} -- cgit From d4aeabeac0a8bd1665b14aacf487df17198c1ab9 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 21:40:41 +0200 Subject: Fixing/refactoring ft_getline, fixed/normed ft_memchr, added ft_strmove --- src/mem/ft_memchr.c | 23 +++++++++-------------- src/mem/ft_memcpy.c | 3 +-- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'src/mem') diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c index e0266af..848b436 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2020/04/01 21:50:49 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:39:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,16 +27,15 @@ void *ft_memchr(const void *s, int c, size_t n) uint64_t lw; c = (uint8_t)c; - while (((uint64_t)s & 0b111) != 0) + while (((uint64_t)s & 0b111) != 0 && n > 0) { - n--; if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); + return ((void*)s); + n--; 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; + buf = c | c << 8 | c << 16 | c << 24; + buf |= buf << 32; while (n >= 8) { lw = *(uint64_t*)s ^ buf; @@ -45,12 +44,8 @@ void *ft_memchr(const void *s, int c, size_t n) n -= 8; s += 8; } - while (n > 0) - { - if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); - n--; - s++; - } + while (n-- > 0) + if (*(uint8_t*)s++ == (uint8_t)c) + return ((void*)(s - 1)); return (NULL); } diff --git a/src/mem/ft_memcpy.c b/src/mem/ft_memcpy.c index 1f84bfd..699111f 100644 --- a/src/mem/ft_memcpy.c +++ b/src/mem/ft_memcpy.c @@ -6,11 +6,10 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:00:07 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:39:04 by cacharle ### ########.fr */ +/* Updated: 2020/05/12 20:49:35 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_mem.h" void *ft_memcpy(void *dest, const void *src, size_t n) -- cgit