This commit is contained in:
~d6 2021-12-27 15:09:49 -05:00
parent a8364516b3
commit e8b6990a9d
2 changed files with 30 additions and 40 deletions

View File

@ -5,7 +5,6 @@
%DEBUG { #ff #0e DEO } %DEBUG { #ff #0e DEO }
%RTN { JMP2r } %RTN { JMP2r }
%TOR { ROT ROT } ( a b c -> c a b ) %TOR { ROT ROT } ( a b c -> c a b )
%TOR2 { ROT2 ROT2 } %TOR2 { ROT2 ROT2 }
%POP4 { POP2 POP2 } %POP4 { POP2 POP2 }
@ -24,7 +23,7 @@
,&loop JMP ,&loop JMP
&done &done
POP ( n ) POP ( n )
RTN RTN
@bitcount16 ( x* -> n^ ) @bitcount16 ( x* -> n^ )
SWP ( xlo xhi ) SWP ( xlo xhi )
@ -35,7 +34,7 @@ RTN
RTN RTN
&hi-set &hi-set
SWP POP #08 ADD ( nhi+8 ) SWP POP #08 ADD ( nhi+8 )
RTN RTN
@bitcount32 ( x** -> n^ ) @bitcount32 ( x** -> n^ )
SWP2 ( xlo* xhi* ) SWP2 ( xlo* xhi* )
@ -45,27 +44,23 @@ RTN
TOR ;bitcount16 JSR2 ADD RTN ( nhi+nlo ) TOR ;bitcount16 JSR2 ADD RTN ( nhi+nlo )
&hi-set &hi-set
TOR POP2 #10 ADD ( nhi+16 ) TOR POP2 #10 ADD ( nhi+16 )
RTN RTN
( equality ) ( equality )
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ ) @eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 EQU2 #00 TOR2 ROT2 EQU2 #00 TOR2
EQU2 SWP POP AND EQU2 SWP POP AND RTN
RTN
@is-zero32 ( x** -> bool^ ) @is-zero32 ( x** -> bool^ )
ORA2 #0000 EQU2 ORA2 #0000 EQU2 RTN
RTN
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ ) @ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 NEQ2 #00 TOR2 ROT2 NEQ2 #00 TOR2
NEQ2 SWP POP ORA NEQ2 SWP POP ORA RTN
RTN
@non-zero32 ( x** -> bool^ ) @non-zero32 ( x** -> bool^ )
ORA2 #0000 NEQ2 ORA2 #0000 NEQ2 RTN
RTN
( comparisons ) ( comparisons )
@ -104,20 +99,16 @@ RTN
( bitwise operations ) ( bitwise operations )
@and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) @and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 AND2 TOR2 AND2 SWP2 ROT2 AND2 TOR2 AND2 SWP2 RTN
RTN
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) @or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 ORA2 TOR2 ORA2 SWP2 ROT2 ORA2 TOR2 ORA2 SWP2 RTN
RTN
@xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) @xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 EOR2 TOR2 EOR2 SWP2 ROT2 EOR2 TOR2 EOR2 SWP2 RTN
RTN
@complement32 ( x** -> ~x** ) @complement32 ( x** -> ~x** )
COMPLEMENT32 COMPLEMENT32 RTN
RTN
( bit shifting ) ( bit shifting )
@ -126,7 +117,7 @@ RTN
DUP #10 LTH ;right-shift1 JCN2 ( x n ) DUP #10 LTH ;right-shift1 JCN2 ( x n )
DUP #18 LTH ;right-shift2 JCN2 ( x n ) DUP #18 LTH ;right-shift2 JCN2 ( x n )
;right-shift3 JMP2 ( x n ) ;right-shift3 JMP2 ( x n )
RTN RTN
( shift right by 0-7 bits ) ( shift right by 0-7 bits )
@right-shift0 ( x** n^ -> x<<n ) @right-shift0 ( x** n^ -> x<<n )
@ -137,7 +128,7 @@ RTN
#00 ,&r LDR SFT2 ,&z1 LDR2 ORA2 ,&z1 STR2 ( write z1,z2 ) #00 ,&r LDR SFT2 ,&z1 LDR2 ORA2 ,&z1 STR2 ( write z1,z2 )
#00 ,&r LDR SFT2 ,&z0 LDR2 ORA2 ,&z0 STR2 ( write z0,z1 ) #00 ,&r LDR SFT2 ,&z0 LDR2 ORA2 ,&z0 STR2 ( write z0,z1 )
,&z0 LDR2 ,&z2 LDR2 ,&z0 LDR2 ,&z2 LDR2
RTN RTN
[ &r $1 &z0 $1 &z1 $1 &z2 $1 &z3 $1 ] [ &r $1 &z0 $1 &z1 $1 &z2 $1 &z3 $1 ]
( shift right by 8-15 bits ) ( shift right by 8-15 bits )
@ -149,7 +140,7 @@ RTN
#00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 ) #00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 )
#00 ,&r LDR SFT2 ,&z1 LDR2 ORA2 ,&z1 STR2 ( write z1,z2 ) #00 ,&r LDR SFT2 ,&z1 LDR2 ORA2 ,&z1 STR2 ( write z1,z2 )
#00 ,&z1 LDR ,&z2 LDR2 #00 ,&z1 LDR ,&z2 LDR2
RTN RTN
[ &r $1 &z1 $1 &z2 $1 &z3 $1 ] [ &r $1 &z1 $1 &z2 $1 &z3 $1 ]
( shift right by 16-23 bits ) ( shift right by 16-23 bits )
@ -160,7 +151,7 @@ RTN
,&r LDR SFT ,&z3 STR ( write z3 ) ,&r LDR SFT ,&z3 STR ( write z3 )
#00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 ) #00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 )
#0000 ,&z2 LDR2 #0000 ,&z2 LDR2
RTN RTN
[ &r $1 &z2 $1 &z3 $1 ] [ &r $1 &z2 $1 &z3 $1 ]
( shift right by 16-23 bits ) ( shift right by 16-23 bits )
@ -168,7 +159,7 @@ RTN
#18 SUB ,&r STR ( n -> r ) #18 SUB ,&r STR ( n -> r )
POP2 POP #00 SWP #0000 SWP2 ( 00 00 00 x0 ) POP2 POP #00 SWP #0000 SWP2 ( 00 00 00 x0 )
,&r LDR SFT ,&r LDR SFT
RTN RTN
[ &r $1 ] [ &r $1 ]
@left-shift ( x** n^ -> x<<n ) @left-shift ( x** n^ -> x<<n )
@ -176,7 +167,7 @@ RTN
DUP #10 LTH ;left-shift1 JCN2 ( x n ) DUP #10 LTH ;left-shift1 JCN2 ( x n )
DUP #18 LTH ;left-shift2 JCN2 ( x n ) DUP #18 LTH ;left-shift2 JCN2 ( x n )
;left-shift3 JMP2 ( x n ) ;left-shift3 JMP2 ( x n )
RTN RTN
( shift left by 0-7 bits ) ( shift left by 0-7 bits )
@left-shift0 ( x** n^ -> x<<n ) @left-shift0 ( x** n^ -> x<<n )
@ -196,7 +187,7 @@ RTN
,&z2 LDR2 ORA2 ,&z2 STR2 ( ) ,&z2 LDR2 ORA2 ,&z2 STR2 ( )
,&z0 LDR2 ,&z2 LDR2 ,&z0 LDR2 ,&z2 LDR2
RTN RTN
[ &r $1 &z0 $1 &z1 $1 &z2 $2 ] [ &r $1 &z0 $1 &z1 $1 &z2 $2 ]
( shift left by 8-15 bits ) ( shift left by 8-15 bits )
@ -214,7 +205,7 @@ RTN
,&z1 LDR2 ORA2 ,&z1 STR2 ( ) ,&z1 LDR2 ORA2 ,&z1 STR2 ( )
,&z0 LDR2 ,&z2 LDR #00 ,&z0 LDR2 ,&z2 LDR #00
RTN RTN
[ &r $1 &z0 $1 &z1 $1 &z2 $1 ] [ &r $1 &z0 $1 &z1 $1 &z2 $1 ]
( shift left by 16-23 bits ) ( shift left by 16-23 bits )
@ -229,7 +220,7 @@ RTN
,&z0 LDR2 ORA2 ,&z0 STR2 ( ) ,&z0 LDR2 ORA2 ,&z0 STR2 ( )
,&z0 LDR2 #0000 ,&z0 LDR2 #0000
RTN RTN
[ &r $1 &z0 $2 ] [ &r $1 &z0 $2 ]
( shift left by 24-31 bits ) ( shift left by 24-31 bits )
@ -237,7 +228,7 @@ RTN
#18 SUB #10 MUL ( x0 x1 x2 x3 r=[n-24]<<4 ) #18 SUB #10 MUL ( x0 x1 x2 x3 r=[n-24]<<4 )
SFT ( x0 x1 x2 x3<<r ) SFT ( x0 x1 x2 x3<<r )
SWP2 POP2 SWP POP #0000 #00 SWP2 POP2 SWP POP #0000 #00
RTN RTN
( arithmetic ) ( arithmetic )
@ -263,7 +254,7 @@ RTN
( load zhi,zlo ) ( load zhi,zlo )
,&z0 LDR2 ,&z2 LDR2 ,&z0 LDR2 ,&z2 LDR2
RTN RTN
( registers for add32 ) ( registers for add32 )
[ &x0 $1 &x1 $1 &x2 $1 &x3 $1 ] [ &x0 $1 &x1 $1 &x2 $1 &x3 $1 ]
[ &y0 $1 &y1 $1 &y2 $1 &y3 $1 ] [ &y0 $1 &y1 $1 &y2 $1 &y3 $1 ]
@ -276,11 +267,10 @@ RTN
,&done JCN ( xlo non-zero => don't inc hi ) ,&done JCN ( xlo non-zero => don't inc hi )
SWP2 INC2 SWP2 ( -xhi -xlo ) SWP2 INC2 SWP2 ( -xhi -xlo )
&done &done
RTN RTN
@sub32 ( x** y** -> z** ) @sub32 ( x** y** -> z** )
;negate32 JSR2 ;add32 JSR2 ;negate32 JSR2 ;add32 JSR2 RTN
RTN
@mul16 ( x* y* -> z** ) @mul16 ( x* y* -> z** )
,&y1 STR ,&y0 STR ( save ylo, yhi ) ,&y1 STR ,&y0 STR ( save ylo, yhi )
@ -304,7 +294,7 @@ RTN
#00 ,&z0 LDR2 ,&z2 LDR #00 ,&z0 LDR2 ,&z2 LDR
,&a0 LDR2 ,&a2 LDR #00 ,&a0 LDR2 ,&a2 LDR #00
;add32 JSR2 ;add32 JSR2
RTN RTN
[ &x0 $1 &x1 $1 ] [ &x0 $1 &x1 $1 ]
[ &y0 $1 &y1 $1 ] [ &y0 $1 &y1 $1 ]
[ &z0 $1 &z1 $1 &z2 $1 ] [ &z0 $1 &z1 $1 &z2 $1 ]
@ -321,7 +311,7 @@ RTN
( [x0*y0]<<32 will completely overflow ) ( [x0*y0]<<32 will completely overflow )
ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 ) ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 )
,&z1 LDR2 ,&z1 LDR2
RTN RTN
[ &x0 $2 &x1 $2 ] [ &x0 $2 &x1 $2 ]
[ &y0 $2 &y1 $2 ] [ &y0 $2 &y1 $2 ]
[ &z0 $2 &z1 $2 ] [ &z0 $2 &z1 $2 ]
@ -370,7 +360,7 @@ RTN
,&cur0 LDR2 ,&cur1 LDR2 #01 ;right-shift JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 ) ,&cur0 LDR2 ,&cur1 LDR2 #01 ;right-shift JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 )
,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done ) ,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done )
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 ) ,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
RTN RTN
[ &div0 $2 &div1 $2 [ &div0 $2 &div1 $2
&rem0 $2 &rem1 $2 &rem0 $2 &rem1 $2
&quo0 $2 &quo1 $2 &quo0 $2 &quo1 $2

View File

@ -3,7 +3,7 @@
from math import floor, log from math import floor, log
from os import environ from os import environ
from random import randint from random import randint
from subprocess import Popen, PIPE from subprocess import Popen, PIPE, run
u3 = {'sz': 1 << 3, 'fmt': b'%02x'} u3 = {'sz': 1 << 3, 'fmt': b'%02x'}
u5 = {'sz': 1 << 5, 'fmt': b'%02x'} u5 = {'sz': 1 << 5, 'fmt': b'%02x'}
@ -50,14 +50,14 @@ def test(p, trials, sym, args, out, f):
print('%s failed %d/%d trials (%r)' % (name, fails, trials, cases)) print('%s failed %d/%d trials (%r)' % (name, fails, trials, cases))
def pipe(): def pipe():
cli = environ['HOME'] + '/w/uxn/bin/uxncli' return Popen(['uxncli', 'run.rom'], stdin=PIPE, stdout=PIPE)
return Popen([cli, 'run.rom'], stdin=PIPE, stdout=PIPE)
def bitcount(x): def bitcount(x):
return floor(log(x, 2)) + 1 return floor(log(x, 2)) + 1
def main(): def main():
trials = 1000 trials = 1000
run(['uxnasm', 'test-math32.tal', 'run.rom'])
p = pipe() p = pipe()
test(p, trials, b'+', [('x', u32), ('y', u32)], u32, lambda x, y: x + y) test(p, trials, b'+', [('x', u32), ('y', u32)], u32, lambda x, y: x + y)
test(p, trials, b'-', [('x', u32), ('y', u32)], u32, lambda x, y: x - y) test(p, trials, b'-', [('x', u32), ('y', u32)], u32, lambda x, y: x - y)