memset.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Hartvig Ekner <hartvige@mips.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <features.h>
  16. #include <sysdep.h>
  17. #include <endian.h>
  18. /* void *memset(void *s, int c, size_t n). */
  19. #ifdef __mips64
  20. #include <sys/asm.h>
  21. #if __BYTE_ORDER == __BIG_ENDIAN
  22. # define SDHI sdl /* high part is left in big-endian */
  23. #else
  24. # define SDHI sdr /* high part is right in little-endian */
  25. #endif
  26. ENTRY (memset)
  27. .set noreorder
  28. slti ta1, a2, 16 # Less than 16?
  29. bne ta1, zero, L(last16)
  30. move v0, a0 # Setup exit value before too late
  31. beq a1, zero, L(ueven) # If zero pattern, no need to extend
  32. andi a1, 0xff # Avoid problems with bogus arguments
  33. dsll ta0, a1, 8
  34. or a1, ta0
  35. dsll ta0, a1, 16
  36. or a1, ta0 # a1 is now pattern in full word
  37. dsll ta0, a1, 32
  38. or a1, ta0 # a1 is now pattern in double word
  39. L(ueven):
  40. PTR_SUBU ta0, zero, a0 # Unaligned address?
  41. andi ta0, 0x7
  42. beq ta0, zero, L(chkw)
  43. PTR_SUBU a2, ta0
  44. SDHI a1, 0(a0) # Yes, handle first unaligned part
  45. PTR_ADDU a0, ta0 # Now both a0 and a2 are updated
  46. L(chkw):
  47. andi ta0, a2, 0xf # Enough left for one loop iteration?
  48. beq ta0, a2, L(chkl)
  49. PTR_SUBU a3, a2, ta0
  50. PTR_ADDU a3, a0 # a3 is last loop address +1
  51. move a2, ta0 # a2 is now # of bytes left after loop
  52. L(loopw):
  53. PTR_ADDIU a0, 16 # Handle 2 dwords pr. iteration
  54. sd a1, -16(a0)
  55. bne a0, a3, L(loopw)
  56. sd a1, -8(a0)
  57. L(chkl):
  58. andi ta0, a2, 0x8 # Check if there is at least a double
  59. beq ta0, zero, L(last16) # word remaining after the loop
  60. PTR_SUBU a2, ta0
  61. sd a1, 0(a0) # Yes...
  62. PTR_ADDIU a0, 8
  63. L(last16):
  64. blez a2, L(exit) # Handle last 16 bytes (if cnt>0)
  65. PTR_ADDU a3, a2, a0 # a3 is last address +1
  66. L(lst16l):
  67. PTR_ADDIU a0, 1
  68. bne a0, a3, L(lst16l)
  69. sb a1, -1(a0)
  70. L(exit):
  71. j ra # Bye, bye
  72. nop
  73. .set reorder
  74. END (memset)
  75. #else /* !__mips64 */
  76. #if __BYTE_ORDER == __BIG_ENDIAN
  77. # define SWHI swl /* high part is left in big-endian */
  78. #else
  79. # define SWHI swr /* high part is right in little-endian */
  80. #endif
  81. ENTRY (memset)
  82. .set noreorder
  83. slti t1, a2, 8 # Less than 8?
  84. bne t1, zero, L(last8)
  85. move v0, a0 # Setup exit value before too late
  86. beq a1, zero, L(ueven) # If zero pattern, no need to extend
  87. andi a1, 0xff # Avoid problems with bogus arguments
  88. sll t0, a1, 8
  89. or a1, t0
  90. sll t0, a1, 16
  91. or a1, t0 # a1 is now pattern in full word
  92. L(ueven):
  93. subu t0, zero, a0 # Unaligned address?
  94. andi t0, 0x3
  95. beq t0, zero, L(chkw)
  96. subu a2, t0
  97. SWHI a1, 0(a0) # Yes, handle first unaligned part
  98. addu a0, t0 # Now both a0 and a2 are updated
  99. L(chkw):
  100. andi t0, a2, 0x7 # Enough left for one loop iteration?
  101. beq t0, a2, L(chkl)
  102. subu a3, a2, t0
  103. addu a3, a0 # a3 is last loop address +1
  104. move a2, t0 # a2 is now # of bytes left after loop
  105. L(loopw):
  106. addiu a0, 8 # Handle 2 words pr. iteration
  107. sw a1, -8(a0)
  108. bne a0, a3, L(loopw)
  109. sw a1, -4(a0)
  110. L(chkl):
  111. andi t0, a2, 0x4 # Check if there is at least a full
  112. beq t0, zero, L(last8) # word remaining after the loop
  113. subu a2, t0
  114. sw a1, 0(a0) # Yes...
  115. addiu a0, 4
  116. L(last8):
  117. blez a2, L(exit) # Handle last 8 bytes (if cnt>0)
  118. addu a3, a2, a0 # a3 is last address +1
  119. L(lst8l):
  120. addiu a0, 1
  121. bne a0, a3, L(lst8l)
  122. sb a1, -1(a0)
  123. L(exit):
  124. j ra # Bye, bye
  125. nop
  126. .set reorder
  127. END (memset)
  128. #endif /* !__mips64 */
  129. libc_hidden_def(memset)