154390Storek/* 261166Sbostic * Copyright (c) 1992, 1993 361166Sbostic * The Regents of the University of California. All rights reserved. 454390Storek * 554390Storek * This software was developed by the Computer Systems Engineering group 654390Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 754390Storek * contributed to Berkeley. 854390Storek * 954390Storek * %sccs.include.redist.c% 1054390Storek * 1154390Storek * from: $Header: divrem.m4,v 1.4 92/06/25 13:23:57 torek Exp $ 1254390Storek */ 1354390Storek 1454390Storek/* 1554390Storek * Division and remainder, from Appendix E of the Sparc Version 8 1654390Storek * Architecture Manual, with fixes from Gordon Irlam. 1754390Storek */ 1854390Storek 1954390Storek#if defined(LIBC_SCCS) && !defined(lint) 20*61167Sbostic .asciz "@(#)divrem.m4 8.1 (Berkeley) 06/04/93" 2154390Storek#endif /* LIBC_SCCS and not lint */ 2254390Storek 2354390Storek/* 2454390Storek * Input: dividend and divisor in %o0 and %o1 respectively. 2554390Storek * 2654390Storek * m4 parameters: 2754390Storek * NAME name of function to generate 2854390Storek * OP OP=div => %o0 / %o1; OP=rem => %o0 % %o1 2954390Storek * S S=true => signed; S=false => unsigned 3054390Storek * 3154390Storek * Algorithm parameters: 3254390Storek * N how many bits per iteration we try to get (4) 3354390Storek * WORDSIZE total number of bits (32) 3454390Storek * 3554390Storek * Derived constants: 3654390Storek * TWOSUPN 2^N, for label generation (m4 exponentiation currently broken) 3754390Storek * TOPBITS number of bits in the top `decade' of a number 3854390Storek * 3954390Storek * Important variables: 4054390Storek * Q the partial quotient under development (initially 0) 4154390Storek * R the remainder so far, initially the dividend 4254390Storek * ITER number of main division loop iterations required; 4354390Storek * equal to ceil(log2(quotient) / N). Note that this 4454390Storek * is the log base (2^N) of the quotient. 4554390Storek * V the current comparand, initially divisor*2^(ITER*N-1) 4654390Storek * 4754390Storek * Cost: 4854390Storek * Current estimate for non-large dividend is 4954390Storek * ceil(log2(quotient) / N) * (10 + 7N/2) + C 5054390Storek * A large dividend is one greater than 2^(31-TOPBITS) and takes a 5154390Storek * different path, as the upper bits of the quotient must be developed 5254390Storek * one bit at a time. 5354390Storek */ 5454390Storek 5554390Storekdefine(N, `4') 5654390Storekdefine(TWOSUPN, `16') 5754390Storekdefine(WORDSIZE, `32') 5854390Storekdefine(TOPBITS, eval(WORDSIZE - N*((WORDSIZE-1)/N))) 5954390Storek 6054390Storekdefine(dividend, `%o0') 6154390Storekdefine(divisor, `%o1') 6254390Storekdefine(Q, `%o2') 6354390Storekdefine(R, `%o3') 6454390Storekdefine(ITER, `%o4') 6554390Storekdefine(V, `%o5') 6654390Storek 6754390Storek/* m4 reminder: ifelse(a,b,c,d) => if a is b, then c, else d */ 6854390Storekdefine(T, `%g1') 6954390Storekdefine(SC, `%g7') 7054390Storekifelse(S, `true', `define(SIGN, `%g6')') 7154390Storek 7254390Storek/* 7354390Storek * This is the recursive definition for developing quotient digits. 7454390Storek * 7554390Storek * Parameters: 7654390Storek * $1 the current depth, 1 <= $1 <= N 7754390Storek * $2 the current accumulation of quotient bits 7854390Storek * N max depth 7954390Storek * 8054390Storek * We add a new bit to $2 and either recurse or insert the bits in 8154390Storek * the quotient. R, Q, and V are inputs and outputs as defined above; 8254390Storek * the condition codes are expected to reflect the input R, and are 8354390Storek * modified to reflect the output R. 8454390Storek */ 8554390Storekdefine(DEVELOP_QUOTIENT_BITS, 8654390Storek` ! depth $1, accumulated bits $2 8754390Storek bl L.$1.eval(TWOSUPN+$2) 8854390Storek srl V,1,V 8954390Storek ! remainder is positive 9054390Storek subcc R,V,R 9154390Storek ifelse($1, N, 9254390Storek ` b 9f 9354390Storek add Q, ($2*2+1), Q 9454390Storek ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2+1)')') 9554390StorekL.$1.eval(TWOSUPN+$2): 9654390Storek ! remainder is negative 9754390Storek addcc R,V,R 9854390Storek ifelse($1, N, 9954390Storek ` b 9f 10054390Storek add Q, ($2*2-1), Q 10154390Storek ', ` DEVELOP_QUOTIENT_BITS(incr($1), `eval(2*$2-1)')') 10254390Storek ifelse($1, 1, `9:')') 10354390Storek 10454390Storek#include "DEFS.h" 10554390Storek#include <machine/trap.h> 10654390Storek 10754390StorekFUNC(NAME) 10854390Storekifelse(S, `true', 10954390Storek` ! compute sign of result; if neither is negative, no problem 11054390Storek orcc divisor, dividend, %g0 ! either negative? 11154390Storek bge 2f ! no, go do the divide 11254390Storek xor divisor, dividend, SIGN ! compute sign in any case 11354390Storek tst divisor 11454390Storek bge 1f 11554390Storek tst dividend 11654390Storek ! divisor is definitely negative; dividend might also be negative 11754390Storek bge 2f ! if dividend not negative... 11854390Storek neg divisor ! in any case, make divisor nonneg 11954390Storek1: ! dividend is negative, divisor is nonnegative 12054390Storek neg dividend ! make dividend nonnegative 12154390Storek2: 12254390Storek') 12354390Storek ! Ready to divide. Compute size of quotient; scale comparand. 12454390Storek orcc divisor, %g0, V 12554390Storek bnz 1f 12654390Storek mov dividend, R 12754390Storek 12854390Storek ! Divide by zero trap. If it returns, return 0 (about as 12954390Storek ! wrong as possible, but that is what SunOS does...). 13054390Storek t ST_DIV0 13154390Storek retl 13254390Storek clr %o0 13354390Storek 13454390Storek1: 13554390Storek cmp R, V ! if divisor exceeds dividend, done 13654390Storek blu Lgot_result ! (and algorithm fails otherwise) 13754390Storek clr Q 13854390Storek sethi %hi(1 << (WORDSIZE - TOPBITS - 1)), T 13954390Storek cmp R, T 14054390Storek blu Lnot_really_big 14154390Storek clr ITER 14254390Storek 14354390Storek ! `Here the dividend is >= 2^(31-N) or so. We must be careful here, 14454390Storek ! as our usual N-at-a-shot divide step will cause overflow and havoc. 14554390Storek ! The number of bits in the result here is N*ITER+SC, where SC <= N. 14654390Storek ! Compute ITER in an unorthodox manner: know we need to shift V into 14754390Storek ! the top decade: so do not even bother to compare to R.' 14854390Storek 1: 14954390Storek cmp V, T 15054390Storek bgeu 3f 15154390Storek mov 1, SC 15254390Storek sll V, N, V 15354390Storek b 1b 15454390Storek inc ITER 15554390Storek 15654390Storek ! Now compute SC. 15754390Storek 2: addcc V, V, V 15854390Storek bcc Lnot_too_big 15954390Storek inc SC 16054390Storek 16154390Storek ! We get here if the divisor overflowed while shifting. 16254390Storek ! This means that R has the high-order bit set. 16354390Storek ! Restore V and subtract from R. 16454390Storek sll T, TOPBITS, T ! high order bit 16554390Storek srl V, 1, V ! rest of V 16654390Storek add V, T, V 16754390Storek b Ldo_single_div 16854390Storek dec SC 16954390Storek 17054390Storek Lnot_too_big: 17154390Storek 3: cmp V, R 17254390Storek blu 2b 17354390Storek nop 17454390Storek be Ldo_single_div 17554390Storek nop 17654390Storek /* NB: these are commented out in the V8-Sparc manual as well */ 17754390Storek /* (I do not understand this) */ 17854390Storek ! V > R: went too far: back up 1 step 17954390Storek ! srl V, 1, V 18054390Storek ! dec SC 18154390Storek ! do single-bit divide steps 18254390Storek ! 18354390Storek ! We have to be careful here. We know that R >= V, so we can do the 18454390Storek ! first divide step without thinking. BUT, the others are conditional, 18554390Storek ! and are only done if R >= 0. Because both R and V may have the high- 18654390Storek ! order bit set in the first step, just falling into the regular 18754390Storek ! division loop will mess up the first time around. 18854390Storek ! So we unroll slightly... 18954390Storek Ldo_single_div: 19054390Storek deccc SC 19154390Storek bl Lend_regular_divide 19254390Storek nop 19354390Storek sub R, V, R 19454390Storek mov 1, Q 19554390Storek b Lend_single_divloop 19654390Storek nop 19754390Storek Lsingle_divloop: 19854390Storek sll Q, 1, Q 19954390Storek bl 1f 20054390Storek srl V, 1, V 20154390Storek ! R >= 0 20254390Storek sub R, V, R 20354390Storek b 2f 20454390Storek inc Q 20554390Storek 1: ! R < 0 20654390Storek add R, V, R 20754390Storek dec Q 20854390Storek 2: 20954390Storek Lend_single_divloop: 21054390Storek deccc SC 21154390Storek bge Lsingle_divloop 21254390Storek tst R 21354390Storek b,a Lend_regular_divide 21454390Storek 21554390StorekLnot_really_big: 21654390Storek1: 21754390Storek sll V, N, V 21854390Storek cmp V, R 21954390Storek bleu 1b 22054390Storek inccc ITER 22154390Storek be Lgot_result 22254390Storek dec ITER 22354390Storek 22454390Storek tst R ! set up for initial iteration 22554390StorekLdivloop: 22654390Storek sll Q, N, Q 22754390Storek DEVELOP_QUOTIENT_BITS(1, 0) 22854390StorekLend_regular_divide: 22954390Storek deccc ITER 23054390Storek bge Ldivloop 23154390Storek tst R 23254390Storek bl,a Lgot_result 23354390Storek ! non-restoring fixup here (one instruction only!) 23454390Storekifelse(OP, `div', 23554390Storek` dec Q 23654390Storek', ` add R, divisor, R 23754390Storek') 23854390Storek 23954390StorekLgot_result: 24054390Storekifelse(S, `true', 24154390Storek` ! check to see if answer should be < 0 24254390Storek tst SIGN 24354390Storek bl,a 1f 24454390Storek ifelse(OP, `div', `neg Q', `neg R') 24554390Storek1:') 24654390Storek retl 24754390Storek ifelse(OP, `div', `mov Q, %o0', `mov R, %o0') 248