memset.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * memset.S
  3. *
  4. * Copyright(c) 2007-2022 Jianjun Jiang <8192542@qq.com>
  5. * Official site: http://xboot.org
  6. * Mobile phone: +86-18665388956
  7. * QQ: 8192542
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. */
  28. #include <linkage.h>
  29. .global memset
  30. .type memset, %function
  31. .align 3
  32. memset:
  33. move t0, a0
  34. sltiu a3, a2, 16
  35. bnez a3, 4f
  36. addi a3, t0, REGSZ - 1
  37. andi a3, a3, ~(REGSZ - 1)
  38. beq a3, t0, 2f
  39. sub a4, a3, t0
  40. 1: sb a1, 0(t0)
  41. addi t0, t0, 1
  42. bltu t0, a3, 1b
  43. sub a2, a2, a4
  44. 2: andi a1, a1, 0xff
  45. slli a3, a1, 8
  46. or a1, a3, a1
  47. slli a3, a1, 16
  48. or a1, a3, a1
  49. #if __riscv_xlen == 64
  50. slli a3, a1, 32
  51. or a1, a3, a1
  52. #endif
  53. andi a4, a2, ~(REGSZ - 1)
  54. add a3, t0, a4
  55. andi a4, a4, 31 * REGSZ
  56. beqz a4, 3f
  57. neg a4, a4
  58. addi a4, a4, 32 * REGSZ
  59. sub t0, t0, a4
  60. la a5, 3f
  61. #if __riscv_xlen == 64
  62. srli a4, a4, 1
  63. #endif
  64. add a5, a5, a4
  65. jr a5
  66. 3: SREG a1, 0(t0)
  67. SREG a1, REGSZ(t0)
  68. SREG a1, 2 * REGSZ(t0)
  69. SREG a1, 3 * REGSZ(t0)
  70. SREG a1, 4 * REGSZ(t0)
  71. SREG a1, 5 * REGSZ(t0)
  72. SREG a1, 6 * REGSZ(t0)
  73. SREG a1, 7 * REGSZ(t0)
  74. SREG a1, 8 * REGSZ(t0)
  75. SREG a1, 9 * REGSZ(t0)
  76. SREG a1, 10 * REGSZ(t0)
  77. SREG a1, 11 * REGSZ(t0)
  78. SREG a1, 12 * REGSZ(t0)
  79. SREG a1, 13 * REGSZ(t0)
  80. SREG a1, 14 * REGSZ(t0)
  81. SREG a1, 15 * REGSZ(t0)
  82. SREG a1, 16 * REGSZ(t0)
  83. SREG a1, 17 * REGSZ(t0)
  84. SREG a1, 18 * REGSZ(t0)
  85. SREG a1, 19 * REGSZ(t0)
  86. SREG a1, 20 * REGSZ(t0)
  87. SREG a1, 21 * REGSZ(t0)
  88. SREG a1, 22 * REGSZ(t0)
  89. SREG a1, 23 * REGSZ(t0)
  90. SREG a1, 24 * REGSZ(t0)
  91. SREG a1, 25 * REGSZ(t0)
  92. SREG a1, 26 * REGSZ(t0)
  93. SREG a1, 27 * REGSZ(t0)
  94. SREG a1, 28 * REGSZ(t0)
  95. SREG a1, 29 * REGSZ(t0)
  96. SREG a1, 30 * REGSZ(t0)
  97. SREG a1, 31 * REGSZ(t0)
  98. addi t0, t0, 32 * REGSZ
  99. bltu t0, a3, 3b
  100. andi a2, a2, REGSZ - 1
  101. 4: beqz a2, 6f
  102. add a3, t0, a2
  103. 5: sb a1, 0(t0)
  104. addi t0, t0, 1
  105. bltu t0, a3, 5b
  106. 6: ret