1570af302Sopenharmony_ci#include <string.h>
2570af302Sopenharmony_ci#include <stdint.h>
3570af302Sopenharmony_ci
4570af302Sopenharmony_civoid *memset(void *dest, int c, size_t n)
5570af302Sopenharmony_ci{
6570af302Sopenharmony_ci	unsigned char *s = dest;
7570af302Sopenharmony_ci	size_t k;
8570af302Sopenharmony_ci
9570af302Sopenharmony_ci	/* Fill head and tail with minimal branching. Each
10570af302Sopenharmony_ci	 * conditional ensures that all the subsequently used
11570af302Sopenharmony_ci	 * offsets are well-defined and in the dest region. */
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci	if (!n) return dest;
14570af302Sopenharmony_ci	s[0] = c;
15570af302Sopenharmony_ci	s[n-1] = c;
16570af302Sopenharmony_ci	if (n <= 2) return dest;
17570af302Sopenharmony_ci	s[1] = c;
18570af302Sopenharmony_ci	s[2] = c;
19570af302Sopenharmony_ci	s[n-2] = c;
20570af302Sopenharmony_ci	s[n-3] = c;
21570af302Sopenharmony_ci	if (n <= 6) return dest;
22570af302Sopenharmony_ci	s[3] = c;
23570af302Sopenharmony_ci	s[n-4] = c;
24570af302Sopenharmony_ci	if (n <= 8) return dest;
25570af302Sopenharmony_ci
26570af302Sopenharmony_ci	/* Advance pointer to align it at a 4-byte boundary,
27570af302Sopenharmony_ci	 * and truncate n to a multiple of 4. The previous code
28570af302Sopenharmony_ci	 * already took care of any head/tail that get cut off
29570af302Sopenharmony_ci	 * by the alignment. */
30570af302Sopenharmony_ci
31570af302Sopenharmony_ci	k = -(uintptr_t)s & 3;
32570af302Sopenharmony_ci	s += k;
33570af302Sopenharmony_ci	n -= k;
34570af302Sopenharmony_ci	n &= -4;
35570af302Sopenharmony_ci
36570af302Sopenharmony_ci#ifdef __GNUC__
37570af302Sopenharmony_ci	typedef uint32_t __attribute__((__may_alias__)) u32;
38570af302Sopenharmony_ci	typedef uint64_t __attribute__((__may_alias__)) u64;
39570af302Sopenharmony_ci
40570af302Sopenharmony_ci	u32 c32 = ((u32)-1)/255 * (unsigned char)c;
41570af302Sopenharmony_ci
42570af302Sopenharmony_ci	/* In preparation to copy 32 bytes at a time, aligned on
43570af302Sopenharmony_ci	 * an 8-byte bounary, fill head/tail up to 28 bytes each.
44570af302Sopenharmony_ci	 * As in the initial byte-based head/tail fill, each
45570af302Sopenharmony_ci	 * conditional below ensures that the subsequent offsets
46570af302Sopenharmony_ci	 * are valid (e.g. !(n<=24) implies n>=28). */
47570af302Sopenharmony_ci
48570af302Sopenharmony_ci	*(u32 *)(s+0) = c32;
49570af302Sopenharmony_ci	*(u32 *)(s+n-4) = c32;
50570af302Sopenharmony_ci	if (n <= 8) return dest;
51570af302Sopenharmony_ci	*(u32 *)(s+4) = c32;
52570af302Sopenharmony_ci	*(u32 *)(s+8) = c32;
53570af302Sopenharmony_ci	*(u32 *)(s+n-12) = c32;
54570af302Sopenharmony_ci	*(u32 *)(s+n-8) = c32;
55570af302Sopenharmony_ci	if (n <= 24) return dest;
56570af302Sopenharmony_ci	*(u32 *)(s+12) = c32;
57570af302Sopenharmony_ci	*(u32 *)(s+16) = c32;
58570af302Sopenharmony_ci	*(u32 *)(s+20) = c32;
59570af302Sopenharmony_ci	*(u32 *)(s+24) = c32;
60570af302Sopenharmony_ci	*(u32 *)(s+n-28) = c32;
61570af302Sopenharmony_ci	*(u32 *)(s+n-24) = c32;
62570af302Sopenharmony_ci	*(u32 *)(s+n-20) = c32;
63570af302Sopenharmony_ci	*(u32 *)(s+n-16) = c32;
64570af302Sopenharmony_ci
65570af302Sopenharmony_ci	/* Align to a multiple of 8 so we can fill 64 bits at a time,
66570af302Sopenharmony_ci	 * and avoid writing the same bytes twice as much as is
67570af302Sopenharmony_ci	 * practical without introducing additional branching. */
68570af302Sopenharmony_ci
69570af302Sopenharmony_ci	k = 24 + ((uintptr_t)s & 4);
70570af302Sopenharmony_ci	s += k;
71570af302Sopenharmony_ci	n -= k;
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci	/* If this loop is reached, 28 tail bytes have already been
74570af302Sopenharmony_ci	 * filled, so any remainder when n drops below 32 can be
75570af302Sopenharmony_ci	 * safely ignored. */
76570af302Sopenharmony_ci
77570af302Sopenharmony_ci	u64 c64 = c32 | ((u64)c32 << 32);
78570af302Sopenharmony_ci	for (; n >= 32; n-=32, s+=32) {
79570af302Sopenharmony_ci		*(u64 *)(s+0) = c64;
80570af302Sopenharmony_ci		*(u64 *)(s+8) = c64;
81570af302Sopenharmony_ci		*(u64 *)(s+16) = c64;
82570af302Sopenharmony_ci		*(u64 *)(s+24) = c64;
83570af302Sopenharmony_ci	}
84570af302Sopenharmony_ci#else
85570af302Sopenharmony_ci	/* Pure C fallback with no aliasing violations. */
86570af302Sopenharmony_ci	for (; n; n--, s++) *s = c;
87570af302Sopenharmony_ci#endif
88570af302Sopenharmony_ci
89570af302Sopenharmony_ci	return dest;
90570af302Sopenharmony_ci}
91