right shift is working
This commit is contained in:
parent
b2bf87ae68
commit
ae4b4cbcdf
73
math32.tal
73
math32.tal
|
@ -68,6 +68,7 @@ RTN
|
|||
;buf LDA LIT '* EQU ;test-mul32 JCN2
|
||||
;buf LDA LIT '- EQU ;test-sub32 JCN2
|
||||
;buf LDA LIT 'L EQU ;test-left-shift JCN2
|
||||
;buf LDA LIT 'R EQU ;test-right-shift JCN2
|
||||
;buf LDA LIT 'B EQU ;test-bitcount32 JCN2
|
||||
;buf LDA LIT '& EQU ;test-and32 JCN2
|
||||
;buf LDA LIT '| EQU ;test-or32 JCN2
|
||||
|
@ -105,6 +106,13 @@ RTN
|
|||
;left-shift JSR2 ;emit-long JSR2
|
||||
NEWLINE RESET-POS BRK
|
||||
|
||||
@test-right-shift
|
||||
( format: "+ xxxxxxxx yy" )
|
||||
;buf #0002 ADD2 ;read-long JSR2
|
||||
;buf #000b ADD2 ;read-byte JSR2
|
||||
;right-shift JSR2 ;emit-long JSR2
|
||||
NEWLINE RESET-POS BRK
|
||||
|
||||
@test-bitcount32
|
||||
( format: "B xxxxxxxx" )
|
||||
;buf #0002 ADD2 ;read-long JSR2
|
||||
|
@ -169,7 +177,7 @@ RTN
|
|||
EQU2 SWP POP AND
|
||||
RTN
|
||||
|
||||
@eq-zero32 ( x** -> bool^ )
|
||||
@is-zero32 ( x** -> bool^ )
|
||||
ORA2 #0000 EQU2
|
||||
RTN
|
||||
|
||||
|
@ -178,7 +186,7 @@ RTN
|
|||
NEQ2 SWP POP ORA
|
||||
RTN
|
||||
|
||||
@ne-zero32 ( x** -> bool^ )
|
||||
@non-zero32 ( x** -> bool^ )
|
||||
ORA2 #0000 NEQ2
|
||||
RTN
|
||||
|
||||
|
@ -209,10 +217,56 @@ RTN
|
|||
&done
|
||||
RTN
|
||||
|
||||
@left-by-16 ( xhi* xlo* -> xlo* 0000 )
|
||||
SWP2 POP2 #0000
|
||||
@right-shift ( x** n^ -> x<<n )
|
||||
DUP #08 LTH ;right-shift0 JCN2 ( x n )
|
||||
DUP #10 LTH ;right-shift1 JCN2 ( x n )
|
||||
DUP #18 LTH ;right-shift2 JCN2 ( x n )
|
||||
;right-shift3 JMP2 ( x n )
|
||||
RTN
|
||||
|
||||
( shift right by 0-7 bits )
|
||||
@right-shift0 ( x** n^ -> x<<n )
|
||||
#0000 ,&z0 STR2 #0000 ,&z2 STR2
|
||||
,&r STR ( n -> r )
|
||||
,&r LDR SFT ,&z3 STR ( write 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 ,&z0 LDR2 ORA2 ,&z0 STR2 ( write z0,z1 )
|
||||
,&z0 LDR2 ,&z2 LDR2
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( shift right by 8-15 bits )
|
||||
@right-shift1 ( x** n^ -> x<<n )
|
||||
#00 ,&z1 STR #0000 ,&z2 STR2
|
||||
#08 SUB ,&r STR ( n -> r )
|
||||
POP
|
||||
,&r LDR SFT ,&z3 STR ( write 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 ,&z1 LDR ,&z2 LDR2
|
||||
RTN
|
||||
[ &r $1 &z1 $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( shift right by 16-23 bits )
|
||||
@right-shift2 ( x** n^ -> x<<n )
|
||||
#0000 ,&z2 STR2
|
||||
#10 SUB ,&r STR ( n -> r )
|
||||
POP2
|
||||
,&r LDR SFT ,&z3 STR ( write z3 )
|
||||
#00 ,&r LDR SFT2 ,&z2 LDR2 ORA2 ,&z2 STR2 ( write z2,z3 )
|
||||
#0000 ,&z2 LDR2
|
||||
RTN
|
||||
[ &r $1 &z2 $1 &z3 $1 ]
|
||||
|
||||
( shift right by 16-23 bits )
|
||||
@right-shift3 ( x** n^ -> x<<n )
|
||||
#18 SUB ,&r STR ( n -> r )
|
||||
POP2 POP #00 SWP #0000 SWP2 ( 00 00 00 x0 )
|
||||
,&r LDR SFT
|
||||
RTN
|
||||
[ &r $1 ]
|
||||
|
||||
@left-shift ( x** n^ -> x<<n )
|
||||
DUP #08 LTH ;left-shift0 JCN2 ( x n )
|
||||
DUP #10 LTH ;left-shift1 JCN2 ( x n )
|
||||
|
@ -239,7 +293,7 @@ RTN
|
|||
|
||||
,&z0 LDR2 ,&z2 LDR2
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $1 &z3 $1 ]
|
||||
[ &r $1 &z0 $1 &z1 $1 &z2 $2 ]
|
||||
|
||||
( shift left by 8-15 bits )
|
||||
@left-shift1 ( x** n^ -> x<<n )
|
||||
|
@ -272,7 +326,7 @@ RTN
|
|||
|
||||
,&z0 LDR2 #0000
|
||||
RTN
|
||||
[ &r $1 &z0 $1 &z1 $1 ]
|
||||
[ &r $1 &z0 $2 ]
|
||||
|
||||
( shift left by 24-31 bits )
|
||||
@left-shift3 ( x** n^ -> x<<n )
|
||||
|
@ -397,13 +451,6 @@ RTN
|
|||
|
||||
%EMIT-BYTE { DUP #04 SFT DIGIT #0f AND DIGIT }
|
||||
|
||||
@emit-short-byte ( x* y^ - > )
|
||||
TOR ( y^ x* )
|
||||
;emit-short JSR2
|
||||
SPACE
|
||||
;emit-byte JSR2
|
||||
RTN
|
||||
|
||||
@emit-short ( x* -> )
|
||||
SWP ( lo^ hi^ )
|
||||
EMIT-BYTE EMIT-BYTE
|
||||
|
|
14
tester.py
14
tester.py
|
@ -4,6 +4,7 @@ from os import environ
|
|||
from subprocess import Popen, PIPE
|
||||
from random import randint
|
||||
|
||||
u3 = {'sz': 1 << 3, 'fmt': b'%02x'}
|
||||
u5 = {'sz': 1 << 5, 'fmt': b'%02x'}
|
||||
u8 = {'sz': 1 << 8, 'fmt': b'%02x'}
|
||||
u16 = {'sz': 1 << 16, 'fmt': b'%04x'}
|
||||
|
@ -13,23 +14,23 @@ def fmt(gx, x):
|
|||
return gx['fmt'].decode('utf-8') % (x % gx['sz'])
|
||||
|
||||
def testcase(p, sym, args, out, f):
|
||||
xs = [randint(0, a[1]['sz'] - 1) for a in args]
|
||||
vals = [(name, g, randint(0, g['sz'] - 1)) for (name, g) in args]
|
||||
p.stdin.write(sym)
|
||||
for i in range(0, len(args)):
|
||||
x = xs[i]
|
||||
g = args[i][1]
|
||||
for _, g, x in vals:
|
||||
p.stdin.write(b' ')
|
||||
p.stdin.write(g['fmt'] % x)
|
||||
p.stdin.write(b'\n')
|
||||
p.stdin.flush()
|
||||
got = p.stdout.readline().strip().decode('utf-8')
|
||||
xs = [x for _, _, x in vals]
|
||||
z = f(*xs)
|
||||
expected = fmt(out, z)
|
||||
if got == expected:
|
||||
return None
|
||||
else:
|
||||
res = {'got': got, 'expected': expected}
|
||||
for (name, x) in args: res[name] = x
|
||||
for name, _, x in vals:
|
||||
res[name] = x
|
||||
return res
|
||||
|
||||
def test(p, runs, sym, args, out, f):
|
||||
|
@ -59,12 +60,13 @@ def bitcount(x):
|
|||
return n
|
||||
|
||||
def main():
|
||||
runs = 100
|
||||
runs = 1000
|
||||
p = pipe()
|
||||
test(p, runs, b'+', [('x', u32), ('y', u32)], u32, lambda x, y: x + y)
|
||||
test(p, runs, b'-', [('x', u32), ('y', u32)], u32, lambda x, y: x - y)
|
||||
test(p, runs, b'*', [('x', u32), ('y', u32)], u32, lambda x, y: x * y)
|
||||
test(p, runs, b'L', [('x', u32), ('y', u5)], u32, lambda x, y: x << y)
|
||||
test(p, runs, b'R', [('x', u32), ('y', u5)], u32, lambda x, y: x >> y)
|
||||
test(p, runs, b'B', [('x', u32)], u8, bitcount)
|
||||
test(p, runs, b'&', [('x', u32), ('y', u32)], u32, lambda x, y: x & y)
|
||||
test(p, runs, b'|', [('x', u32), ('y', u32)], u32, lambda x, y: x | y)
|
||||
|
|
Loading…
Reference in New Issue