\ amr-math.fs -- adapted from math.fs 28Aug07rjn
\ GForth
[

decimal

\ quad = 4 16bit words.
: q2*  ( q1 - q2) >r >r >r 2* r> 2*' r> 2*' r> 2*' ;

: rot  ( a b c - c a b) swap >r swap r> ;

: 2swap  ( a b c d - c d a b) >r rot r> rot ;

\ Compare two unsigned double numbers.
: ud<  ( ud1 ud2 - flag)
	>r swap r>  2dup = not if  2swap  then  2drop u< ;

: dnegate  ( d1 - d2) >r invert r> invert 1 0 d+ ;

: udm/  ( uq ud1 - ud2)
        32 for
                2>r q2*
                2dup 2r@ ud< not if
                        2r@ dnegate d+
                        2>r 1 0 d+ 2r>
                then    2r>
        next
	2drop ( divisor) 2drop ( remainder) ;

\ : udn*t  ( ud n - t) swap over um* 2>r um* 0 2r> d+ ;

\ variable q0
\ variable q1
\ variable q2
\ variable q3

\ : q+  ( q1 q2 - q3)
\	q3 ! q2 ! q1 ! q0 !
\	>r >r >r q0 @ + r> q1 @ +' r> q2 @ +' r> q3 @ +' ;

\ : udm*  ( ud1 ud2 - uq3)
\	2>r 2dup 2r> 2swap rot udn*t
\	2>r 0 swap 2>r udn*t 0 2r> 2r> q+ ;

\ : ud*/  ( ud1 ud2 ud3 - ud4) 2>r udm* 2r> udm/ ;

\ Divide an unsigned double number by 2, right shifting.
code ud2/  ( ud1 - ud2)
	C clr
	R2 A xch  A rrc  A R6 mov  R2 A xch  A rrc  A R7 mov
	' drop call  R2 A xch  A rrc  R2 A xch  A rrc
	' dup call  R6 A mov  R2 A xch  R7 A mov
	next c;
	
