string.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <libc/sys/types.h>
  3. void *memset(void *dst, unsigned char C, uint64_t size)
  4. {
  5. int d0, d1;
  6. unsigned long tmp = C * 0x0101010101010101UL;
  7. __asm__ __volatile__("cld \n\t"
  8. "rep \n\t"
  9. "stosq \n\t"
  10. "testb $4, %b3 \n\t"
  11. "je 1f \n\t"
  12. "stosl \n\t"
  13. "1:\ttestb $2, %b3 \n\t"
  14. "je 2f\n\t"
  15. "stosw \n\t"
  16. "2:\ttestb $1, %b3 \n\t"
  17. "je 3f \n\t"
  18. "stosb \n\t"
  19. "3: \n\t"
  20. : "=&c"(d0), "=&D"(d1)
  21. : "a"(tmp), "q"(size), "0"(size / 8), "1"(dst)
  22. : "memory");
  23. return dst;
  24. }
  25. size_t strlen(const char *s)
  26. {
  27. register size_t __res = 0;
  28. while (s[__res] != '\0')
  29. {
  30. ++__res;
  31. }
  32. return __res;
  33. }
  34. /*
  35. 比较字符串 FirstPart and SecondPart
  36. FirstPart = SecondPart => 0
  37. FirstPart > SecondPart => 1
  38. FirstPart < SecondPart => -1
  39. */
  40. int strcmp(const char *FirstPart, const char *SecondPart)
  41. {
  42. register int __res;
  43. __asm__ __volatile__("cld \n\t"
  44. "1: \n\t"
  45. "lodsb \n\t"
  46. "scasb \n\t"
  47. "jne 2f \n\t"
  48. "testb %%al, %%al \n\t"
  49. "jne 1b \n\t"
  50. "xorl %%eax, %%eax \n\t"
  51. "jmp 3f \n\t"
  52. "2: \n\t"
  53. "movl $1, %%eax \n\t"
  54. "jl 3f \n\t"
  55. "negl %%eax \n\t"
  56. "3: \n\t"
  57. : "=a"(__res)
  58. : "D"(FirstPart), "S"(SecondPart)
  59. :);
  60. return __res;
  61. }