PatchworkOS
Loading...
Searching...
No Matches
memset.c
Go to the documentation of this file.
1#include <stdint.h>
2#include <string.h>
3
4void* memset(void* s, int c, size_t n)
5{
6 uint8_t* p = s;
7
8 uint8_t ch8 = (uint8_t)c;
9 uint64_t ch64 = ((uint64_t)ch8) | ((uint64_t)ch8 << 8) | ((uint64_t)ch8 << 16) | ((uint64_t)ch8 << 24) |
10 ((uint64_t)ch8 << 32) | ((uint64_t)ch8 << 40) | ((uint64_t)ch8 << 48) | ((uint64_t)ch8 << 56);
11
12 while (((uintptr_t)p & 7) && n)
13 {
14 *p++ = ch8;
15 n--;
16 }
17
18 while (n >= 64)
19 {
20 *(uint64_t*)(p + 0) = ch64;
21 *(uint64_t*)(p + 8) = ch64;
22 *(uint64_t*)(p + 16) = ch64;
23 *(uint64_t*)(p + 24) = ch64;
24 *(uint64_t*)(p + 32) = ch64;
25 *(uint64_t*)(p + 40) = ch64;
26 *(uint64_t*)(p + 48) = ch64;
27 *(uint64_t*)(p + 56) = ch64;
28 p += 64;
29 n -= 64;
30 }
31
32 while (n >= 8)
33 {
34 *(uint64_t*)p = ch64;
35 p += 8;
36 n -= 8;
37 }
38
39 while (n--)
40 {
41 *p++ = ch8;
42 }
43
44 return s;
45}
void * memset(void *s, int c, size_t n)
Definition memset.c:4
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43