/* Copyright (C) 1996 Free Software Foundation, Inc.1This file is part of the GNU C Library.2Contributed by David Mosberger ([email protected]).34The GNU C Library is free software; you can redistribute it and/or5modify it under the terms of the GNU Library General Public License as6published by the Free Software Foundation; either version 2 of the7License, or (at your option) any later version.89The GNU C Library is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12Library General Public License for more details.1314You should have received a copy of the GNU Library General Public15License along with the GNU C Library; see the file COPYING.LIB. If not,16write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,17Boston, MA 02111-1307, USA. */1819/* Finds characters in a memory area. Optimized for the Alpha:2021- memory accessed as aligned quadwords only22- uses cmpbge to compare 8 bytes in parallel23- does binary search to find 0 byte in last24quadword (HAKMEM needed 12 instructions to25do this instead of the 9 instructions that26binary search needs).2728For correctness consider that:2930- only minimum number of quadwords may be accessed31- the third argument is an unsigned long32*/3334.set noreorder35.set noat3637.globl memchr38.ent memchr39memchr:40.frame $30,0,$26,041.prologue 04243# Hack -- if someone passes in (size_t)-1, hoping to just44# search til the end of the address space, we will overflow45# below when we find the address of the last byte. Given46# that we will never have a 56-bit address space, cropping47# the length is the easiest way to avoid trouble.48zap $18, 0x80, $5 #-e0 :4950beq $18, $not_found # .. e1 :51ldq_u $1, 0($16) # e1 : load first quadword52insbl $17, 1, $2 # .. e0 : $2 = 000000000000ch0053and $17, 0xff, $17 #-e0 : $17 = 00000000000000ch54cmpult $18, 9, $4 # .. e1 :55or $2, $17, $17 # e0 : $17 = 000000000000chch56lda $3, -1($31) # .. e1 :57sll $17, 16, $2 #-e0 : $2 = 00000000chch000058addq $16, $5, $5 # .. e1 :59or $2, $17, $17 # e1 : $17 = 00000000chchchch60unop # :61sll $17, 32, $2 #-e0 : $2 = chchchch0000000062or $2, $17, $17 # e1 : $17 = chchchchchchchch63extql $1, $16, $7 # e0 :64beq $4, $first_quad # .. e1 :6566ldq_u $6, -1($5) #-e1 : eight or less bytes to search67extqh $6, $16, $6 # .. e0 :68mov $16, $0 # e0 :69or $7, $6, $1 # .. e1 : $1 = quadword starting at $167071# Deal with the case where at most 8 bytes remain to be searched72# in $1. E.g.:73# $18 = 674# $1 = ????c6c5c4c3c2c175$last_quad:76negq $18, $6 #-e0 :77xor $17, $1, $1 # .. e1 :78srl $3, $6, $6 # e0 : $6 = mask of $18 bits set79cmpbge $31, $1, $2 # .. e1 :80and $2, $6, $2 #-e0 :81beq $2, $not_found # .. e1 :8283$found_it:84# Now, determine which byte matched:85negq $2, $3 # e0 :86and $2, $3, $2 # e1 :8788and $2, 0x0f, $1 #-e0 :89addq $0, 4, $3 # .. e1 :90cmoveq $1, $3, $0 # e0 :9192addq $0, 2, $3 # .. e1 :93and $2, 0x33, $1 #-e0 :94cmoveq $1, $3, $0 # .. e1 :9596and $2, 0x55, $1 # e0 :97addq $0, 1, $3 # .. e1 :98cmoveq $1, $3, $0 #-e0 :99100$done: ret # .. e1 :101102# Deal with the case where $18 > 8 bytes remain to be103# searched. $16 may not be aligned.104.align 4105$first_quad:106andnot $16, 0x7, $0 #-e1 :107insqh $3, $16, $2 # .. e0 : $2 = 0000ffffffffffff ($16<0:2> ff)108xor $1, $17, $1 # e0 :109or $1, $2, $1 # e1 : $1 = ====ffffffffffff110cmpbge $31, $1, $2 #-e0 :111bne $2, $found_it # .. e1 :112113# At least one byte left to process.114115ldq $1, 8($0) # e0 :116subq $5, 1, $18 # .. e1 :117addq $0, 8, $0 #-e0 :118119# Make $18 point to last quad to be accessed (the120# last quad may or may not be partial).121122andnot $18, 0x7, $18 # .. e1 :123cmpult $0, $18, $2 # e0 :124beq $2, $final # .. e1 :125126# At least two quads remain to be accessed.127128subq $18, $0, $4 #-e0 : $4 <- nr quads to be processed129and $4, 8, $4 # e1 : odd number of quads?130bne $4, $odd_quad_count # e1 :131132# At least three quads remain to be accessed133134mov $1, $4 # e0 : move prefetched value to correct reg135136.align 4137$unrolled_loop:138ldq $1, 8($0) #-e0 : prefetch $1139xor $17, $4, $2 # .. e1 :140cmpbge $31, $2, $2 # e0 :141bne $2, $found_it # .. e1 :142143addq $0, 8, $0 #-e0 :144$odd_quad_count:145xor $17, $1, $2 # .. e1 :146ldq $4, 8($0) # e0 : prefetch $4147cmpbge $31, $2, $2 # .. e1 :148addq $0, 8, $6 #-e0 :149bne $2, $found_it # .. e1 :150151cmpult $6, $18, $6 # e0 :152addq $0, 8, $0 # .. e1 :153bne $6, $unrolled_loop #-e1 :154155mov $4, $1 # e0 : move prefetched value into $1156$final: subq $5, $0, $18 # .. e1 : $18 <- number of bytes left to do157bne $18, $last_quad # e1 :158159$not_found:160mov $31, $0 #-e0 :161ret # .. e1 :162163.end memchr164165166