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