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