Passing tests

This commit is contained in:
~d6 2024-09-09 22:43:00 -04:00
parent dd7a610c83
commit 62cc1ffd12
2 changed files with 110 additions and 47 deletions

View File

@ -7,35 +7,6 @@
%DENOM16 { #03e8 } %DENOM16 { #03e8 }
%DENOM32 { #0000 #03e8 } %DENOM32 { #0000 #03e8 }
|0100
( test cases -- compare the two 32-bit values on wst )
#0000 #03e8 ( a=1 )
#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-eq ( x/** y/** -> bool^ ) !u32-eq
@x32-ne ( x/** y/** -> bool^ ) !u32-ne @x32-ne ( x/** y/** -> bool^ ) !u32-ne
@ -53,6 +24,15 @@
@x32-from-u32 ( x** -> x/** ) @x32-from-u32 ( x** -> x/** )
DENOM32 !u32-mul DENOM32 !u32-mul
@x32-signed-op ( x** y** f* -> f[x,y]** )
STH2 LIT2r 0001 ( x** y** [f* 0^ 1^] )
OVR2 #8000 LTH2 ?{ u32-negate SWPr } ( x** y** [f* ab*] )
ROT2 STH2 ROT2 STH2r ( y** x** [f* ab*] )
OVR2 #8000 LTH2 ?{ u32-negate SWPr } ( y** x** [f* cd*] )
ROT2 STH2 ROT2 STH2r SWP2r ( x** y** [cd* f*] )
STH2r JSR2 ( f[x,y]** [cd*] )
NIPr STHr ?{ u32-negate } JMP2r ( z** )
@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
@ -86,6 +66,9 @@
@x32-gteq ( x/** y/** -> bool^ ) @x32-gteq ( x/** y/** -> bool^ )
x32-prepare-cmp NEQk ?{ POP2 !u32-gteq } GTH STH POP8 STHr JMP2r x32-prepare-cmp NEQk ?{ POP2 !u32-gteq } GTH STH POP8 STHr JMP2r
( TODO: support saturation at +/- infinity )
( TODO: support signed operations )
@x32-add ( x/** y/** -> z/** ) @x32-add ( x/** y/** -> z/** )
!u32-add !u32-add
@ -106,26 +89,46 @@
@x32-scaled-div32 ( x/** y** -> z/** ) @x32-scaled-div32 ( x/** y** -> z/** )
!u32-div !u32-div
( [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*] ) ;x32-mul-unsigned !x32-signed-op
u32-divmod ( q** r** [ylo* yhi*] )
STH2kr OVR2r STH2r u32-mul ( q** ry** [ylo* yhi*] ) ( [x * y]/1000 = floor[x/1000] + [[x%1000]*y]/1000 )
DENOM32 u32-div ( q** ry1000** [ylo* yhi*] ) @x32-mul-unsigned ( x/** y/** -> z/** )
ROT2 STH2 ROT2 STH2r ( ry1000** q** [ylo* yhi*] ) STH2 STH2 DENOM32 ( x** 1000** [ylo* yhi*] )
STH2r STH2r u32-mul ( ry1000** qy** ) u32-divmod ( q=x/1000** r=x%1000** [ylo* yhi*] )
u32-add ( qy+ry/1000** ) STH2kr OVR2r STH2r u32-mul ( q** ry** [ylo* yhi*] )
JMP2r ( z/** ) DENOM32 u32-divmod ( q** rq=ry/1000** rr=ry%1000** [ylo* yhi*] )
NIP2 ,&r1 STR2 ( q** rq** [ylo* yhi*] ; <-rr1 )
ROT2 STH2 ROT2 STH2r ( ry/1000** q** [ylo* yhi*] )
STH2r STH2r u32-mul ( ry/1000** qy** )
u32-add ( z=qy+ry/1000** )
DUP2 #0001 AND2 STH2 ( z** [odd*] )
#0000 LIT2 [ &r1 $2 ] ( z** rr** [odd*] )
STH2r ADD2 #01f3 ADD2 ( z** rr+odd+499** )
DENOM32 u32-div ( z** b=rr+odd+499/1000** )
!u32-add ( z+b** )
@x32-div ( x/** y/** -> z/** )
;x32-div-unsigned !x32-signed-op
( [x * 1000]/y = floor[x/y]*1000 + [[x%y]*1000]/y ) ( [x * 1000]/y = floor[x/y]*1000 + [[x%y]*1000]/y )
@x32-div ( x/** y/** -> z/** ) @x32-div-unsigned ( x/** y/** -> z/** )
STH2k OVR2 STH2 ( x/** y/** [ylo* yhi*] ) STH2k OVR2 STH2 ( x/** y/** [ylo* yhi*] )
u32-divmod u32-divmod ( q=x/y** r=x%y** [ylo* yhi*] )
DENOM32 u32-mul ( q** r1000** [ylo* yhi*] ) DENOM32 u32-mul ( q** r1000** [ylo* yhi*] )
STH2r STH2r u32-div ( q** r1000/y** [ylo* yhi*] ) STH2kr OVR2r STH2r u32-divmod ( q** rq** rr** [ylo* yhi*] )
ROT2 STH2 ROT2 STH2r ( r1000/y** q** [ylo* yhi*] ) ,&r1 STR2 ,&r0 STR2 ( q** rq** ; <-rr0 <-rr1 [ylo* yhi*] )
DENOM32 u32-mul ( r1000/y** q1000** ) ROT2 STH2 ROT2 STH2r ( rq** q** [ylo* yhi*] )
u32-add ( q+r1000/y** ) DENOM32 u32-mul ( rq** q1000** [ylo* yhi*] )
JMP2r ( z/** ) u32-add ( z=rq+q1000** [ylo* yhi*] )
DUP ,&e STR ( z** ; e<-z3^ [ylo* yhi*] )
LIT2 [ &r0 $2 ] LIT2 [ &r1 $2 ] ( z** rr** [ylo* yhi*] )
LIT [ &e $1 ] #01 AND ( z** rr** e^ )
#00 SWP #0000 SWP2 ( z** rr** e** [ylo* yhi*] )
u32-add ( z** w=rr+e** [ylo* yhi*] )
STH2kr OVR2r STH2r ( z** w** y** [ylo* yhi*] )
#0000 #0001 u32-sub ( z** w** y-1** [ylo* yhi*] )
#01 u32-rshift u32-add ( z** v=w+y-1/2** [ylo* yhi*] )
STH2r STH2r u32-div !u32-add ( z+v/y** )
~math32.tal ~math32.tal

60
test-fix32.tal Normal file
View File

@ -0,0 +1,60 @@
( test-fix32.tal )
( )
( methods for testing math32 and emitting output )
( TODO: consider rounding modes. currently we always round toward zero )
( program )
|0100
#0000 #03e8 #0000 #07d0 LIT "+ ;x32-add #0000 #0bb8 test-binop ( 1 + 2 = 3 )
#0a18 DEO
#0000 #03e8 #0000 #03e8 LIT "* ;x32-mul #0000 #03e8 test-binop ( 1 * 1 = 1 )
#0000 #07d0 #0000 #0bb8 LIT "* ;x32-mul #0000 #1770 test-binop ( 2 * 3 = 6 )
#0000 #4a38 #0000 #6978 LIT "* ;x32-mul #0007 #d3d8 test-binop ( 19 * 27 = 513 )
#0000 #0064 #0000 #0064 LIT "* ;x32-mul #0000 #000a test-binop ( 0.1 * 0.1 = 0.01 )
#0000 #01f4 #0000 #0001 LIT "* ;x32-mul #0000 #0000 test-binop ( 0.5 * 0.001 = 0.0 )
#0000 #01f4 #0000 #0003 LIT "* ;x32-mul #0000 #0002 test-binop ( 0.5 * 0.003 = 0.002 )
#0000 #01f4 #0000 #0005 LIT "* ;x32-mul #0000 #0002 test-binop ( 0.5 * 0.005 = 0.002 )
#0000 #01f4 #0000 #0007 LIT "* ;x32-mul #0000 #0004 test-binop ( 0.5 * 0.007 = 0.004 )
#0000 #01f4 #0000 #0009 LIT "* ;x32-mul #0000 #0004 test-binop ( 0.5 * 0.009 = 0.004 )
#0000 #0bb8 #ffff #f830 LIT "* ;x32-mul #ffff #e890 test-binop ( 3 * -2 = -6 )
#ffff #fc18 #ffff #fc18 LIT "* ;x32-mul #0000 #03e8 test-binop ( -1 * -1 = 1 )
#0a18 DEO
#0000 #1d4c #0000 #05dc LIT "/ ;x32-div #0000 #1388 test-binop ( 7.5 / 1.5 = 5.0 )
#0000 #03e8 #0000 #0001 LIT "/ ;x32-div #000f #4240 test-binop ( 1.0 / 0.001 = 1000.0 )
#0000 #03e8 #0000 #0bb8 LIT "/ ;x32-div #0000 #014d test-binop ( 1.0 / 3.0 = 0.333 )
#0000 #07d0 #0000 #0bb8 LIT "/ ;x32-div #0000 #029b test-binop ( 2.0 / 3.0 = 0.667 )
#ffff #adf8 #0000 #1b58 LIT "/ ;x32-div #ffff #f448 test-binop ( -21.0 / 7.0 = -3.0 )
#0000 #0003 #0000 #07d0 LIT "/ ;x32-div #0000 #0002 test-binop ( 0.003 / 2 = 0.002 )
#0000 #0005 #0000 #07d0 LIT "/ ;x32-div #0000 #0002 test-binop ( 0.005 / 2 = 0.002 )
#0000 #0007 #0000 #07d0 LIT "/ ;x32-div #0000 #0004 test-binop ( 0.007 / 2 = 0.004 )
#0000 #0009 #0000 #07d0 LIT "/ ;x32-div #0000 #0004 test-binop ( 0.009 / 2 = 0.004 )
#0a18 DEO
#800f DEO BRK
~fix32.tal
@test-binop ( x** y** op^ f* z** -> x** y** )
STH2 STH2 ,&f STR2 ,&op STR ( x** y** [z1* z0*] )
STH2 STH2 OVR2 OVR2 emit/long ( x** [z1* z0* y1* z0*] ; emit x )
#2018 DEO ( x** [z1* z0* y1* y0*] ; emit space )
LIT [ &op $1 ] #18 DEO ( x** [z1* z0* y1* y0*] ; emit operator symbol )
#2018 DEO ( x** [z1* z0* y1* y0*] ; emit space )
STH2r STH2r OVR2 OVR2 emit/long ( x** y** [z1* z0*] ; emit y )
#2018 DEO ( x** y** [z1* z0*] ; emit space )
LIT "= #18 DEO ( x** y** [z1* z0*] ; emit = )
#2018 DEO ( x** y** [z1* z0*] ; emit space )
LIT2 [ &f $2 ] JSR2 ( f[x,y]** [z1* z0*] )
emit/long ( [z1* z0*] ; emit f[x,y] )
STH2r STH2r #2018 DEO ( z** ; emit space )
LIT "[ #18 DEO ( z** ; emit [ )
emit/long ( ; emit z )
LIT "] #18 DEO ( ; emit ] )
#0a18 DEO JMP2r ( ; emit newline )
@emit
&long SWP2 /short
&short SWP ,&byte JSR
&byte DUP #04 SFT ,&char JSR
&char #0f AND DUP #09 GTH #27 MUL ADD #30 ADD #18 DEO
JMP2r