string.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* arch/x86/include/arch/string.h
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #ifndef __ARCH_STRING_H
  21. #define __ARCH_STRING_H
  22. #ifndef __ASSEMBLER__
  23. /* well... but in kernel, these are only required. */
  24. #define memset(dest, v, n) ({ \
  25. __asm__ volatile ( \
  26. "cld\n\t" \
  27. "rep\n\t" \
  28. "stosb\n\t" \
  29. :: "c" (n), "a" (v), "D" (dest) \
  30. ); \
  31. })
  32. #define memcpy(dest, src, n) ({ \
  33. __asm__ volatile ( \
  34. "cld\n\t" \
  35. "rep\n\t" \
  36. "movsb\n\t" \
  37. :: "c" (n), "S" (src), "D" (dest) \
  38. ); \
  39. })
  40. #define strcpy(dest, src) ({ \
  41. __asm__ volatile ( \
  42. "cld\n\t" \
  43. "1:\n\t" \
  44. "lodsb\n\t" \
  45. "stosb\n\t" \
  46. "testb %%al, %%al\n\t" \
  47. "jnz 1b\n\t" \
  48. : "D" (dest), "S" (src) \
  49. :: "ax" \
  50. ); \
  51. })
  52. #endif
  53. #endif