right shift is working

This commit is contained in:
~d6 2021-12-26 01:56:43 -05:00
parent b2bf87ae68
commit ae4b4cbcdf
2 changed files with 68 additions and 19 deletions

View File

@ -68,6 +68,7 @@ RTN
;buf LDA LIT '* EQU ;test-mul32 JCN2 ;buf LDA LIT '* EQU ;test-mul32 JCN2
;buf LDA LIT '- EQU ;test-sub32 JCN2 ;buf LDA LIT '- EQU ;test-sub32 JCN2
;buf LDA LIT 'L EQU ;test-left-shift 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 'B EQU ;test-bitcount32 JCN2
;buf LDA LIT '& EQU ;test-and32 JCN2 ;buf LDA LIT '& EQU ;test-and32 JCN2
;buf LDA LIT '| EQU ;test-or32 JCN2 ;buf LDA LIT '| EQU ;test-or32 JCN2
@ -105,6 +106,13 @@ RTN
;left-shift JSR2 ;emit-long JSR2 ;left-shift JSR2 ;emit-long JSR2
NEWLINE RESET-POS BRK 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 @test-bitcount32
( format: "B xxxxxxxx" ) ( format: "B xxxxxxxx" )
;buf #0002 ADD2 ;read-long JSR2 ;buf #0002 ADD2 ;read-long JSR2
@ -169,7 +177,7 @@ RTN
EQU2 SWP POP AND EQU2 SWP POP AND
RTN RTN
@eq-zero32 ( x** -> bool^ ) @is-zero32 ( x** -> bool^ )
ORA2 #0000 EQU2 ORA2 #0000 EQU2
RTN RTN
@ -178,7 +186,7 @@ RTN
NEQ2 SWP POP ORA NEQ2 SWP POP ORA
RTN RTN
@ne-zero32 ( x** -> bool^ ) @non-zero32 ( x** -> bool^ )
ORA2 #0000 NEQ2 ORA2 #0000 NEQ2
RTN RTN
@ -209,10 +217,56 @@ RTN
&done &done
RTN RTN
@left-by-16 ( xhi* xlo* -> xlo* 0000 ) @right-shift ( x** n^ -> x<<n )
SWP2 POP2 #0000 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 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 ) @left-shift ( x** n^ -> x<<n )
DUP #08 LTH ;left-shift0 JCN2 ( x n ) DUP #08 LTH ;left-shift0 JCN2 ( x n )
DUP #10 LTH ;left-shift1 JCN2 ( x n ) DUP #10 LTH ;left-shift1 JCN2 ( x n )
@ -239,7 +293,7 @@ RTN
,&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 $2 ]
( shift left by 8-15 bits ) ( shift left by 8-15 bits )
@left-shift1 ( x** n^ -> x<<n ) @left-shift1 ( x** n^ -> x<<n )
@ -272,7 +326,7 @@ RTN
,&z0 LDR2 #0000 ,&z0 LDR2 #0000
RTN RTN
[ &r $1 &z0 $1 &z1 $1 ] [ &r $1 &z0 $2 ]
( shift left by 24-31 bits ) ( shift left by 24-31 bits )
@left-shift3 ( x** n^ -> x<<n ) @left-shift3 ( x** n^ -> x<<n )
@ -397,13 +451,6 @@ RTN
%EMIT-BYTE { DUP #04 SFT DIGIT #0f AND DIGIT } %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* -> ) @emit-short ( x* -> )
SWP ( lo^ hi^ ) SWP ( lo^ hi^ )
EMIT-BYTE EMIT-BYTE EMIT-BYTE EMIT-BYTE

View File

@ -4,6 +4,7 @@ from os import environ
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from random import randint from random import randint
u3 = {'sz': 1 << 3, 'fmt': b'%02x'}
u5 = {'sz': 1 << 5, 'fmt': b'%02x'} u5 = {'sz': 1 << 5, 'fmt': b'%02x'}
u8 = {'sz': 1 << 8, 'fmt': b'%02x'} u8 = {'sz': 1 << 8, 'fmt': b'%02x'}
u16 = {'sz': 1 << 16, 'fmt': b'%04x'} u16 = {'sz': 1 << 16, 'fmt': b'%04x'}
@ -13,23 +14,23 @@ def fmt(gx, x):
return gx['fmt'].decode('utf-8') % (x % gx['sz']) return gx['fmt'].decode('utf-8') % (x % gx['sz'])
def testcase(p, sym, args, out, f): 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) p.stdin.write(sym)
for i in range(0, len(args)): for _, g, x in vals:
x = xs[i]
g = args[i][1]
p.stdin.write(b' ') p.stdin.write(b' ')
p.stdin.write(g['fmt'] % x) p.stdin.write(g['fmt'] % x)
p.stdin.write(b'\n') p.stdin.write(b'\n')
p.stdin.flush() p.stdin.flush()
got = p.stdout.readline().strip().decode('utf-8') got = p.stdout.readline().strip().decode('utf-8')
xs = [x for _, _, x in vals]
z = f(*xs) z = f(*xs)
expected = fmt(out, z) expected = fmt(out, z)
if got == expected: if got == expected:
return None return None
else: else:
res = {'got': got, 'expected': expected} res = {'got': got, 'expected': expected}
for (name, x) in args: res[name] = x for name, _, x in vals:
res[name] = x
return res return res
def test(p, runs, sym, args, out, f): def test(p, runs, sym, args, out, f):
@ -59,12 +60,13 @@ def bitcount(x):
return n return n
def main(): def main():
runs = 100 runs = 1000
p = pipe() 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'-', [('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'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'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)
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)