memchr.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <asm/export.h>
  28. .set noreorder
  29. .set noat
  30. .globl memchr
  31. .ent memchr
  32. memchr:
  33. .frame $30,0,$26,0
  34. .prologue 0
  35. # Hack -- if someone passes in (size_t)-1, hoping to just
  36. # search til the end of the address space, we will overflow
  37. # below when we find the address of the last byte. Given
  38. # that we will never have a 56-bit address space, cropping
  39. # the length is the easiest way to avoid trouble.
  40. zap $18, 0x80, $5 #-e0 :
  41. beq $18, $not_found # .. e1 :
  42. ldq_u $1, 0($16) # e1 : load first quadword
  43. insbl $17, 1, $2 # .. e0 : $2 = 000000000000ch00
  44. and $17, 0xff, $17 #-e0 : $17 = 00000000000000ch
  45. cmpult $18, 9, $4 # .. e1 :
  46. or $2, $17, $17 # e0 : $17 = 000000000000chch
  47. lda $3, -1($31) # .. e1 :
  48. sll $17, 16, $2 #-e0 : $2 = 00000000chch0000
  49. addq $16, $5, $5 # .. e1 :
  50. or $2, $17, $17 # e1 : $17 = 00000000chchchch
  51. unop # :
  52. sll $17, 32, $2 #-e0 : $2 = chchchch00000000
  53. or $2, $17, $17 # e1 : $17 = chchchchchchchch
  54. extql $1, $16, $7 # e0 :
  55. beq $4, $first_quad # .. e1 :
  56. ldq_u $6, -1($5) #-e1 : eight or less bytes to search
  57. extqh $6, $16, $6 # .. e0 :
  58. mov $16, $0 # e0 :
  59. or $7, $6, $1 # .. e1 : $1 = quadword starting at $16
  60. # Deal with the case where at most 8 bytes remain to be searched
  61. # in $1. E.g.:
  62. # $18 = 6
  63. # $1 = ????c6c5c4c3c2c1
  64. $last_quad:
  65. negq $18, $6 #-e0 :
  66. xor $17, $1, $1 # .. e1 :
  67. srl $3, $6, $6 # e0 : $6 = mask of $18 bits set
  68. cmpbge $31, $1, $2 # .. e1 :
  69. and $2, $6, $2 #-e0 :
  70. beq $2, $not_found # .. e1 :
  71. $found_it:
  72. # Now, determine which byte matched:
  73. negq $2, $3 # e0 :
  74. and $2, $3, $2 # e1 :
  75. and $2, 0x0f, $1 #-e0 :
  76. addq $0, 4, $3 # .. e1 :
  77. cmoveq $1, $3, $0 # e0 :
  78. addq $0, 2, $3 # .. e1 :
  79. and $2, 0x33, $1 #-e0 :
  80. cmoveq $1, $3, $0 # .. e1 :
  81. and $2, 0x55, $1 # e0 :
  82. addq $0, 1, $3 # .. e1 :
  83. cmoveq $1, $3, $0 #-e0 :
  84. $done: ret # .. e1 :
  85. # Deal with the case where $18 > 8 bytes remain to be
  86. # searched. $16 may not be aligned.
  87. .align 4
  88. $first_quad:
  89. andnot $16, 0x7, $0 #-e1 :
  90. insqh $3, $16, $2 # .. e0 : $2 = 0000ffffffffffff ($16<0:2> ff)
  91. xor $1, $17, $1 # e0 :
  92. or $1, $2, $1 # e1 : $1 = ====ffffffffffff
  93. cmpbge $31, $1, $2 #-e0 :
  94. bne $2, $found_it # .. e1 :
  95. # At least one byte left to process.
  96. ldq $1, 8($0) # e0 :
  97. subq $5, 1, $18 # .. e1 :
  98. addq $0, 8, $0 #-e0 :
  99. # Make $18 point to last quad to be accessed (the
  100. # last quad may or may not be partial).
  101. andnot $18, 0x7, $18 # .. e1 :
  102. cmpult $0, $18, $2 # e0 :
  103. beq $2, $final # .. e1 :
  104. # At least two quads remain to be accessed.
  105. subq $18, $0, $4 #-e0 : $4 <- nr quads to be processed
  106. and $4, 8, $4 # e1 : odd number of quads?
  107. bne $4, $odd_quad_count # e1 :
  108. # At least three quads remain to be accessed
  109. mov $1, $4 # e0 : move prefetched value to correct reg
  110. .align 4
  111. $unrolled_loop:
  112. ldq $1, 8($0) #-e0 : prefetch $1
  113. xor $17, $4, $2 # .. e1 :
  114. cmpbge $31, $2, $2 # e0 :
  115. bne $2, $found_it # .. e1 :
  116. addq $0, 8, $0 #-e0 :
  117. $odd_quad_count:
  118. xor $17, $1, $2 # .. e1 :
  119. ldq $4, 8($0) # e0 : prefetch $4
  120. cmpbge $31, $2, $2 # .. e1 :
  121. addq $0, 8, $6 #-e0 :
  122. bne $2, $found_it # .. e1 :
  123. cmpult $6, $18, $6 # e0 :
  124. addq $0, 8, $0 # .. e1 :
  125. bne $6, $unrolled_loop #-e1 :
  126. mov $4, $1 # e0 : move prefetched value into $1
  127. $final: subq $5, $0, $18 # .. e1 : $18 <- number of bytes left to do
  128. bne $18, $last_quad # e1 :
  129. $not_found:
  130. mov $31, $0 #-e0 :
  131. ret # .. e1 :
  132. .end memchr
  133. EXPORT_SYMBOL(memchr)