From ae4b4cbcdfd2379d082391d6ec5e87757b712007 Mon Sep 17 00:00:00 2001 From: d6 Date: Sun, 26 Dec 2021 01:56:43 -0500 Subject: [PATCH] right shift is working --- math32.tal | 73 ++++++++++++++++++++++++++++++++++++++++++++---------- tester.py | 14 ++++++----- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/math32.tal b/math32.tal index c846f83..3f4c135 100644 --- a/math32.tal +++ b/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< x< 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< 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< 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< r ) + POP2 POP #00 SWP #0000 SWP2 ( 00 00 00 x0 ) + ,&r LDR SFT +RTN +[ &r $1 ] + @left-shift ( x** n^ -> x< x< x< ) - TOR ( y^ x* ) - ;emit-short JSR2 - SPACE - ;emit-byte JSR2 -RTN - @emit-short ( x* -> ) SWP ( lo^ hi^ ) EMIT-BYTE EMIT-BYTE diff --git a/tester.py b/tester.py index da1ae3a..3355d7c 100644 --- a/tester.py +++ b/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)