memchr.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright (C) 1996 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by David Mosberger (davidm@cs.arizona.edu).
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. /* Finds characters in a memory area. Optimized for the Alpha:
  17. - memory accessed as aligned quadwords only
  18. - uses cmpbge to compare 8 bytes in parallel
  19. - does binary search to find 0 byte in last
  20. quadword (HAKMEM needed 12 instructions to
  21. do this instead of the 9 instructions that
  22. binary search needs).
  23. For correctness consider that:
  24. - only minimum number of quadwords may be accessed
  25. - the third argument is an unsigned long
  26. */
  27. .set noreorder
  28. .set noat
  29. .globl memchr
  30. .ent memchr
  31. memchr:
  32. .frame $30,0,$26,0
  33. .prologue 0
  34. # Hack -- if someone passes in (size_t)-1, hoping to just
  35. # search til the end of the address space, we will overflow
  36. # below when we find the address of the last byte. Given
  37. # that we will never have a 56-bit address space, cropping
  38. # the length is the easiest way to avoid trouble.
  39. zap $18, 0x80, $5 #-e0 :
  40. beq $18, $not_found # .. e1 :
  41. ldq_u $1, 0($16) # e1 : load first quadword
  42. insbl $17, 1, $2 # .. e0 : $2 = 000000000000ch00
  43. and $17, 0xff, $17 #-e0 : $17 = 00000000000000ch
  44. cmpult $18, 9, $4 # .. e1 :
  45. or $2, $17, $17 # e0 : $17 = 000000000000chch
  46. lda $3, -1($31) # .. e1 :
  47. sll $17, 16, $2 #-e0 : $2 = 00000000chch0000
  48. addq $16, $5, $5 # .. e1 :
  49. or $2, $17, $17 # e1 : $17 = 00000000chchchch
  50. unop # :
  51. sll $17, 32, $2 #-e0 : $2 = chchchch00000000
  52. or $2, $17, $17 # e1 : $17 = chchchchchchchch
  53. extql $1, $16, $7 # e0 :
  54. beq $4, $first_quad # .. e1 :
  55. ldq_u $6, -1($5) #-e1 : eight or less bytes to search
  56. extqh $6, $16, $6 # .. e0 :
  57. mov $16, $0 # e0 :
  58. or $7, $6, $1 # .. e1 : $1 = quadword starting at $16
  59. # Deal with the case where at most 8 bytes remain to be searched
  60. # in $1. E.g.:
  61. # $18 = 6
  62. # $1 = ????c6c5c4c3c2c1
  63. $last_quad:
  64. negq $18, $6 #-e0 :
  65. xor $17, $1, $1 # .. e1 :
  66. srl $3, $6, $6 # e0 : $6 = mask of $18 bits set
  67. cmpbge $31, $1, $2 # .. e1 :
  68. and $2, $6, $2 #-e0 :
  69. beq $2, $not_found # .. e1 :
  70. $found_it:
  71. # Now, determine which byte matched:
  72. negq $2, $3 # e0 :
  73. and $2, $3, $2 # e1 :
  74. and $2, 0x0f, $1 #-e0 :
  75. addq $0, 4, $3 # .. e1 :
  76. cmoveq $1, $3, $0 # e0 :
  77. addq $0, 2, $3 # .. e1 :
  78. and $2, 0x33, $1 #-e0 :
  79. cmoveq $1, $3, $0 # .. e1 :
  80. and $2, 0x55, $1 # e0 :
  81. addq $0, 1, $3 # .. e1 :
  82. cmoveq $1, $3, $0 #-e0 :
  83. $done: ret # .. e1 :
  84. # Deal with the case where $18 > 8 bytes remain to be
  85. # searched. $16 may not be aligned.
  86. .align 4
  87. $first_quad:
  88. andnot $16, 0x7, $0 #-e1 :
  89. insqh $3, $16, $2 # .. e0 : $2 = 0000ffffffffffff ($16<0:2> ff)
  90. xor $1, $17, $1 # e0 :
  91. or $1, $2, $1 # e1 : $1 = ====ffffffffffff
  92. cmpbge $31, $1, $2 #-e0 :
  93. bne $2, $found_it # .. e1 :
  94. # At least one byte left to process.
  95. ldq $1, 8($0) # e0 :
  96. subq $5, 1, $18 # .. e1 :
  97. addq $0, 8, $0 #-e0 :
  98. # Make $18 point to last quad to be accessed (the
  99. # last quad may or may not be partial).
  100. andnot $18, 0x7, $18 # .. e1 :
  101. cmpult $0, $18, $2 # e0 :
  102. beq $2, $final # .. e1 :
  103. # At least two quads remain to be accessed.
  104. subq $18, $0, $4 #-e0 : $4 <- nr quads to be processed
  105. and $4, 8, $4 # e1 : odd number of quads?
  106. bne $4, $odd_quad_count # e1 :
  107. # At least three quads remain to be accessed
  108. mov $1, $4 # e0 : move prefetched value to correct reg
  109. .align 4
  110. $unrolled_loop:
  111. ldq $1, 8($0) #-e0 : prefetch $1
  112. xor $17, $4, $2 # .. e1 :
  113. cmpbge $31, $2, $2 # e0 :
  114. bne $2, $found_it # .. e1 :
  115. addq $0, 8, $0 #-e0 :
  116. $odd_quad_count:
  117. xor $17, $1, $2 # .. e1 :
  118. ldq $4, 8($0) # e0 : prefetch $4
  119. cmpbge $31, $2, $2 # .. e1 :
  120. addq $0, 8, $6 #-e0 :
  121. bne $2, $found_it # .. e1 :
  122. cmpult $6, $18, $6 # e0 :
  123. addq $0, 8, $0 # .. e1 :
  124. bne $6, $unrolled_loop #-e1 :
  125. mov $4, $1 # e0 : move prefetched value into $1
  126. $final: subq $5, $0, $18 # .. e1 : $18 <- number of bytes left to do
  127. bne $18, $last_quad # e1 :
  128. $not_found:
  129. mov $31, $0 #-e0 :
  130. ret # .. e1 :
  131. .end memchr