1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #pragma once
- #include <libc/sys/types.h>
- void *memset(void *dst, unsigned char C, uint64_t size)
- {
- int d0, d1;
- unsigned long tmp = C * 0x0101010101010101UL;
- __asm__ __volatile__("cld \n\t"
- "rep \n\t"
- "stosq \n\t"
- "testb $4, %b3 \n\t"
- "je 1f \n\t"
- "stosl \n\t"
- "1:\ttestb $2, %b3 \n\t"
- "je 2f\n\t"
- "stosw \n\t"
- "2:\ttestb $1, %b3 \n\t"
- "je 3f \n\t"
- "stosb \n\t"
- "3: \n\t"
- : "=&c"(d0), "=&D"(d1)
- : "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
- : "memory");
- return dst;
- }
- size_t strlen(const char *s)
- {
- register size_t __res = 0;
- while (s[__res] != '\0')
- {
- ++__res;
- }
- return __res;
- }
- /*
- 比较字符串 FirstPart and SecondPart
- FirstPart = SecondPart => 0
- FirstPart > SecondPart => 1
- FirstPart < SecondPart => -1
- */
- int strcmp(const char *FirstPart, const char *SecondPart)
- {
- register int __res;
- __asm__ __volatile__("cld \n\t"
- "1: \n\t"
- "lodsb \n\t"
- "scasb \n\t"
- "jne 2f \n\t"
- "testb %%al, %%al \n\t"
- "jne 1b \n\t"
- "xorl %%eax, %%eax \n\t"
- "jmp 3f \n\t"
- "2: \n\t"
- "movl $1, %%eax \n\t"
- "jl 3f \n\t"
- "negl %%eax \n\t"
- "3: \n\t"
- : "=a"(__res)
- : "D"(FirstPart), "S"(SecondPart)
- :);
- return __res;
- }
|