memset.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <features.h>
  17. #include <sysdep.h>
  18. #include <endian.h>
  19. /* void *memset(void *s, int c, size_t n). */
  20. #ifdef __mips64
  21. #include <sys/asm.h>
  22. #if __BYTE_ORDER == __BIG_ENDIAN
  23. # define SDHI sdl /* high part is left in big-endian */
  24. #else
  25. # define SDHI sdr /* high part is right in little-endian */
  26. #endif
  27. ENTRY (memset)
  28. .set noreorder
  29. slti ta1, a2, 16 # Less than 16?
  30. bne ta1, zero, L(last16)
  31. move v0, a0 # Setup exit value before too late
  32. beq a1, zero, L(ueven) # If zero pattern, no need to extend
  33. andi a1, 0xff # Avoid problems with bogus arguments
  34. dsll ta0, a1, 8
  35. or a1, ta0
  36. dsll ta0, a1, 16
  37. or a1, ta0 # a1 is now pattern in full word
  38. dsll ta0, a1, 32
  39. or a1, ta0 # a1 is now pattern in double word
  40. L(ueven):
  41. PTR_SUBU ta0, zero, a0 # Unaligned address?
  42. andi ta0, 0x7
  43. beq ta0, zero, L(chkw)
  44. PTR_SUBU a2, ta0
  45. SDHI a1, 0(a0) # Yes, handle first unaligned part
  46. PTR_ADDU a0, ta0 # Now both a0 and a2 are updated
  47. L(chkw):
  48. andi ta0, a2, 0xf # Enough left for one loop iteration?
  49. beq ta0, a2, L(chkl)
  50. PTR_SUBU a3, a2, ta0
  51. PTR_ADDU a3, a0 # a3 is last loop address +1
  52. move a2, ta0 # a2 is now # of bytes left after loop
  53. L(loopw):
  54. PTR_ADDIU a0, 16 # Handle 2 dwords pr. iteration
  55. sd a1, -16(a0)
  56. bne a0, a3, L(loopw)
  57. sd a1, -8(a0)
  58. L(chkl):
  59. andi ta0, a2, 0x8 # Check if there is at least a double
  60. beq ta0, zero, L(last16) # word remaining after the loop
  61. PTR_SUBU a2, ta0
  62. sd a1, 0(a0) # Yes...
  63. PTR_ADDIU a0, 8
  64. L(last16):
  65. blez a2, L(exit) # Handle last 16 bytes (if cnt>0)
  66. PTR_ADDU a3, a2, a0 # a3 is last address +1
  67. L(lst16l):
  68. PTR_ADDIU a0, 1
  69. bne a0, a3, L(lst16l)
  70. sb a1, -1(a0)
  71. L(exit):
  72. j ra # Bye, bye
  73. nop
  74. .set reorder
  75. END (memset)
  76. #else /* !__mips64 */
  77. #if __BYTE_ORDER == __BIG_ENDIAN
  78. # define SWHI swl /* high part is left in big-endian */
  79. #else
  80. # define SWHI swr /* high part is right in little-endian */
  81. #endif
  82. ENTRY (memset)
  83. .set noreorder
  84. slti t1, a2, 8 # Less than 8?
  85. bne t1, zero, L(last8)
  86. move v0, a0 # Setup exit value before too late
  87. beq a1, zero, L(ueven) # If zero pattern, no need to extend
  88. andi a1, 0xff # Avoid problems with bogus arguments
  89. sll t0, a1, 8
  90. or a1, t0
  91. sll t0, a1, 16
  92. or a1, t0 # a1 is now pattern in full word
  93. L(ueven):
  94. subu t0, zero, a0 # Unaligned address?
  95. andi t0, 0x3
  96. beq t0, zero, L(chkw)
  97. subu a2, t0
  98. SWHI a1, 0(a0) # Yes, handle first unaligned part
  99. addu a0, t0 # Now both a0 and a2 are updated
  100. L(chkw):
  101. andi t0, a2, 0x7 # Enough left for one loop iteration?
  102. beq t0, a2, L(chkl)
  103. subu a3, a2, t0
  104. addu a3, a0 # a3 is last loop address +1
  105. move a2, t0 # a2 is now # of bytes left after loop
  106. L(loopw):
  107. addiu a0, 8 # Handle 2 words pr. iteration
  108. sw a1, -8(a0)
  109. bne a0, a3, L(loopw)
  110. sw a1, -4(a0)
  111. L(chkl):
  112. andi t0, a2, 0x4 # Check if there is at least a full
  113. beq t0, zero, L(last8) # word remaining after the loop
  114. subu a2, t0
  115. sw a1, 0(a0) # Yes...
  116. addiu a0, 4
  117. L(last8):
  118. blez a2, L(exit) # Handle last 8 bytes (if cnt>0)
  119. addu a3, a2, a0 # a3 is last address +1
  120. L(lst8l):
  121. addiu a0, 1
  122. bne a0, a3, L(lst8l)
  123. sb a1, -1(a0)
  124. L(exit):
  125. j ra # Bye, bye
  126. nop
  127. .set reorder
  128. END (memset)
  129. #endif /* !__mips64 */
  130. libc_hidden_def(memset)