string.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of Cunix. */
  3. /*
  4. Cunix 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. Cunix 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 Cunix. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/types.h>
  20. /* copy src to dest for n bytes */
  21. void *memcpy(void *dest, void *src, __uint64_t n) {
  22. __asm__ (
  23. "cld\n\t"
  24. "rep movsb\n\t"
  25. :: "c" (n), "S" (src), "D" (dest)
  26. );
  27. return dest;
  28. }
  29. /* set n to src for n bytes */
  30. void *memset(void *dest, char c, __uint64_t n) {
  31. __asm__ (
  32. "cld\n\t"
  33. "rep stosb\n\t"
  34. :: "c" (n), "D" (dest), "a" (c)
  35. );
  36. return dest;
  37. }
  38. /* set n to src for n words (n must be power of 2) */
  39. void *memsetw(void *dest, __uint16_t c, __uint64_t n) {
  40. __asm__ (
  41. "cld\n\t"
  42. "rep stosw\n\t"
  43. :: "c" (n >> 1), "D" (dest), "a" (c)
  44. );
  45. return dest;
  46. }
  47. /* set n to src for n dwords (n must be power of 4) */
  48. void *memsetl(void *dest, __uint32_t c, __uint64_t n) {
  49. __asm__ (
  50. "cld\n\t"
  51. "rep stosl\n\t"
  52. :: "c" (n >> 2), "D" (dest), "a" (c)
  53. );
  54. return dest;
  55. }
  56. /* set n to src for n quads (n must be power of 8) */
  57. void *memsetq(void *dest, __uint64_t c, __uint64_t n) {
  58. __asm__ (
  59. "cld\n\t"
  60. "rep stosq\n\t"
  61. :: "c" (n >> 3), "D" (dest), "a" (c)
  62. );
  63. return dest;
  64. }