at least sort of working

This commit is contained in:
~d6 2024-09-09 13:23:24 -04:00
parent e2d4e0506e
commit ba462deeed
2 changed files with 171 additions and 110 deletions

View File

@ -5,64 +5,125 @@
%DENOM16 { #03e8 } %DENOM16 { #03e8 }
%DENOM32 { #0000 #03e8 } %DENOM32 { #0000 #03e8 }
@x32-eq ( x/** y/** -> bool^ ) !eq32 |0100
@x32-ne ( x/** y/** -> bool^ ) !ne32 ( test cases -- compare the two 32-bit values on wst )
@x32-is-zero ( x/** -> bool^ ) !is-zero32 #0000 #03e8 ( a=1 )
@x32-non-zero ( x/** -> bool^ ) !non-zero32 #0000 #07d0 ( b=2 )
x32-add ( a+b )
#0000 #0bb8 ( c=3 )
#010e DEO POP4 POP4 #0a18 DEO
#0000 #07d0 ( a=2 )
#0000 #0bb8 ( b=3 )
x32-mul ( a*b )
#0000 #1770 ( c=6 )
#010e DEO POP4 POP4 #0a18 DEO
#0000 #4a38 ( a=19 )
#0000 #6978 ( b=27 )
x32-mul ( a*b )
#0007 #d3d8 ( c=513 )
#010e DEO POP4 POP4 #0a18 DEO
#0000 #1d4c ( a=7.5 )
#0000 #05dc ( b=1.5 )
x32-div ( a/b )
#0000 #1388 ( c=5.0 )
#010e DEO POP4 POP4 #0a18 DEO
#010e DEO #800f DEO BRK ( ensure stack is empty )
@x32-eq ( x/** y/** -> bool^ ) !u32-eq
@x32-ne ( x/** y/** -> bool^ ) !u32-ne
@x32-is-zero ( x/** -> bool^ ) !u32-is-zero
@x32-non-zero ( x/** -> bool^ ) !u32-non-zero
@x32-is-positive ( x/** -> bool^ ) POP2 #8000 LTH2 JMP2r @x32-is-positive ( x/** -> bool^ ) POP2 #8000 LTH2 JMP2r
@x32-is-negative ( x/** -> bool^ ) POP2 #7fff GTH2 JMP2r @x32-is-negative ( x/** -> bool^ ) POP2 #7fff GTH2 JMP2r
@x32-from-u8 ( x^ -> x/** ) @x32-from-u8 ( x^ -> x/** )
#0000 ROT OVR SWP DENOM32 !mul32 #0000 ROT OVR SWP DENOM32 !u32-mul
@x32-from-u16 ( x* -> x/** ) @x32-from-u16 ( x* -> x/** )
#0000 SWP2 DENOM32 !mul32 #0000 SWP2 DENOM32 !u32-mul
@x32-from-u32 ( x** -> x/** ) @x32-from-u32 ( x** -> x/** )
DENOM32 !mul32 DENOM32 !u32-mul
@x32-prepare-cmp ( x/** y/** -> x/** y/** xp^ yp^ ) @x32-prepare-cmp ( x/** y/** -> x/** y/** xp^ yp^ )
OVR2 #8000 LTH2 ,&yp STR STH2 STH2 OVR2 #8000 LTH2 ,&yp STR STH2 STH2
OVR2 #8000 LTH2 ,&xp STR STH2r STH2r OVR2 #8000 LTH2 ,&xp STR STH2r STH2r
LIT2 [ &xp $1 &yp $1 ] JMP2r LIT2 [ &xp $1 &yp $1 ] JMP2r
( TODO: test these implementations )
@x32-lt-old ( x** y** -> x<y^ )
STH2 SWP2 STH2 EOR2k #8000 LTH2 ?{ ( ; do x and y have different signs? )
POP2r POP2r POP2 #8000 GTH2 JMP2r ( ; signs differ, is x negative? )
} GTH2r STHr ?{ ( ; same signs, is xlo < ylo? )
LTH2 JMP2r ( ; no, is xhi < yhi? )
} GTH2 #00 EQU JMP2r ( ; yes, is xhi <= yhi? )
( TODO: test these implementations )
@x32-gt-old ( x** y** -> x<y^ )
STH2 SWP2 STH2 EOR2k #8000 LTH2 ?{ ( ; do x and y have different signs? )
POP2r POP2r POP2 #8000 LTH2 JMP2r ( ; signs differ, is x positive? )
} LTH2r STHr ?{ ( ; same signs, is xlo > ylo? )
GTH2 JMP2r ( ; no, is xhi > yhi? )
} LTH2 #00 EQU JMP2r ( ; yes, is xhi >= yhi? )
@x32-lt ( x/** y/** -> bool^ ) @x32-lt ( x/** y/** -> bool^ )
x32-prepare-cmp NEQk ?{ POP2 !lt32 } LTH STH POP8 STHr JMP2r x32-prepare-cmp NEQk ?{ POP2 !u32-lt } LTH STH POP8 STHr JMP2r
@x32-gt ( x/** y/** -> bool^ ) @x32-gt ( x/** y/** -> bool^ )
x32-prepare-cmp NEQk ?{ POP2 !gt32 } GTH STH POP8 STHr JMP2r x32-prepare-cmp NEQk ?{ POP2 !u32-gt } GTH STH POP8 STHr JMP2r
@x32-lteq ( x/** y/** -> bool^ ) @x32-lteq ( x/** y/** -> bool^ )
x32-prepare-cmp NEQk ?{ POP2 !lteq32 } LTH STH POP8 STHr JMP2r x32-prepare-cmp NEQk ?{ POP2 !u32-lteq } LTH STH POP8 STHr JMP2r
@x32-gteq ( x/** y/** -> bool^ ) @x32-gteq ( x/** y/** -> bool^ )
x32-prepare-cmp NEQk ?{ POP2 !gteq32 } GTH STH POP8 STHr JMP2r x32-prepare-cmp NEQk ?{ POP2 !u32-gteq } GTH STH POP8 STHr JMP2r
@x32-add ( x/** y/** -> z/** ) @x32-add ( x/** y/** -> z/** )
!add32 !u32-add
@x32-sub ( x/** y/** -> z/** ) @x32-sub ( x/** y/** -> z/** )
!sub32 !u32-sub
@x32-negate ( x/** y/** -> z/** ) @x32-negate ( x/** y/** -> z/** )
!negate32 !u32-negate
( multiply a fixed point number by an unsigned integer ) ( multiply a fixed point number by an unsigned integer )
@x32-scaled-mul32 ( x/** y** -> z/** ) @x32-scaled-mul32 ( x/** y** -> z/** )
!mul32 !u32-mul
( multiply a fixed point number by an unsigned integer ) ( multiply a fixed point number by an unsigned integer )
@x32-scaled-mul16 ( x/** y* -> z/** ) @x32-scaled-mul16 ( x/** y* -> z/** )
!mul16 !u32-mul16
@x32-scaled-div32 ( x/** y** -> z/** ) @x32-scaled-div32 ( x/** y** -> z/** )
!div32 !u32-div
( TODO ) ( [x * y]/1000 = floor[x/1000] + [[x%1000]*y]/1000 )
@x32-mul ( x/** y/** -> z/** ) @x32-mul ( x/** y/** -> z/** )
STH2 STH2 DENOM32 ( x/** 1000** [ylo* yhi*] )
u32-divmod ( q** r** [ylo* yhi*] )
STH2kr OVR2r STH2r u32-mul ( q** ry** [ylo* yhi*] )
DENOM32 u32-div ( q** ry1000** [ylo* yhi*] )
ROT2 STH2 ROT2 STH2r ( ry1000** q** [ylo* yhi*] )
STH2r STH2r u32-mul ( ry1000** qy** )
u32-add ( qy+ry/1000** )
JMP2r ( z/** )
( TODO ) ( [x * 1000]/y = floor[x/y]*1000 + [[x%y]*1000]/y )
@x32-div ( x/** y/** -> z/** ) @x32-div ( x/** y/** -> z/** )
STH2k OVR2 STH2 ( x/** y/** [ylo* yhi*] )
u32-divmod
DENOM32 u32-mul ( q** r1000** [ylo* yhi*] )
STH2r STH2r u32-div ( q** r1000/y** [ylo* yhi*] )
ROT2 STH2 ROT2 STH2r ( r1000/y** q** [ylo* yhi*] )
DENOM32 u32-mul ( r1000/y** q1000** )
u32-add ( q+r1000/y** )
JMP2r ( z/** )
~math32.tal ~math32.tal

View File

@ -20,176 +20,176 @@
( Operations supported: ) ( Operations supported: )
( ) ( )
( NAME STACK EFFECT DEFINITION ) ( NAME STACK EFFECT DEFINITION )
( add32 x** y** -> z** x + y ) ( u32-add x** y** -> z** x + y )
( sub32 x** y** -> z** x - y ) ( u32-sub x** y** -> z** x - y )
( mul16 x* y* -> z** x * y ) ( u32-mul x** y** -> z** x * y )
( mul32 x** y** -> z** x * y ) ( u32-mul16 x* y* -> z** x * y )
( div32 x** y** -> q** x / y ) ( u32-div x** y** -> q** x / y )
( mod32 x** y** -> r** x % y ) ( u32-mod x** y** -> r** x % y )
( divmod32 x** y** -> q** r** x / y, x % y ) ( u32-divmod x** y** -> q** r** x / y, x % y )
( gcd32 x** y** -> z** gcd[x, y] ) ( u32-gcd x** y** -> z** gcd[x, y] )
( negate32 x** -> z** -x ) ( u32-negate x** -> z** -x )
( lshift32 x** n^ -> z** x<<n ) ( u32-lshift x** n^ -> z** x<<n )
( rshift32 x** n^ -> z** x>>n ) ( u32-rshift x** n^ -> z** x>>n )
( and32 x** y** -> z** x & y ) ( u32-and x** y** -> z** x & y )
( or32 x** y** -> z** x | y ) ( u32-or x** y** -> z** x | y )
( xor32 x** y** -> z** x ^ y ) ( u32-xor x** y** -> z** x ^ y )
( complement32 x** -> z** ~x ) ( u32-complement x** -> z** ~x )
( eq32 x** y** -> bool^ x == y ) ( u32-eq x** y** -> bool^ x == y )
( ne32 x** y** -> bool^ x != y ) ( u32-ne x** y** -> bool^ x != y )
( is-zero32 x** -> bool^ x == 0 ) ( u32-is-zero x** -> bool^ x == 0 )
( non-zero32 x** -> bool^ x != 0 ) ( u32-non-zero x** -> bool^ x != 0 )
( lt32 x** y** -> bool^ x < y ) ( u32-lt x** y** -> bool^ x < y )
( gt32 x** y** -> bool^ x > y ) ( u32-gt x** y** -> bool^ x > y )
( lteq32 x** y** -> bool^ x <= y ) ( u32-lteq x** y** -> bool^ x <= y )
( gteq32 x** y** -> bool^ x >= y ) ( u32-gteq x** y** -> bool^ x >= y )
( bitcount8 x^ -> bool^ floor[log2[x]]+1 ) ( u8-bitcount x^ -> bool^ floor[log2[x]]+1 )
( bitcount16 x* -> bool^ floor[log2[x]]+1 ) ( u16-bitcount x* -> bool^ floor[log2[x]]+1 )
( bitcount32 x** -> bool^ floor[log2[x]]+1 ) ( u32-bitcount x** -> bool^ floor[log2[x]]+1 )
( ) ( )
( bitcount: number of bits needed to represent the number. ) ( bitcount: number of bits needed to represent the number. )
( this is equivalent to floor[log2[x]] + 1 ) ( this is equivalent to floor[log2[x]] + 1 )
@bitcount8 ( x^ -> n^ ) @u8-bitcount ( x^ -> n^ )
LITr 00 &loop DUP ?{ POP STHr JMP2r } #01 SFT INCr !&loop LITr 00 &loop DUP ?{ POP STHr JMP2r } #01 SFT INCr !&loop
@bitcount16 ( x* -> n^ ) @u16-bitcount ( x* -> n^ )
LITr 00 &loop ORAk ?{ POP2 STHr JMP2r } #01 SFT2 INCr !&loop LITr 00 &loop ORAk ?{ POP2 STHr JMP2r } #01 SFT2 INCr !&loop
@bitcount32 ( x** -> n^ ) @u32-bitcount ( x** -> n^ )
SWP2 bitcount16 DUP ?{ POP !bitcount16 } #10 NIP2 ADD JMP2r SWP2 u16-bitcount DUP ?{ POP !u16-bitcount } #10 NIP2 ADD JMP2r
( -- equality ) ( -- equality )
( x == y ) ( x == y )
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ ) @u32-eq ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 EQU2 STH EQU2 STHr AND JMP2r ROT2 EQU2 STH EQU2 STHr AND JMP2r
( x != y ) ( x != y )
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ ) @u32-ne ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 NEQ2 STH NEQ2 STHr ORA JMP2r ROT2 NEQ2 STH NEQ2 STHr ORA JMP2r
( x == 0 ) ( x == 0 )
@is-zero32 ( x** -> bool^ ) @u32-is-zero ( x** -> bool^ )
ORA2 #0000 EQU2 JMP2r ORA2 #0000 EQU2 JMP2r
( x != 0 ) ( x != 0 )
@non-zero32 ( x** -> bool^ ) @u32-non-zero ( x** -> bool^ )
ORA2 ORA JMP2r ORA2 ORA JMP2r
( -- comparisons ) ( -- comparisons )
( x < y ) ( x < y )
@lt32 ( x** y** -> bool^ ) @u32-lt ( x** y** -> bool^ )
ROT2 SWP2 LTH2 ?{ LTH2 JMP2r } GTH2 #00 EQU JMP2r ROT2 SWP2 LTH2 ?{ LTH2 JMP2r } GTH2 #00 EQU JMP2r
( x <= y ) ( x <= y )
@lteq32 ( x** y** -> bool^ ) @u32-lteq ( x** y** -> bool^ )
ROT2 SWP2 GTH2 ?{ GTH2 #00 EQU JMP2r } LTH2 JMP2r ROT2 SWP2 GTH2 ?{ GTH2 #00 EQU JMP2r } LTH2 JMP2r
( x > y ) ( x > y )
@gt32 ( x** y** -> bool^ ) @u32-gt ( x** y** -> bool^ )
ROT2 SWP2 GTH2 ?{ GTH2 JMP2r } LTH2 #00 EQU JMP2r ROT2 SWP2 GTH2 ?{ GTH2 JMP2r } LTH2 #00 EQU JMP2r
( x > y ) ( x > y )
@gteq32 ( x** y** -> bool^ ) @u32-gteq ( x** y** -> bool^ )
ROT2 SWP2 LTH2 ?{ LTH2 #00 EQU JMP2r } GTH2 JMP2r ROT2 SWP2 LTH2 ?{ LTH2 #00 EQU JMP2r } GTH2 JMP2r
( -- bitwise operations ) ( -- bitwise operations )
( x & y ) ( x & y )
@and32 ( xhi* xlo* yhi* ylo* -> xhi&yhi* xlo&ylo* ) @u32-and ( xhi* xlo* yhi* ylo* -> xhi&yhi* xlo&ylo* )
ROT2 AND2 STH2 AND2 STH2r JMP2r ROT2 AND2 STH2 AND2 STH2r JMP2r
( x | y ) ( x | y )
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) @u32-or ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 ORA2 STH2 ORA2 STH2r JMP2r ROT2 ORA2 STH2 ORA2 STH2r JMP2r
( x ^ y ) ( x ^ y )
@xor32 ( xhi* xlo* yhi* ylo* -> xhi^yhi* xlo^ylo* ) @u32-xor ( xhi* xlo* yhi* ylo* -> xhi^yhi* xlo^ylo* )
ROT2 EOR2 STH2 EOR2 STH2r JMP2r ROT2 EOR2 STH2 EOR2 STH2r JMP2r
( ~x ) ( ~x )
@complement32 ( x** -> ~xhi* ~xlo* ) @u32-complement ( x** -> ~xhi* ~xlo* )
SWP2 #ffff EOR2 SWP2 #ffff EOR2 JMP2r SWP2 #ffff EOR2 SWP2 #ffff EOR2 JMP2r
( -- bit-shifting ) ( -- bit-shifting )
( x >> n ) ( x >> n )
@rshift32 ( x** n^ -> x>>n ) @u32-rshift ( x** n^ -> x>>n )
DUP #08 LTH ?shift32-0 ( x n ) DUP #08 LTH ?u32-shift-0 ( x n )
DUP #10 LTH ?rshift32-1 ( x n ) DUP #10 LTH ?u32-rshift-1 ( x n )
DUP #18 LTH ?rshift32-2 ( x n ) DUP #18 LTH ?u32-rshift-2 ( x n )
!rshift32-3 ( x n ) !u32-rshift-3 ( x n )
( shift by 0-7 bits; used by both lshift and rshift ) ( shift by 0-7 bits; used by both lshift and rshift )
@shift32-0 ( x** n^ -> x>>n ) @u32-shift-0 ( x** n^ -> x>>n )
STH DUP2 STHkr SFT2 ,&z2 STR2 STH DUP2 STHkr SFT2 ,&z2 STR2
POP DUP2 STHkr SFT2 ,&z2 LDR ORA ,&z2 STR ,&z1 STR POP DUP2 STHkr SFT2 ,&z2 LDR ORA ,&z2 STR ,&z1 STR
POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR
LIT [ &z1 $1 ] LIT2 [ &z2 $2 ] JMP2r LIT [ &z1 $1 ] LIT2 [ &z2 $2 ] JMP2r
( shift right by 8-15 bits ) ( shift right by 8-15 bits )
@rshift32-1 ( x** n^ -> x>>n ) @u32-rshift-1 ( x** n^ -> x>>n )
#08 SUB STH ( stash [n>>8] ) #08 SUB STH ( stash [n>>8] )
POP DUP2 STHkr SFT2 ,&z2 STR2 POP DUP2 STHkr SFT2 ,&z2 STR2
POP STHr SFT2 ,&z2 LDR ORA ,&z2 STR POP STHr SFT2 ,&z2 LDR ORA ,&z2 STR
#00 SWP LIT2 [ &z2 $2 ] JMP2r #00 SWP LIT2 [ &z2 $2 ] JMP2r
( shift right by 16-23 bits ) ( shift right by 16-23 bits )
@rshift32-2 ( x** n^ -> x>>n ) @u32-rshift-2 ( x** n^ -> x>>n )
#10 SUB STH ( stash [n>>16] ) #10 SUB STH ( stash [n>>16] )
POP2 STHr SFT2 #0000 SWP2 JMP2r POP2 STHr SFT2 #0000 SWP2 JMP2r
( shift right by 16-23 bits ) ( shift right by 16-23 bits )
@rshift32-3 ( x** n^ -> x>>n ) @u32-rshift-3 ( x** n^ -> x>>n )
#18 SUB STH ( stash [n>>24] ) #18 SUB STH ( stash [n>>24] )
POP2 POP STH SWPr SFTr #00 #0000 STHr JMP2r POP2 POP STH SWPr SFTr #00 #0000 STHr JMP2r
( x << n ) ( x << n )
@lshift32 ( x** n^ -> x<<n ) @u32-lshift ( x** n^ -> x<<n )
DUP #08 LTH ?lshift32-0 ( x n ) DUP #08 LTH ?u32-lshift-0 ( x n )
DUP #10 LTH ?lshift32-1 ( x n ) DUP #10 LTH ?u32-lshift-1 ( x n )
DUP #18 LTH ?lshift32-2 ( x n ) DUP #18 LTH ?u32-lshift-2 ( x n )
!lshift32-3 ( x n ) !u32-lshift-3 ( x n )
( shift left by 0-7 bits ) ( shift left by 0-7 bits )
@lshift32-0 ( x** n^ -> x<<n ) @u32-lshift-0 ( x** n^ -> x<<n )
#40 SFT !shift32-0 #40 SFT !u32-shift-0
( shift left by 8-15 bits ) ( shift left by 8-15 bits )
@lshift32-1 ( x** n^ -> x<<n ) @u32-lshift-1 ( x** n^ -> x<<n )
#08 SUB #40 SFT STH ( stash [n-8]<<4 ) #08 SUB #40 SFT STH ( stash [n-8]<<4 )
DUP2 STHkr SFT2 ,&z1 STR2 DUP2 STHkr SFT2 ,&z1 STR2
POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR
NIP LIT2 [ &z1 $1 &z2 $1 ] #00 JMP2r NIP LIT2 [ &z1 $1 &z2 $1 ] #00 JMP2r
( shift left by 16-23 bits ) ( shift left by 16-23 bits )
@lshift32-2 ( x** n^ -> x<<n ) @u32-lshift-2 ( x** n^ -> x<<n )
#10 SUB #40 SFT STH ( stash [n-16]<<4 ) #10 SUB #40 SFT STH ( stash [n-16]<<4 )
NIP2 STHr SFT2 #0000 JMP2r NIP2 STHr SFT2 #0000 JMP2r
( shift left by 24-31 bits ) ( shift left by 24-31 bits )
@lshift32-3 ( x** n^ -> x<<n ) @u32-lshift-3 ( x** n^ -> x<<n )
#18 SUB #40 SFT ( stash [n-24]<<4 ) #18 SUB #40 SFT ( stash [n-24]<<4 )
SFT NIP2 NIP #0000 #00 JMP2r SFT NIP2 NIP #0000 #00 JMP2r
( -- arithmetic ) ( -- arithmetic )
( x + y ) ( x + y )
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* ) @u32-add ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
ROT2 STH2k ADD2 STH2k ROT2 ROT2 GTH2r #00 STHr ADD2 ADD2 SWP2 JMP2r ROT2 STH2k ADD2 STH2k ROT2 ROT2 GTH2r #00 STHr ADD2 ADD2 SWP2 JMP2r
( -x ) ( -x )
@negate32 ( x** -> -x** ) @u32-negate ( x** -> -x** )
complement32 INC2 ORAk ?{ SWP2 INC2 SWP2 } JMP2r u32-complement INC2 ORAk ?{ SWP2 INC2 SWP2 } JMP2r
( x - y ) ( x - y )
@sub32 ( x** y** -> z** ) @u32-sub ( x** y** -> z** )
ROT2 STH2k SWP2 SUB2 STH2k ROT2 ROT2 LTH2r #00 STHr ADD2 SUB2 SWP2 JMP2r ROT2 STH2k SWP2 SUB2 STH2k ROT2 ROT2 LTH2r #00 STHr ADD2 SUB2 SWP2 JMP2r
( 16-bit multiplication ) ( 16-bit multiplication )
@mul16 ( x* y* -> z** ) @u32-mul16 ( x* y* -> z** )
,&y1 STR ,&y0 STR ( save ylo, yhi ) ,&y1 STR ,&y0 STR ( save ylo, yhi )
,&x1 STR ,&x0 STR ( save xlo, xhi ) ,&x1 STR ,&x0 STR ( save xlo, xhi )
#0000 ,&z1 STR ,&w0 STR ( reset z1 and w0 ) #0000 ,&z1 STR ,&w0 STR ( reset z1 and w0 )
@ -209,12 +209,12 @@
( add z and a<<8 ) ( add z and a<<8 )
#00 LIT2 [ &z1 $1 &z2 $1 ] LIT [ &z3 $1 ] #00 LIT2 [ &z1 $1 &z2 $1 ] LIT [ &z3 $1 ]
LIT2 [ &w0 $1 &w1 $1 ] LIT [ &w2 $1 ] #00 LIT2 [ &w0 $1 &w1 $1 ] LIT [ &w2 $1 ] #00
!add32 !u32-add
( x * y ) ( x * y )
@mul32 ( x** y** -> z** ) @u32-mul ( x** y** -> z** )
ROT2k ( x0* x1* y0* y1* y0* y1* x1* ) ROT2k ( x0* x1* y0* y1* y0* y1* x1* )
mul16 ,&z1 STR2 ,&z0 STR2 POP2 ( x0* x1* y0* y1* ; sum = [x1*y1] ) u32-mul16 ,&z1 STR2 ,&z0 STR2 POP2 ( x0* x1* y0* y1* ; sum = [x1*y1] )
STH2 ROT2 STH2 ( x1* y0* [y1* x0*] ) STH2 ROT2 STH2 ( x1* y0* [y1* x0*] )
MUL2r MUL2 STH2r ADD2 ( x1*y0+y1*x0* ) MUL2r MUL2 STH2r ADD2 ( x1*y0+y1*x0* )
( [x0*y0]<<32 will completely overflow ) ( [x0*y0]<<32 will completely overflow )
@ -222,62 +222,62 @@
LIT2 [ &z1 $2 ] JMP2r LIT2 [ &z1 $2 ] JMP2r
( x / y ) ( x / y )
@div32 ( x** y** -> q** ) @u32-div ( x** y** -> q** )
z_divmod32 ;z_divmod32/quo0 LDA2 ;z_divmod32/quo1 LDA2 JMP2r z_u32-divmod ;z_u32-divmod/quo0 LDA2 ;z_u32-divmod/quo1 LDA2 JMP2r
( x % y ) ( x % y )
@mod32 ( x** y** -> r** ) @u32-mod ( x** y** -> r** )
z_divmod32 ;z_divmod32/rem0 LDA2 ;z_divmod32/rem1 LDA2 JMP2r z_u32-divmod ;z_u32-divmod/rem0 LDA2 ;z_u32-divmod/rem1 LDA2 JMP2r
( x / y, x % y ) ( x / y, x % y )
@divmod32 ( x** y** -> q** r** ) @u32-divmod ( x** y** -> q** r** )
z_divmod32 z_u32-divmod
;z_divmod32/quo0 LDA2 ;z_divmod32/quo1 LDA2 ;z_u32-divmod/quo0 LDA2 ;z_u32-divmod/quo1 LDA2
;z_divmod32/rem0 LDA2 ;z_divmod32/rem1 LDA2 ;z_u32-divmod/rem0 LDA2 ;z_u32-divmod/rem1 LDA2
JMP2r JMP2r
( private: calculate and store x / y and x % y ) ( private: calculate and store x / y and x % y )
@z_divmod32 ( x** y** -> ) @z_u32-divmod ( x** y** -> )
( ; store y and x for repeated use ) ( ; store y and x for repeated use )
#0000 DUP2 ,&quo0 STR2 ,&quo1 STR2 ( x** y** ; quo<-0 ) #0000 DUP2 ,&quo0 STR2 ,&quo1 STR2 ( x** y** ; quo<-0 )
STH2k ,&div1 STR2 STH2k ,&div0 STR2 ( x** [ylo* yhi*] ; div<-y ) STH2k ,&div1 STR2 STH2k ,&div0 STR2 ( x** [ylo* yhi*] ; div<-y )
OVR2 OVR2 ,&rem1 STR2 ,&rem0 STR2 ( x** [ylo* yhi*] ; rem<-x ) OVR2 OVR2 ,&rem1 STR2 ,&rem0 STR2 ( x** [ylo* yhi*] ; rem<-x )
OVR2 OVR2 STH2r STH2r ( x** x** y** ) OVR2 OVR2 STH2r STH2r ( x** x** y** )
OVR2 OVR2 STH2 STH2 ( x** x** y** [ylo* yhi*] ) OVR2 OVR2 STH2 STH2 ( x** x** y** [ylo* yhi*] )
gteq32 ?{ POP2 POP2 POP2r POP2r JMP2r } ( x** [ylo* yhi*] ; return if x < y ) u32-gteq ?{ POP2 POP2 POP2r POP2r JMP2r } ( x** [ylo* yhi*] ; return if x < y )
( ; bitcount[x] - bitcount[y] determines largest multiple of y to try ) ( ; bitcount[x] - bitcount[y] determines largest multiple of y to try )
bitcount32 STH2r STH2r bitcount32 SUB ( shift=rbits-dits^ ) u32-bitcount STH2r STH2r u32-bitcount SUB ( shift=rbits-dits^ )
#00 DUP2 ( shift^ 0^ shift^ 0^ ) #00 DUP2 ( shift^ 0^ shift^ 0^ )
#0000 INC2k ROT2 POP ( shift^ 0^ 0* 1* shift^ ) #0000 INC2k ROT2 POP ( shift^ 0^ 0* 1* shift^ )
lshift32 ,&cur1 STR2 ,&cur0 STR2 ( shift^ 0^ ; cur<-1<<shift ) u32-lshift ,&cur1 STR2 ,&cur0 STR2 ( shift^ 0^ ; cur<-1<<shift )
,&div0 LDR2 ,&div1 LDR2 ROT2 POP ( div** shift^ ) ,&div0 LDR2 ,&div1 LDR2 ROT2 POP ( div** shift^ )
lshift32 ,&div1 STR2 ,&div0 STR2 ( ; div<-div<<shift ) u32-lshift ,&div1 STR2 ,&div0 STR2 ( ; div<-div<<shift )
&loop &loop
( ; if rem >= cur [current divisor], we can subtract it and add to quotient ) ( ; if rem >= cur [current divisor], we can subtract it and add to quotient )
( ; otherwise, skip that iteration and reduce cur. ) ( ; otherwise, skip that iteration and reduce cur. )
LIT2 [ &rem0 $2 ] LIT2 [ &rem1 $2 ] ,&div0 LDR2 ,&div1 LDR2 LIT2 [ &rem0 $2 ] LIT2 [ &rem1 $2 ] ,&div0 LDR2 ,&div1 LDR2
lt32 ?{ u32-lt ?{
( ; since rem >= div, we have found a multiple of y that divides x ) ( ; since rem >= div, we have found a multiple of y that divides x )
,&rem0 LDR2 ,&rem1 LDR2 ( rem** ) ,&rem0 LDR2 ,&rem1 LDR2 ( rem** )
LIT2 [ &div0 $2 ] LIT2 [ &div1 $2 ] ( rem** div** ) LIT2 [ &div0 $2 ] LIT2 [ &div1 $2 ] ( rem** div** )
sub32 ,&rem1 STR2 ,&rem0 STR2 ( ; rem<-rem-div** ) u32-sub ,&rem1 STR2 ,&rem0 STR2 ( ; rem<-rem-div** )
LIT2 [ &quo0 $2 ] LIT2 [ &quo1 $2 ] ( quo** ) LIT2 [ &quo0 $2 ] LIT2 [ &quo1 $2 ] ( quo** )
LIT2 [ &cur0 $2 ] LIT2 [ &cur1 $2 ] ( quo** cur** ) LIT2 [ &cur0 $2 ] LIT2 [ &cur1 $2 ] ( quo** cur** )
add32 ,&quo1 STR2 ,&quo0 STR2 ( ; quo<-quo+cur** ) u32-add ,&quo1 STR2 ,&quo0 STR2 ( ; quo<-quo+cur** )
} }
,&div0 LDR2 ,&div1 LDR2 #01 rshift32 ( div>>1** ) ,&div0 LDR2 ,&div1 LDR2 #01 u32-rshift ( div>>1** )
,&div1 STR2 ,&div0 STR2 ( ; div<-div>>1 ) ,&div1 STR2 ,&div0 STR2 ( ; div<-div>>1 )
,&cur0 LDR2 ,&cur1 LDR2 #01 rshift32 ( cur>>1** ) ,&cur0 LDR2 ,&cur1 LDR2 #01 u32-rshift ( cur>>1** )
OVR2 OVR2 ,&cur1 STR2 ,&cur0 STR2 ( cur>>1** ; cur<-cur>>1 ) OVR2 OVR2 ,&cur1 STR2 ,&cur0 STR2 ( cur>>1** ; cur<-cur>>1 )
non-zero32 ?&loop JMP2r ( ; loop if cur>0, else we're done ) u32-non-zero ?&loop JMP2r ( ; loop if cur>0, else we're done )
( greatest common divisor - euclidean algorithm ) ( greatest common divisor - euclidean algorithm )
@gcd32 ( x** y** -> z** ) @u32-gcd ( x** y** -> z** )
&loop OVR2 OVR2 is-zero32 ?{ ( x** y** ) &loop OVR2 OVR2 u32-is-zero ?{ ( x** y** )
OVR2 OVR2 STH2 STH2 ( x** y** [y**] ) OVR2 OVR2 STH2 STH2 ( x** y** [y**] )
mod32 ( r=x%y** [y**] ) u32-mod ( r=x%y** [y**] )
STH2r ROT2 ROT2 ( yhi* rhi* rlo* [ylo*] ) STH2r ROT2 ROT2 ( yhi* rhi* rlo* [ylo*] )
STH2r ROT2 ROT2 !&loop ( y** r** ) STH2r ROT2 ROT2 !&loop ( y** r** )
} POP2 POP2 JMP2r ( z** ) } POP2 POP2 JMP2r ( z** )