-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_memcpy.c
57 lines (54 loc) · 1.91 KB
/
ft_memcpy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yuotsuka <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/18 21:03:04 by yuotsuka #+# #+# */
/* Updated: 2024/11/06 16:53:11 by yuotsuka ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void copy_aligned32(const void *src, void *dst, size_t n)
{
{
asm ("1:\n\t"
"vmovdqa (%[src]), %%ymm0 \n\t"
"vmovdqa %%ymm0, (%[dst]) \n\t"
"addq $32, %[src] \n\t"
"addq $32, %[dst] \n\t"
"subq $32, %[n] \n\t"
"jnz 1b \n\t"
: [src] "+r" (src), [dst] "+r" (dst), [n] "+r" (n)
:
: "memory", "ymm0");
}
}
void *ft_memcpy(void *dst, const void *src, size_t n)
{
const unsigned char *(src_ptr) = (const unsigned char *) src;
unsigned char *(dst_ptr) = (unsigned char *)dst;
size_t (src_al) = ((size_t)src_ptr) & 0x1f;
size_t (dst_al) = ((size_t)dst_ptr) & 0x1f;
if (!dst && !src)
return (NULL);
while ((src_al && n) || (dst_al && n))
{
*dst_ptr++ = *src_ptr++;
src_al = (size_t)src_ptr & 0x1f;
dst_al = (size_t)dst_ptr & 0x1f;
--n;
}
if (n >= 32)
{
size_t (aligned_blocks) = n & ~(size_t)0x1f;
copy_aligned32(src_ptr, dst_ptr, aligned_blocks);
src_ptr += aligned_blocks;
dst_ptr += aligned_blocks;
n -= aligned_blocks;
}
while (n--)
*dst_ptr++ = *src_ptr++;
return (dst);
}