37 lines
687 B
C
37 lines
687 B
C
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
void *ft_memset32(void *s, int c, size_t n)
|
|
{
|
|
unsigned char *ptr = (unsigned char *)s;
|
|
uint32_t pattern;
|
|
uint32_t *long_ptr;
|
|
|
|
unsigned char byte = (unsigned char)c;
|
|
|
|
while (n > 0 && ((uintptr_t)ptr % 4 != 0))
|
|
{
|
|
*ptr++ = byte;
|
|
n--;
|
|
}
|
|
|
|
if (n >= 4)
|
|
{
|
|
pattern = byte;
|
|
pattern |= pattern << 8;
|
|
pattern |= pattern << 16;
|
|
|
|
long_ptr = (uint32_t *)ptr;
|
|
while (n >= 4)
|
|
{
|
|
*long_ptr++ = pattern;
|
|
n -= 4;
|
|
}
|
|
ptr = (unsigned char *)long_ptr;
|
|
}
|
|
|
|
while (n--)
|
|
*ptr++ = byte;
|
|
|
|
return s;
|
|
}
|