memset.S 4.0 KB

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