176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman/*
276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman * memset.c
376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman */
476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <string.h>
676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#include <stdint.h>
776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanvoid *memset(void *dst, int c, size_t n)
976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman{
1076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	char *q = dst;
1176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
1276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#if defined(__i386__)
1376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	size_t nl = n >> 2;
1476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	asm volatile ("cld ; rep ; stosl ; movl %3,%0 ; rep ; stosb"
1576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		      : "+c" (nl), "+D" (q)
1676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		      : "a" ((unsigned char)c * 0x01010101U), "r" (n & 3));
1776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#elif defined(__x86_64__)
1876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	size_t nq = n >> 3;
1976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	asm volatile ("cld ; rep ; stosq ; movl %3,%%ecx ; rep ; stosb"
2076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		      :"+c" (nq), "+D" (q)
2176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		      : "a" ((unsigned char)c * 0x0101010101010101U),
2276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman			"r" ((uint32_t) n & 7));
2376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#else
2476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	while (n--) {
2576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		*q++ = c;
2676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	}
2776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#endif
2876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
2976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	return dst;
3076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
31