You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
/* Copyright (C) 2021,2022 fef <owo@fef.moe>. All rights reserved. */
|
|
|
|
|
|
|
|
#include <asm/common.h>
|
|
|
|
|
|
|
|
/* void *memset(void *dest, int c, usize n) */
|
|
|
|
ENTRY(memset)
|
|
|
|
pushl %ebp
|
|
|
|
movl %esp, %ebp
|
|
|
|
|
|
|
|
cld
|
|
|
|
|
|
|
|
movl 8(%ebp), %edi /* edi = dest */
|
|
|
|
movl 12(%ebp), %eax /* eax = c */
|
|
|
|
movl 16(%ebp), %ecx /* ecx = n */
|
|
|
|
|
|
|
|
cmpl $16, %ecx
|
|
|
|
jb out /* n < 16, not worth the effort */
|
|
|
|
|
|
|
|
testl $1, %edi
|
|
|
|
jz 2f
|
|
|
|
stosb
|
|
|
|
decl %ecx
|
|
|
|
|
|
|
|
/* 2-byte aligned */
|
|
|
|
2: movl %eax, %edx
|
|
|
|
shll $8, %eax
|
|
|
|
orl %edx, %eax /* c |= (c << 8) */
|
|
|
|
|
|
|
|
testl $2, %edi
|
|
|
|
jz 4f
|
|
|
|
stosw
|
|
|
|
subl $2, %ecx /* n -= 2 */
|
|
|
|
|
|
|
|
/* 4-byte aligned, now we can fire stosl */
|
|
|
|
4: movl %eax, %edx
|
|
|
|
shll $16, %eax
|
|
|
|
orl %edx, %eax /* c |= (c << 16) */
|
|
|
|
movl %ecx, %edx
|
|
|
|
andl $7, %edx /* edx = n % 8 */
|
|
|
|
shrl $2, %ecx /* n /= 4 */
|
|
|
|
rep
|
|
|
|
stosl
|
|
|
|
movl %edx, %ecx
|
|
|
|
|
|
|
|
/* write out remaining bytes (or do the whole memset, if n < 16) */
|
|
|
|
out: rep
|
|
|
|
stosb
|
|
|
|
movl 8(%ebp), %eax /* return original pointer to dest */
|
|
|
|
popl %ebp
|
|
|
|
ret
|
|
|
|
END(memset)
|