memset.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2013 Regents of the University of California
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/linkage.h>
  14. #include <asm/asm.h>
  15. /* void *memset(void *, int, size_t) */
  16. ENTRY(memset)
  17. move t0, a0 /* Preserve return value */
  18. /* Defer to byte-oriented fill for small sizes */
  19. sltiu a3, a2, 16
  20. bnez a3, 4f
  21. /*
  22. * Round to nearest XLEN-aligned address
  23. * greater than or equal to start address
  24. */
  25. addi a3, t0, SZREG-1
  26. andi a3, a3, ~(SZREG-1)
  27. beq a3, t0, 2f /* Skip if already aligned */
  28. /* Handle initial misalignment */
  29. sub a4, a3, t0
  30. 1:
  31. sb a1, 0(t0)
  32. addi t0, t0, 1
  33. bltu t0, a3, 1b
  34. sub a2, a2, a4 /* Update count */
  35. 2: /* Duff's device with 32 XLEN stores per iteration */
  36. /* Broadcast value into all bytes */
  37. andi a1, a1, 0xff
  38. slli a3, a1, 8
  39. or a1, a3, a1
  40. slli a3, a1, 16
  41. or a1, a3, a1
  42. #ifdef CONFIG_64BIT
  43. slli a3, a1, 32
  44. or a1, a3, a1
  45. #endif
  46. /* Calculate end address */
  47. andi a4, a2, ~(SZREG-1)
  48. add a3, t0, a4
  49. andi a4, a4, 31*SZREG /* Calculate remainder */
  50. beqz a4, 3f /* Shortcut if no remainder */
  51. neg a4, a4
  52. addi a4, a4, 32*SZREG /* Calculate initial offset */
  53. /* Adjust start address with offset */
  54. sub t0, t0, a4
  55. /* Jump into loop body */
  56. /* Assumes 32-bit instruction lengths */
  57. la a5, 3f
  58. #ifdef CONFIG_64BIT
  59. srli a4, a4, 1
  60. #endif
  61. add a5, a5, a4
  62. jr a5
  63. 3:
  64. REG_S a1, 0(t0)
  65. REG_S a1, SZREG(t0)
  66. REG_S a1, 2*SZREG(t0)
  67. REG_S a1, 3*SZREG(t0)
  68. REG_S a1, 4*SZREG(t0)
  69. REG_S a1, 5*SZREG(t0)
  70. REG_S a1, 6*SZREG(t0)
  71. REG_S a1, 7*SZREG(t0)
  72. REG_S a1, 8*SZREG(t0)
  73. REG_S a1, 9*SZREG(t0)
  74. REG_S a1, 10*SZREG(t0)
  75. REG_S a1, 11*SZREG(t0)
  76. REG_S a1, 12*SZREG(t0)
  77. REG_S a1, 13*SZREG(t0)
  78. REG_S a1, 14*SZREG(t0)
  79. REG_S a1, 15*SZREG(t0)
  80. REG_S a1, 16*SZREG(t0)
  81. REG_S a1, 17*SZREG(t0)
  82. REG_S a1, 18*SZREG(t0)
  83. REG_S a1, 19*SZREG(t0)
  84. REG_S a1, 20*SZREG(t0)
  85. REG_S a1, 21*SZREG(t0)
  86. REG_S a1, 22*SZREG(t0)
  87. REG_S a1, 23*SZREG(t0)
  88. REG_S a1, 24*SZREG(t0)
  89. REG_S a1, 25*SZREG(t0)
  90. REG_S a1, 26*SZREG(t0)
  91. REG_S a1, 27*SZREG(t0)
  92. REG_S a1, 28*SZREG(t0)
  93. REG_S a1, 29*SZREG(t0)
  94. REG_S a1, 30*SZREG(t0)
  95. REG_S a1, 31*SZREG(t0)
  96. addi t0, t0, 32*SZREG
  97. bltu t0, a3, 3b
  98. andi a2, a2, SZREG-1 /* Update count */
  99. 4:
  100. /* Handle trailing misalignment */
  101. beqz a2, 6f
  102. add a3, t0, a2
  103. 5:
  104. sb a1, 0(t0)
  105. addi t0, t0, 1
  106. bltu t0, a3, 5b
  107. 6:
  108. ret
  109. END(memset)