libft/srcs/ft_memset64.c

38 lines
595 B
C

#include <stddef.h>
#include <stdint.h>
void *ft_memset64(void *s, int c, size_t n)
{
unsigned char *ptr = (unsigned char *)s;
uint64_t pattern;
uint64_t *long_ptr;
unsigned char byte = (unsigned char)c;
while (n > 0 && ((uintptr_t)ptr % 8 != 0))
{
*ptr++ = byte;
n--;
}
if (n >= 8)
{
pattern = byte;
pattern |= pattern << 8;
pattern |= pattern << 16;
pattern |= pattern << 32;
long_ptr = (uint64_t *)ptr;
while (n >= 8)
{
*long_ptr++ = pattern;
n -= 8;
}
ptr = (unsigned char *)long_ptr;
}
while (n--)
*ptr++ = byte;
return s;
}