ev6-memchr.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * arch/alpha/lib/ev6-memchr.S
  3. *
  4. * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
  5. *
  6. * Finds characters in a memory area. Optimized for the Alpha:
  7. *
  8. * - memory accessed as aligned quadwords only
  9. * - uses cmpbge to compare 8 bytes in parallel
  10. * - does binary search to find 0 byte in last
  11. * quadword (HAKMEM needed 12 instructions to
  12. * do this instead of the 9 instructions that
  13. * binary search needs).
  14. *
  15. * For correctness consider that:
  16. *
  17. * - only minimum number of quadwords may be accessed
  18. * - the third argument is an unsigned long
  19. *
  20. * Much of the information about 21264 scheduling/coding comes from:
  21. * Compiler Writer's Guide for the Alpha 21264
  22. * abbreviated as 'CWG' in other comments here
  23. * ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
  24. * Scheduling notation:
  25. * E - either cluster
  26. * U - upper subcluster; U0 - subcluster U0; U1 - subcluster U1
  27. * L - lower subcluster; L0 - subcluster L0; L1 - subcluster L1
  28. * Try not to change the actual algorithm if possible for consistency.
  29. */
  30. #include <asm/export.h>
  31. .set noreorder
  32. .set noat
  33. .align 4
  34. .globl memchr
  35. .ent memchr
  36. memchr:
  37. .frame $30,0,$26,0
  38. .prologue 0
  39. # Hack -- if someone passes in (size_t)-1, hoping to just
  40. # search til the end of the address space, we will overflow
  41. # below when we find the address of the last byte. Given
  42. # that we will never have a 56-bit address space, cropping
  43. # the length is the easiest way to avoid trouble.
  44. zap $18, 0x80, $5 # U : Bound length
  45. beq $18, $not_found # U :
  46. ldq_u $1, 0($16) # L : load first quadword Latency=3
  47. and $17, 0xff, $17 # E : L L U U : 00000000000000ch
  48. insbl $17, 1, $2 # U : 000000000000ch00
  49. cmpult $18, 9, $4 # E : small (< 1 quad) string?
  50. or $2, $17, $17 # E : 000000000000chch
  51. lda $3, -1($31) # E : U L L U
  52. sll $17, 16, $2 # U : 00000000chch0000
  53. addq $16, $5, $5 # E : Max search address
  54. or $2, $17, $17 # E : 00000000chchchch
  55. sll $17, 32, $2 # U : U L L U : chchchch00000000
  56. or $2, $17, $17 # E : chchchchchchchch
  57. extql $1, $16, $7 # U : $7 is upper bits
  58. beq $4, $first_quad # U :
  59. ldq_u $6, -1($5) # L : L U U L : eight or less bytes to search Latency=3
  60. extqh $6, $16, $6 # U : 2 cycle stall for $6
  61. mov $16, $0 # E :
  62. nop # E :
  63. or $7, $6, $1 # E : L U L U $1 = quadword starting at $16
  64. # Deal with the case where at most 8 bytes remain to be searched
  65. # in $1. E.g.:
  66. # $18 = 6
  67. # $1 = ????c6c5c4c3c2c1
  68. $last_quad:
  69. negq $18, $6 # E :
  70. xor $17, $1, $1 # E :
  71. srl $3, $6, $6 # U : $6 = mask of $18 bits set
  72. cmpbge $31, $1, $2 # E : L U L U
  73. nop
  74. nop
  75. and $2, $6, $2 # E :
  76. beq $2, $not_found # U : U L U L
  77. $found_it:
  78. #ifdef CONFIG_ALPHA_EV67
  79. /*
  80. * Since we are guaranteed to have set one of the bits, we don't
  81. * have to worry about coming back with a 0x40 out of cttz...
  82. */
  83. cttz $2, $3 # U0 :
  84. addq $0, $3, $0 # E : All done
  85. nop # E :
  86. ret # L0 : L U L U
  87. #else
  88. /*
  89. * Slow and clunky. It can probably be improved.
  90. * An exercise left for others.
  91. */
  92. negq $2, $3 # E :
  93. and $2, $3, $2 # E :
  94. and $2, 0x0f, $1 # E :
  95. addq $0, 4, $3 # E :
  96. cmoveq $1, $3, $0 # E : Latency 2, extra map cycle
  97. nop # E : keep with cmov
  98. and $2, 0x33, $1 # E :
  99. addq $0, 2, $3 # E : U L U L : 2 cycle stall on $0
  100. cmoveq $1, $3, $0 # E : Latency 2, extra map cycle
  101. nop # E : keep with cmov
  102. and $2, 0x55, $1 # E :
  103. addq $0, 1, $3 # E : U L U L : 2 cycle stall on $0
  104. cmoveq $1, $3, $0 # E : Latency 2, extra map cycle
  105. nop
  106. nop
  107. ret # L0 : L U L U
  108. #endif
  109. # Deal with the case where $18 > 8 bytes remain to be
  110. # searched. $16 may not be aligned.
  111. .align 4
  112. $first_quad:
  113. andnot $16, 0x7, $0 # E :
  114. insqh $3, $16, $2 # U : $2 = 0000ffffffffffff ($16<0:2> ff)
  115. xor $1, $17, $1 # E :
  116. or $1, $2, $1 # E : U L U L $1 = ====ffffffffffff
  117. cmpbge $31, $1, $2 # E :
  118. bne $2, $found_it # U :
  119. # At least one byte left to process.
  120. ldq $1, 8($0) # L :
  121. subq $5, 1, $18 # E : U L U L
  122. addq $0, 8, $0 # E :
  123. # Make $18 point to last quad to be accessed (the
  124. # last quad may or may not be partial).
  125. andnot $18, 0x7, $18 # E :
  126. cmpult $0, $18, $2 # E :
  127. beq $2, $final # U : U L U L
  128. # At least two quads remain to be accessed.
  129. subq $18, $0, $4 # E : $4 <- nr quads to be processed
  130. and $4, 8, $4 # E : odd number of quads?
  131. bne $4, $odd_quad_count # U :
  132. # At least three quads remain to be accessed
  133. mov $1, $4 # E : L U L U : move prefetched value to correct reg
  134. .align 4
  135. $unrolled_loop:
  136. ldq $1, 8($0) # L : prefetch $1
  137. xor $17, $4, $2 # E :
  138. cmpbge $31, $2, $2 # E :
  139. bne $2, $found_it # U : U L U L
  140. addq $0, 8, $0 # E :
  141. nop # E :
  142. nop # E :
  143. nop # E :
  144. $odd_quad_count:
  145. xor $17, $1, $2 # E :
  146. ldq $4, 8($0) # L : prefetch $4
  147. cmpbge $31, $2, $2 # E :
  148. addq $0, 8, $6 # E :
  149. bne $2, $found_it # U :
  150. cmpult $6, $18, $6 # E :
  151. addq $0, 8, $0 # E :
  152. nop # E :
  153. bne $6, $unrolled_loop # U :
  154. mov $4, $1 # E : move prefetched value into $1
  155. nop # E :
  156. nop # E :
  157. $final: subq $5, $0, $18 # E : $18 <- number of bytes left to do
  158. nop # E :
  159. nop # E :
  160. bne $18, $last_quad # U :
  161. $not_found:
  162. mov $31, $0 # E :
  163. nop # E :
  164. nop # E :
  165. ret # L0 :
  166. .end memchr
  167. EXPORT_SYMBOL(memchr)