/*1* arch/alpha/lib/ev6-divide.S2*3* 21264 version contributed by Rick Gorton <[email protected]>4*5* Alpha division..6*/78/*9* The alpha chip doesn't provide hardware division, so we have to do it10* by hand. The compiler expects the functions11*12* __divqu: 64-bit unsigned long divide13* __remqu: 64-bit unsigned long remainder14* __divqs/__remqs: signed 64-bit15* __divlu/__remlu: unsigned 32-bit16* __divls/__remls: signed 32-bit17*18* These are not normal C functions: instead of the normal19* calling sequence, these expect their arguments in registers20* $24 and $25, and return the result in $27. Register $28 may21* be clobbered (assembly temporary), anything else must be saved.22*23* In short: painful.24*25* This is a rather simple bit-at-a-time algorithm: it's very good26* at dividing random 64-bit numbers, but the more usual case where27* the divisor is small is handled better by the DEC algorithm28* using lookup tables. This uses much less memory, though, and is29* nicer on the cache.. Besides, I don't know the copyright status30* of the DEC code.31*/3233/*34* My temporaries:35* $0 - current bit36* $1 - shifted divisor37* $2 - modulus/quotient38*39* $23 - return address40* $24 - dividend41* $25 - divisor42*43* $27 - quotient/modulus44* $28 - compare status45*46* Much of the information about 21264 scheduling/coding comes from:47* Compiler Writer's Guide for the Alpha 2126448* abbreviated as 'CWG' in other comments here49* ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html50* Scheduling notation:51* E - either cluster52* U - upper subcluster; U0 - subcluster U0; U1 - subcluster U153* L - lower subcluster; L0 - subcluster L0; L1 - subcluster L154* Try not to change the actual algorithm if possible for consistency.55*/5657#define halt .long 05859/*60* Select function type and registers61*/62#define mask $063#define divisor $164#define compare $2865#define tmp1 $366#define tmp2 $46768#ifdef DIV69#define DIV_ONLY(x,y...) x,##y70#define MOD_ONLY(x,y...)71#define func(x) __div##x72#define modulus $273#define quotient $2774#define GETSIGN(x) xor $24,$25,x75#define STACK 4876#else77#define DIV_ONLY(x,y...)78#define MOD_ONLY(x,y...) x,##y79#define func(x) __rem##x80#define modulus $2781#define quotient $282#define GETSIGN(x) bis $24,$24,x83#define STACK 3284#endif8586/*87* For 32-bit operations, we need to extend to 64-bit88*/89#ifdef INTSIZE90#define ufunction func(lu)91#define sfunction func(l)92#define LONGIFY(x) zapnot x,15,x93#define SLONGIFY(x) addl x,0,x94#else95#define ufunction func(qu)96#define sfunction func(q)97#define LONGIFY(x)98#define SLONGIFY(x)99#endif100101.set noat102.align 4103.globl ufunction104.ent ufunction105ufunction:106subq $30,STACK,$30 # E :107.frame $30,STACK,$23108.prologue 01091107: stq $1, 0($30) # L :111bis $25,$25,divisor # E :112stq $2, 8($30) # L : L U L U113114bis $24,$24,modulus # E :115stq $0,16($30) # L :116bis $31,$31,quotient # E :117LONGIFY(divisor) # E : U L L U118119stq tmp1,24($30) # L :120LONGIFY(modulus) # E :121bis $31,1,mask # E :122DIV_ONLY(stq tmp2,32($30)) # L : L U U L123124beq divisor, 9f /* div by zero */125/*126* In spite of the DIV_ONLY being either a non-instruction127* or an actual stq, the addition of the .align directive128* below ensures that label 1 is going to be nicely aligned129*/130131.align 4132#ifdef INTSIZE133/*134* shift divisor left, using 3-bit shifts for135* 32-bit divides as we can't overflow. Three-bit136* shifts will result in looping three times less137* here, but can result in two loops more later.138* Thus using a large shift isn't worth it (and139* s8add pairs better than a sll..)140*/1411: cmpult divisor,modulus,compare # E :142s8addq divisor,$31,divisor # E :143s8addq mask,$31,mask # E :144bne compare,1b # U : U L U L145#else1461: cmpult divisor,modulus,compare # E :147nop # E :148nop # E :149blt divisor, 2f # U : U L U L150151addq divisor,divisor,divisor # E :152addq mask,mask,mask # E :153unop # E :154bne compare,1b # U : U L U L155#endif156157/* ok, start to go right again.. */1582:159/*160* Keep things nicely bundled... use a nop instead of not161* having an instruction for DIV_ONLY162*/163#ifdef DIV164DIV_ONLY(addq quotient,mask,tmp2) # E :165#else166nop # E :167#endif168srl mask,1,mask # U :169cmpule divisor,modulus,compare # E :170subq modulus,divisor,tmp1 # E :171172#ifdef DIV173DIV_ONLY(cmovne compare,tmp2,quotient) # E : Latency 2, extra map slot174nop # E : as part of the cmovne175srl divisor,1,divisor # U :176nop # E : L U L U177178nop # E :179cmovne compare,tmp1,modulus # E : Latency 2, extra map slot180nop # E : as part of the cmovne181bne mask,2b # U : U L U L182#else183srl divisor,1,divisor # U :184cmovne compare,tmp1,modulus # E : Latency 2, extra map slot185nop # E : as part of the cmovne186bne mask,2b # U : U L L U187#endif1881899: ldq $1, 0($30) # L :190ldq $2, 8($30) # L :191nop # E :192nop # E : U U L L193194ldq $0,16($30) # L :195ldq tmp1,24($30) # L :196nop # E :197nop # E :198199#ifdef DIV200DIV_ONLY(ldq tmp2,32($30)) # L :201#else202nop # E :203#endif204addq $30,STACK,$30 # E :205ret $31,($23),1 # L0 : L U U L206.end ufunction207208/*209* Uhh.. Ugly signed division. I'd rather not have it at all, but210* it's needed in some circumstances. There are different ways to211* handle this, really. This does:212* -a / b = a / -b = -(a / b)213* -a % b = -(a % b)214* a % -b = a % b215* which is probably not the best solution, but at least should216* have the property that (x/y)*y + (x%y) = x.217*/218.align 4219.globl sfunction220.ent sfunction221sfunction:222subq $30,STACK,$30 # E :223.frame $30,STACK,$23224.prologue 0225bis $24,$25,$28 # E :226SLONGIFY($28) # E :227bge $28,7b # U :228229stq $24,0($30) # L :230subq $31,$24,$28 # E :231stq $25,8($30) # L :232nop # E : U L U L233234cmovlt $24,$28,$24 /* abs($24) */ # E : Latency 2, extra map slot235nop # E : as part of the cmov236stq $23,16($30) # L :237subq $31,$25,$28 # E : U L U L238239stq tmp1,24($30) # L :240cmovlt $25,$28,$25 /* abs($25) */ # E : Latency 2, extra map slot241nop # E :242bsr $23,ufunction # L0: L U L U243244ldq $24,0($30) # L :245ldq $25,8($30) # L :246GETSIGN($28) # E :247subq $31,$27,tmp1 # E : U U L L248249SLONGIFY($28) # E :250ldq $23,16($30) # L :251cmovlt $28,tmp1,$27 # E : Latency 2, extra map slot252nop # E : U L L U : as part of the cmov253254ldq tmp1,24($30) # L :255nop # E : as part of the cmov256addq $30,STACK,$30 # E :257ret $31,($23),1 # L0 : L U U L258.end sfunction259260261