clean up
This commit is contained in:
parent
a8364516b3
commit
e8b6990a9d
64
math32.tal
64
math32.tal
|
@ -5,7 +5,6 @@
|
|||
|
||||
%DEBUG { #ff #0e DEO }
|
||||
%RTN { JMP2r }
|
||||
|
||||
%TOR { ROT ROT } ( a b c -> c a b )
|
||||
%TOR2 { ROT2 ROT2 }
|
||||
%POP4 { POP2 POP2 }
|
||||
|
@ -24,7 +23,7 @@
|
|||
,&loop JMP
|
||||
&done
|
||||
POP ( n )
|
||||
RTN
|
||||
RTN
|
||||
|
||||
@bitcount16 ( x* -> n^ )
|
||||
SWP ( xlo xhi )
|
||||
|
@ -35,7 +34,7 @@ RTN
|
|||
RTN
|
||||
&hi-set
|
||||
SWP POP #08 ADD ( nhi+8 )
|
||||
RTN
|
||||
RTN
|
||||
|
||||
@bitcount32 ( x** -> n^ )
|
||||
SWP2 ( xlo* xhi* )
|
||||
|
@ -45,27 +44,23 @@ RTN
|
|||
TOR ;bitcount16 JSR2 ADD RTN ( nhi+nlo )
|
||||
&hi-set
|
||||
TOR POP2 #10 ADD ( nhi+16 )
|
||||
RTN
|
||||
RTN
|
||||
|
||||
( equality )
|
||||
|
||||
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
||||
ROT2 EQU2 #00 TOR2
|
||||
EQU2 SWP POP AND
|
||||
RTN
|
||||
EQU2 SWP POP AND RTN
|
||||
|
||||
@is-zero32 ( x** -> bool^ )
|
||||
ORA2 #0000 EQU2
|
||||
RTN
|
||||
ORA2 #0000 EQU2 RTN
|
||||
|
||||
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
|
||||
ROT2 NEQ2 #00 TOR2
|
||||
NEQ2 SWP POP ORA
|
||||
RTN
|
||||
NEQ2 SWP POP ORA RTN
|
||||
|
||||
@non-zero32 ( x** -> bool^ )
|
||||
ORA2 #0000 NEQ2
|
||||
RTN
|
||||
ORA2 #0000 NEQ2 RTN
|
||||
|
||||
( comparisons )
|
||||
|
||||
|
@ -104,20 +99,16 @@ RTN
|
|||
( bitwise operations )
|
||||
|
||||
@and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
||||
ROT2 AND2 TOR2 AND2 SWP2
|
||||
RTN
|
||||
ROT2 AND2 TOR2 AND2 SWP2 RTN
|
||||
|
||||
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
||||
ROT2 ORA2 TOR2 ORA2 SWP2
|
||||
RTN
|
||||
ROT2 ORA2 TOR2 ORA2 SWP2 RTN
|
||||
|
||||
@xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
|
||||
ROT2 EOR2 TOR2 EOR2 SWP2
|
||||
RTN
|
||||
ROT2 EOR2 TOR2 EOR2 SWP2 RTN
|
||||
|
||||
@complement32 ( x** -> ~x** )
|
||||
COMPLEMENT32
|
||||
RTN
|
||||
COMPLEMENT32 RTN
|
||||
|
||||
( bit shifting )
|
||||
|
||||
|
@ -126,7 +117,7 @@ RTN
|
|||
DUP #10 LTH ;right-shift1 JCN2 ( x n )
|
||||
DUP #18 LTH ;right-shift2 JCN2 ( x n )
|
||||
;right-shift3 JMP2 ( x n )
|
||||
RTN
|
||||
RTN
|
||||
|
||||
( shift right by 0-7 bits )
|
||||
@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 ,&z0 LDR2 ORA2 ,&z0 STR2 ( write z0,z1 )
|
||||
,&z0 LDR2 ,&z2 LDR2
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( 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 ,&z1 LDR2 ORA2 ,&z1 STR2 ( write z1,z2 )
|
||||
#00 ,&z1 LDR ,&z2 LDR2
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z1 $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( shift right by 16-23 bits )
|
||||
|
@ -160,7 +151,7 @@ RTN
|
|||
,&r LDR SFT ,&z3 STR ( write z3 )
|
||||
#00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 )
|
||||
#0000 ,&z2 LDR2
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( shift right by 16-23 bits )
|
||||
|
@ -168,7 +159,7 @@ RTN
|
|||
#18 SUB ,&r STR ( n -> r )
|
||||
POP2 POP #00 SWP #0000 SWP2 ( 00 00 00 x0 )
|
||||
,&r LDR SFT
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 ]
|
||||
|
||||
@left-shift ( x** n^ -> x<<n )
|
||||
|
@ -176,7 +167,7 @@ RTN
|
|||
DUP #10 LTH ;left-shift1 JCN2 ( x n )
|
||||
DUP #18 LTH ;left-shift2 JCN2 ( x n )
|
||||
;left-shift3 JMP2 ( x n )
|
||||
RTN
|
||||
RTN
|
||||
|
||||
( shift left by 0-7 bits )
|
||||
@left-shift0 ( x** n^ -> x<<n )
|
||||
|
@ -196,7 +187,7 @@ RTN
|
|||
,&z2 LDR2 ORA2 ,&z2 STR2 ( )
|
||||
|
||||
,&z0 LDR2 ,&z2 LDR2
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $2 ]
|
||||
|
||||
( shift left by 8-15 bits )
|
||||
|
@ -214,7 +205,7 @@ RTN
|
|||
,&z1 LDR2 ORA2 ,&z1 STR2 ( )
|
||||
|
||||
,&z0 LDR2 ,&z2 LDR #00
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $1 ]
|
||||
|
||||
( shift left by 16-23 bits )
|
||||
|
@ -229,7 +220,7 @@ RTN
|
|||
,&z0 LDR2 ORA2 ,&z0 STR2 ( )
|
||||
|
||||
,&z0 LDR2 #0000
|
||||
RTN
|
||||
RTN
|
||||
[ &r $1 &z0 $2 ]
|
||||
|
||||
( shift left by 24-31 bits )
|
||||
|
@ -237,7 +228,7 @@ RTN
|
|||
#18 SUB #10 MUL ( x0 x1 x2 x3 r=[n-24]<<4 )
|
||||
SFT ( x0 x1 x2 x3<<r )
|
||||
SWP2 POP2 SWP POP #0000 #00
|
||||
RTN
|
||||
RTN
|
||||
|
||||
( arithmetic )
|
||||
|
||||
|
@ -263,7 +254,7 @@ RTN
|
|||
|
||||
( load zhi,zlo )
|
||||
,&z0 LDR2 ,&z2 LDR2
|
||||
RTN
|
||||
RTN
|
||||
( registers for add32 )
|
||||
[ &x0 $1 &x1 $1 &x2 $1 &x3 $1 ]
|
||||
[ &y0 $1 &y1 $1 &y2 $1 &y3 $1 ]
|
||||
|
@ -276,11 +267,10 @@ RTN
|
|||
,&done JCN ( xlo non-zero => don't inc hi )
|
||||
SWP2 INC2 SWP2 ( -xhi -xlo )
|
||||
&done
|
||||
RTN
|
||||
RTN
|
||||
|
||||
@sub32 ( x** y** -> z** )
|
||||
;negate32 JSR2 ;add32 JSR2
|
||||
RTN
|
||||
;negate32 JSR2 ;add32 JSR2 RTN
|
||||
|
||||
@mul16 ( x* y* -> z** )
|
||||
,&y1 STR ,&y0 STR ( save ylo, yhi )
|
||||
|
@ -304,7 +294,7 @@ RTN
|
|||
#00 ,&z0 LDR2 ,&z2 LDR
|
||||
,&a0 LDR2 ,&a2 LDR #00
|
||||
;add32 JSR2
|
||||
RTN
|
||||
RTN
|
||||
[ &x0 $1 &x1 $1 ]
|
||||
[ &y0 $1 &y1 $1 ]
|
||||
[ &z0 $1 &z1 $1 &z2 $1 ]
|
||||
|
@ -321,7 +311,7 @@ RTN
|
|||
( [x0*y0]<<32 will completely overflow )
|
||||
ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 )
|
||||
,&z1 LDR2
|
||||
RTN
|
||||
RTN
|
||||
[ &x0 $2 &x1 $2 ]
|
||||
[ &y0 $2 &y1 $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 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done )
|
||||
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
|
||||
RTN
|
||||
RTN
|
||||
[ &div0 $2 &div1 $2
|
||||
&rem0 $2 &rem1 $2
|
||||
&quo0 $2 &quo1 $2
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
from math import floor, log
|
||||
from os import environ
|
||||
from random import randint
|
||||
from subprocess import Popen, PIPE
|
||||
from subprocess import Popen, PIPE, run
|
||||
|
||||
u3 = {'sz': 1 << 3, '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))
|
||||
|
||||
def pipe():
|
||||
cli = environ['HOME'] + '/w/uxn/bin/uxncli'
|
||||
return Popen([cli, 'run.rom'], stdin=PIPE, stdout=PIPE)
|
||||
return Popen(['uxncli', 'run.rom'], stdin=PIPE, stdout=PIPE)
|
||||
|
||||
def bitcount(x):
|
||||
return floor(log(x, 2)) + 1
|
||||
|
||||
def main():
|
||||
trials = 1000
|
||||
run(['uxnasm', 'test-math32.tal', 'run.rom'])
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue