string.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #ifndef INCLUDED_STRING_H
  20. #define INCLUDED_STRING_H
  21. /* copy src to dest for n bytes */
  22. #define memcpy(dest, src, n) ({ \
  23. __asm__ ( \
  24. "cld\n\t" \
  25. "rep movsb\n\t" \
  26. :: "c" (n), "S" (src), "D" (dest) \
  27. ); \
  28. dest; \
  29. })
  30. /* set n to src for n bytes */
  31. #define memset(dest, c, n) ({ \
  32. __asm__ ( \
  33. "cld\n\t" \
  34. "rep stosb\n\t" \
  35. :: "c" (n), "D" (dest), "a" (c) \
  36. ); \
  37. dest; \
  38. })
  39. /* set n to src for n words (n must be power of 2) */
  40. #define memsetw(dest, c, n) ({ \
  41. __asm__ ( \
  42. "cld\n\t" \
  43. "rep stosw\n\t" \
  44. :: "c" (n >> 1), "D" (dest), "a" (c) \
  45. ); \
  46. dest; \
  47. })
  48. /* set n to src for n dwords (n must be power of 4) */
  49. #define memsetl(dest, c, n) ({ \
  50. __asm__ ( \
  51. "cld\n\t" \
  52. "rep stosl\n\t" \
  53. :: "c" (n >> 2), "D" (dest), "a" (c) \
  54. ); \
  55. dest; \
  56. })
  57. /* set n to src for n quads (n must be power of 8) */
  58. #define memsetq(dest, c, n) ({ \
  59. __asm__ ( \
  60. "cld\n\t" \
  61. "rep stosq\n\t" \
  62. :: "c" (n >> 3), "D" (dest), "a" (c) \
  63. ); \
  64. dest; \
  65. })
  66. #endif