strcmp.S 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /* Bytewise compare two null-terminated strings. */
  15. #include <sysdep.h>
  16. .set noat
  17. .set noreorder
  18. .text
  19. ENTRY(strcmp)
  20. #ifdef PROF
  21. ldgp gp, 0(pv)
  22. lda AT, _mcount
  23. jmp AT, (AT), _mcount
  24. .prologue 1
  25. #else
  26. .prologue 0
  27. #endif
  28. ldq_u t0, 0(a0) # e0 : give cache time to catch up
  29. xor a0, a1, t2 # .. e1 : are s1 and s2 co-aligned?
  30. ldq_u t1, 0(a1) # e0 :
  31. and t2, 7, t2 # .. e1 :
  32. lda t3, -1 # e0 :
  33. bne t2, $unaligned # .. e1 :
  34. /* On entry to this basic block:
  35. t0 == the first destination word for masking back in
  36. t1 == the first source word.
  37. t3 == -1. */
  38. $aligned:
  39. mskqh t3, a0, t3 # e0 :
  40. nop # .. e1 :
  41. ornot t1, t3, t1 # e0 :
  42. ornot t0, t3, t0 # .. e1 :
  43. cmpbge zero, t1, t7 # e0 : bits set iff null found
  44. bne t7, $eos # e1 (zdb)
  45. /* Aligned compare main loop.
  46. On entry to this basic block:
  47. t0 == an s1 word.
  48. t1 == an s2 word not containing a null. */
  49. $a_loop:
  50. xor t0, t1, t2 # e0 :
  51. bne t2, $wordcmp # .. e1 (zdb)
  52. ldq_u t1, 8(a1) # e0 :
  53. ldq_u t0, 8(a0) # .. e1 :
  54. addq a1, 8, a1 # e0 :
  55. addq a0, 8, a0 # .. e1 :
  56. cmpbge zero, t1, t7 # e0 :
  57. beq t7, $a_loop # .. e1 (zdb)
  58. br $eos # e1 :
  59. /* The two strings are not co-aligned. Align s1 and cope. */
  60. $unaligned:
  61. and a0, 7, t4 # e0 : find s1 misalignment
  62. and a1, 7, t5 # .. e1 : find s2 misalignment
  63. subq a1, t4, a1 # e0 :
  64. /* If s2 misalignment is larger than s2 misalignment, we need
  65. extra startup checks to avoid SEGV. */
  66. cmplt t4, t5, t8 # .. e1 :
  67. beq t8, $u_head # e1 :
  68. mskqh t3, t5, t3 # e0 :
  69. ornot t1, t3, t3 # e0 :
  70. cmpbge zero, t3, t7 # e1 : is there a zero?
  71. beq t7, $u_head # e1 :
  72. /* We've found a zero in the first partial word of s2. Align
  73. our current s1 and s2 words and compare what we've got. */
  74. extql t1, t5, t1 # e0 :
  75. extql t0, a0, t0 # e0 :
  76. cmpbge zero, t1, t7 # .. e1 : find that zero again
  77. br $eos # e1 : and finish up
  78. .align 3
  79. $u_head:
  80. /* We know just enough now to be able to assemble the first
  81. full word of s2. We can still find a zero at the end of it.
  82. On entry to this basic block:
  83. t0 == first word of s1
  84. t1 == first partial word of s2. */
  85. ldq_u t2, 8(a1) # e0 : load second partial s2 word
  86. lda t3, -1 # .. e1 : create leading garbage mask
  87. extql t1, a1, t1 # e0 : create first s2 word
  88. mskqh t3, a0, t3 # e0 :
  89. extqh t2, a1, t4 # e0 :
  90. ornot t0, t3, t0 # .. e1 : kill s1 garbage
  91. or t1, t4, t1 # e0 : s2 word now complete
  92. cmpbge zero, t0, t7 # .. e1 : find zero in first s1 word
  93. ornot t1, t3, t1 # e0 : kill s2 garbage
  94. lda t3, -1 # .. e1 :
  95. mskql t3, a1, t3 # e0 : mask for s2[1] bits we have seen
  96. bne t7, $eos # .. e1 :
  97. xor t0, t1, t4 # e0 : compare aligned words
  98. bne t4, $wordcmp # .. e1 (zdb)
  99. or t2, t3, t3 # e0 :
  100. cmpbge zero, t3, t7 # e1 :
  101. bne t7, $u_final # e1 :
  102. /* Unaligned copy main loop. In order to avoid reading too much,
  103. the loop is structured to detect zeros in aligned words from s2.
  104. This has, unfortunately, effectively pulled half of a loop
  105. iteration out into the head and half into the tail, but it does
  106. prevent nastiness from accumulating in the very thing we want
  107. to run as fast as possible.
  108. On entry to this basic block:
  109. t2 == the unshifted low-bits from the next s2 word. */
  110. .align 3
  111. $u_loop:
  112. extql t2, a1, t3 # e0 :
  113. ldq_u t2, 16(a1) # .. e1 : load next s2 high bits
  114. ldq_u t0, 8(a0) # e0 : load next s1 word
  115. addq a1, 8, a1 # .. e1 :
  116. addq a0, 8, a0 # e0 :
  117. nop # .. e1 :
  118. extqh t2, a1, t1 # e0 :
  119. cmpbge zero, t0, t7 # .. e1 : find zero in current s1 word
  120. or t1, t3, t1 # e0 :
  121. bne t7, $eos # .. e1 :
  122. xor t0, t1, t4 # e0 : compare the words
  123. bne t4, $wordcmp # .. e1 (zdb)
  124. cmpbge zero, t2, t4 # e0 : find zero in next low bits
  125. beq t4, $u_loop # .. e1 (zdb)
  126. /* We've found a zero in the low bits of the last s2 word. Get
  127. the next s1 word and align them. */
  128. $u_final:
  129. ldq_u t0, 8(a0) # e1 :
  130. extql t2, a1, t1 # .. e0 :
  131. cmpbge zero, t1, t7 # e0 :
  132. /* We've found a zero somewhere in a word we just read.
  133. On entry to this basic block:
  134. t0 == s1 word
  135. t1 == s2 word
  136. t7 == cmpbge mask containing the zero. */
  137. .align 3
  138. $eos:
  139. negq t7, t6 # e0 : create bytemask of valid data
  140. and t6, t7, t8 # e1 :
  141. subq t8, 1, t6 # e0 :
  142. or t6, t8, t7 # e1 :
  143. zapnot t0, t7, t0 # e0 : kill the garbage
  144. zapnot t1, t7, t1 # .. e1 :
  145. xor t0, t1, v0 # e0 : and compare
  146. beq v0, $done # .. e1 :
  147. /* Here we have two differing co-aligned words in t0 & t1.
  148. Bytewise compare them and return (t0 > t1 ? 1 : -1). */
  149. $wordcmp:
  150. cmpbge t0, t1, t2 # e0 : comparison yields bit mask of ge
  151. cmpbge t1, t0, t3 # .. e1 :
  152. xor t2, t3, t0 # e0 : bits set iff t0/t1 bytes differ
  153. negq t0, t1 # e1 : clear all but least bit
  154. and t0, t1, t0 # e0 :
  155. lda v0, -1 # .. e1 :
  156. and t0, t2, t1 # e0 : was bit set in t0 > t1?
  157. cmovne t1, 1, v0 # .. e1 (zdb)
  158. $done:
  159. ret # e1 :
  160. END(strcmp)
  161. libc_hidden_builtin_def (strcmp)