nxu/math32.tal

533 lines
14 KiB
Tal

( math32.tal )
( )
( 32-bit integers are represented by two 16-bit integers )
( x** means xhi* xlo* )
%DEBUG { #ff #0e DEO }
%RTN { JMP2r }
%EMIT { .Console/write DEO }
%DIGIT { #00 SWP ;digits ADD2 LDA EMIT }
%SPACE { #20 EMIT }
%NEWLINE { #0a EMIT }
%TOR { ROT ROT } ( a b c -> c a b )
%TOR2 { ROT2 ROT2 }
%POP4 { POP2 POP2 }
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 }
%RESET-POS { #0000 ;pos STA2 #00 ;buf STA }
%EMIT-BYTE { DUP #04 SFT DIGIT #0f AND DIGIT }
( devices )
|10 @Console [ &vector $2 &read $1 &pad $5 &write $1 ]
( program )
|0100
( #1234 #5678 ;emit-long JSR2 NEWLINE
#0000 #0009 #0000 #0003 ;div32 JSR2 ;emit-long JSR2 NEWLINE )
;test-interact .Console/vector DEO2 BRK
( bitcount: number of bits needed to represent number )
( equivalent to floor[log2[x]] + 1 )
@bitcount8 ( x^ -> n^ )
#00 SWP ( n x )
&loop
DUP #00 EQU ( n x x=0 )
,&done JCN ( n x )
#01 SFT ( n x>>1 )
SWP INC SWP ( n+1 x>>1 )
,&loop JMP
&done
POP ( n )
RTN
@bitcount16 ( x* -> n^ )
SWP ( xlo xhi )
;bitcount8 JSR2 ( xlo nhi )
DUP #00 NEQ ( xlo nhi nhi!=0 )
,&hi-set JCN ( xlo nhi )
SWP ;bitcount8 JSR2 ADD ( nhi+nlo )
RTN
&hi-set
SWP POP #08 ADD ( nhi+8 )
RTN
@bitcount32 ( x** -> n^ )
SWP2 ( xlo* xhi* )
;bitcount16 JSR2 ( xlo* nhi )
DUP #00 NEQ ( xlo* nhi nhi!=0 )
,&hi-set JCN ( xlo* nhi )
TOR ;bitcount16 JSR2 ADD RTN ( nhi+nlo )
&hi-set
TOR POP2 #10 ADD ( nhi+16 )
RTN
( equality )
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 EQU2 #00 TOR2
EQU2 SWP POP AND
RTN
@is-zero32 ( x** -> bool^ )
ORA2 #0000 EQU2
RTN
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 NEQ2 #00 TOR2
NEQ2 SWP POP ORA
RTN
@non-zero32 ( x** -> bool^ )
ORA2 #0000 NEQ2
RTN
( comparisons )
( x < y )
@lt32 ( x** y** -> bool^ )
ROT2 SWP2 ( xhi yhi xlo ylo )
LTH2 ,&lt-lo JCN ( xhi yhi )
LTH2 RTN
&lt-lo
GTH2 #00 EQU RTN
( x <= y )
@lteq32 ( x** y** -> bool^ )
ROT2 SWP2 ( xhi yhi xlo ylo )
GTH2 ,&gt-lo JCN ( xhi yhi )
GTH2 #00 EQU RTN
&gt-lo
LTH2 RTN
( x > y )
@gt32 ( x** y** -> bool^ )
ROT2 SWP2 ( xhi yhi xlo ylo )
GTH2 ,&gt-lo JCN ( xhi yhi )
GTH2 RTN
&gt-lo
LTH2 #00 EQU RTN
( x > y )
@gteq32 ( x** y** -> bool^ )
ROT2 SWP2 ( xhi yhi xlo ylo )
LTH2 ,&lt-lo JCN ( xhi yhi )
LTH2 #00 EQU RTN
&lt-lo
GTH2 RTN
( bitwise operations )
@and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 AND2 TOR2 AND2 SWP2
RTN
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 ORA2 TOR2 ORA2 SWP2
RTN
@xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 EOR2 TOR2 EOR2 SWP2
RTN
@complement32 ( x** -> ~x** )
COMPLEMENT32
RTN
( bit shifting )
@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 )
DUP #18 LTH ;left-shift2 JCN2 ( x n )
;left-shift3 JMP2 ( x n )
RTN
( shift left by 0-7 bits )
@left-shift0 ( x** n^ -> x<<n )
#0000 ,&z0 STR2 #0000 ,&z2 STR2
#40 SFT ,&r STR ( n<<4 -> r )
SWP SWP2 SWP ( x3 x2 x1 x0 )
,&r LDR SFT ,&z0 STR ( x3 x2 x1 )
#00 SWP ,&r LDR SFT2 ( x3 x2 00x1<<r )
,&z0 LDR2 ORA2 ,&z0 STR2 ( x3 x2 )
#00 SWP ,&r LDR SFT2 ( x3 00x2<<r )
,&z1 LDR2 ORA2 ,&z1 STR2 ( x3 )
#00 SWP ,&r LDR SFT2 ( 00x3<<r )
,&z2 LDR2 ORA2 ,&z2 STR2 ( )
,&z0 LDR2 ,&z2 LDR2
RTN
[ &r $1 &z0 $1 &z1 $1 &z2 $2 ]
( shift left by 8-15 bits )
@left-shift1 ( x** n^ -> x<<n )
#0000 ,&z0 STR2 #00 ,&z2 STR
#08 SUB #40 SFT ,&r STR ( n<<4 -> r )
SWP SWP2 SWP POP ( x3 x2 x1 )
,&r LDR SFT ,&z0 STR ( x3 x2 )
#00 SWP ,&r LDR SFT2 ( x3 00x2<<r )
,&z0 LDR2 ORA2 ,&z0 STR2 ( x3 )
#00 SWP ,&r LDR SFT2 ( 00x3<<r )
,&z1 LDR2 ORA2 ,&z1 STR2 ( )
,&z0 LDR2 ,&z2 LDR #00
RTN
[ &r $1 &z0 $1 &z1 $1 &z2 $1 ]
( shift left by 16-23 bits )
@left-shift2 ( x** n^ -> x<<n )
#0000 ,&z0 STR2
#10 SUB #40 SFT ,&r STR ( n<<4 -> r )
SWP2 POP2 SWP ( x3 x2 )
,&r LDR SFT ,&z0 STR ( x3 )
#00 SWP ,&r LDR SFT2 ( x3<<r )
,&z0 LDR2 ORA2 ,&z0 STR2 ( )
,&z0 LDR2 #0000
RTN
[ &r $1 &z0 $2 ]
( shift left by 24-31 bits )
@left-shift3 ( x** n^ -> x<<n )
#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
( arithmetic )
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
,&y2 STR2 ,&y0 STR2 ( save ylo, yhi )
,&x2 STR2 ,&x0 STR2 ( save xlo, xhi )
#0000 #0000 ,&z0 STR2 ,&z2 STR2 ( reset zhi, zlo )
( x3 + y3 => z2z3 )
#00 ,&x3 LDR #00 ,&y3 LDR ADD2 ,&z2 STR2
( x2 + y2 + z2 => z1z2 )
#00 ,&x2 LDR ,&z1 LDR2 ADD2 ,&z1 STR2
#00 ,&y2 LDR ,&z1 LDR2 ADD2 ,&z1 STR2
( x1 + y1 + z1 => z0z1 )
#00 ,&x1 LDR ,&z0 LDR2 ADD2 ,&z0 STR2
#00 ,&y1 LDR ,&z0 LDR2 ADD2 ,&z0 STR2
( x0 + y0 + z0 => z0 )
,&x0 LDR ,&z0 LDR ADD ,&z0 STR
,&y0 LDR ,&z0 LDR ADD ,&z0 STR
( load zhi,zlo )
,&z0 LDR2 ,&z2 LDR2
RTN
( registers for add32 )
[ &x0 $1 &x1 $1 &x2 $1 &x3 $1 ]
[ &y0 $1 &y1 $1 &y2 $1 &y3 $1 ]
[ &z0 $1 &z1 $1 &z2 $2 ]
@negate32 ( x** -> -x** )
COMPLEMENT32
INC2 ( ~xhi -xlo )
DUP2 #0000 NEQ2 ( ~xhi -xlo non-zero? )
,&done JCN ( xlo non-zero => don't inc hi )
SWP2 INC2 SWP2 ( -xhi -xlo )
&done
RTN
@sub32 ( x** y** -> z** )
;negate32 JSR2 ;add32 JSR2
RTN
@mul16 ( x* y* -> z** )
,&y1 STR ,&y0 STR ( save ylo, yhi )
,&x1 STR ,&x0 STR ( save xlo, xhi )
#0000 #00 ,&z0 STR2 ,&z2 STR ( reset z0,z1,z2 )
#0000 #00 ,&a0 STR2 ,&a2 STR ( reset a0,a1,a2 )
( x1 * y1 => z1z2 )
#00 ,&x1 LDR #00 ,&y1 LDR MUL2 ,&z1 STR2
( x0 * y1 => z0z1 )
#00 ,&x0 LDR #00 ,&y1 LDR MUL2 ,&z0 LDR2 ADD2 ,&z0 STR2
( x1 * y0 => a1a2 )
#00 ,&x1 LDR #00 ,&y0 LDR MUL2 ,&a1 STR2
( x0 * y0 => a0a1 )
#00 ,&x0 LDR #00 ,&y0 LDR MUL2 ,&a0 LDR2 ADD2 ,&a0 STR2
( add z and a<<8 )
#00 ,&z0 LDR2 ,&z2 LDR
,&a0 LDR2 ,&a2 LDR #00
;add32 JSR2
RTN
[ &x0 $1 &x1 $1 ]
[ &y0 $1 &y1 $1 ]
[ &z0 $1 &z1 $1 &z2 $1 ]
[ &a0 $1 &a1 $1 &a2 $1 ]
@mul32 ( x** y** -> z** )
,&y1 STR2 ,&y0 STR2 ( save ylo, yhi )
,&x1 STR2 ,&x0 STR2 ( save xlo, xhi )
,&y1 LDR2 ,&x1 LDR2 ;mul16 JSR2 ( [x1*y1] )
,&z1 STR2 ,&z0 STR2 ( sum = x1*y1, save zlo, zhi )
,&y1 LDR2 ,&x0 LDR2 MUL2 ( [x0*y1]<<16 )
,&y0 LDR2 ,&x1 LDR2 MUL2 ( [x1*y0]<<16 )
( [x0*y0]<<32 will completely overflow )
ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 )
,&z1 LDR2
RTN
[ &x0 $2 &x1 $2 ]
[ &y0 $2 &y1 $2 ]
[ &z0 $2 &z1 $2 ]
@div32 ( x** y** -> q** )
( store y and x for repeated use )
;div32/div1 STA2 ;div32/div0 STA2 ( y -> div )
;div32/rem1 STA2 ;div32/rem0 STA2 ( x -> rem )
( if x < y then the answer is 0 )
;div32/rem0 LDA2 ;div32/rem1 LDA2
;div32/div0 LDA2 ;div32/div1 LDA2
;lt32 JSR2 ,&is-zero JCN ,&not-zero JMP
&is-zero
#0000 #0000 RTN
( x >= y so the answer is >= 1 )
&not-zero
#0000 ;div32/quo0 STA2 #0000 ;div32/quo1 STA2 ( 0 -> quo )
( bitcount[x] - bitcount[y] determines the largest multiple of y to try )
;div32/rem0 LDA2 ;div32/rem1 LDA2 ;bitcount32 JSR2 ( rbits^ )
;div32/div0 LDA2 ;div32/div1 LDA2 ;bitcount32 JSR2 ( rbits^ dbits^ )
SUB ( shift=rbits-dits )
#00 DUP2 ( shift 0 shift 0 )
( 1<<shift -> cur )
#0000 #0001 ROT2 POP
;left-shift JSR2 ;div32/cur1 STA2 ;div32/cur0 STA2
( div<<shift -> div )
;div32/div0 LDA2 ;div32/div1 LDA2 ROT2 POP
;left-shift JSR2 ;div32/div1 STA2 ;div32/div0 STA2
&loop
( if rem >= the current divisor, we can subtract it and add to quotient )
,&rem0 LDR2 ,&rem1 LDR2 ,&div0 LDR2 ,&div1 LDR2 ;lt32 JSR2 ( rem<div? )
,&rem-lt JCN ( if rem < div skip this iteration )
( if rem >= div, then we have found a multiple of y that divides x )
,&rem0 LDR2 ,&rem1 LDR2 ,&div0 LDR2 ,&div1 LDR2 ;sub32 JSR2 ,&rem1 STR2 ,&rem0 STR2 ( rem -= div )
,&quo0 LDR2 ,&quo1 LDR2 ,&cur0 LDR2 ,&cur1 LDR2 ;add32 JSR2 ,&quo1 STR2 ,&quo0 STR2 ( quo += cur )
&rem-lt
,&div0 LDR2 ,&div1 LDR2 #01 ;right-shift JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 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 )
,&quo0 LDR2 ,&quo1 LDR2 ( TODO: consider making this divmod32 )
RTN
[ &div0 $2 &div1 $2
&rem0 $2 &rem1 $2
&quo0 $2 &quo1 $2
&cur0 $2 &cur1 $2 ]
( testing )
( parses hex representation e.g. #31 #33 -> #13 )
@parse-byte ( c0 c1 -> x^ )
( lower char )
DUP #3a LTH ,&lo-digit JCN
#57 ,&lo JMP &lo-digit #30
&lo SUB SWP
( higher char ))
DUP #3a LTH ,&hi-digit JCN
#57 ,&hi JMP &hi-digit #30
&hi SUB #40 SFT ORA
RTN
@buf $24 ( buffer used by test-interact )
@pos $2 ( position in buffer used by test-interact )
( save character input and execute tests on \n )
( tests always start with a single character and a space )
( then additional arguments are passed. )
@test-interact
.Console/read DEI ( char^ )
DUP #0a EQU ( char^ char=\n? )
,&exec JCN ( char^ )
;pos LDA2 ;buf ADD2 STA
;pos LDA2k INC2 SWP2 STA2 BRK
&exec
POP ( )
;buf LDA LIT '+ EQU ;test-add32 JCN2
;buf LDA LIT '* EQU ;test-mul32 JCN2
;buf LDA LIT '- EQU ;test-sub32 JCN2
;buf LDA LIT '/ EQU ;test-div32 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
;buf LDA LIT '^ EQU ;test-xor32 JCN2
;buf LDA LIT '~ EQU ;test-complement32 JCN2
;buf LDA LIT 'N EQU ;test-negate32 JCN2
;buf LDA LIT '= EQU ;test-eq32 JCN2
;buf LDA LIT '! EQU ;test-ne32 JCN2
;buf LDA LIT '0 EQU ;test-is-zero32 JCN2
;buf LDA LIT 'Z EQU ;test-non-zero32 JCN2
;buf LDA LIT '< EQU ;test-lt32 JCN2
;buf LDA LIT '> EQU ;test-gt32 JCN2
;buf LDA LIT '{ EQU ;test-lteq32 JCN2
;buf LDA LIT '} EQU ;test-gteq32 JCN2
LIT '? EMIT NEWLINE RESET-POS BRK
@read-byte ( addr* -> x^ )
LDA2 ;parse-byte JSR2
RTN
@read-long ( addr* -> x** )
DUP2 ,&loc STR2 LDA2 ;parse-byte JSR2
,&loc LDR2 #0002 ADD2 LDA2 ;parse-byte JSR2
,&loc LDR2 #0004 ADD2 LDA2 ;parse-byte JSR2
,&loc LDR2 #0006 ADD2 LDA2 ;parse-byte JSR2
RTN
[ &loc $2 ]
( format: ". xxxxxxxx" -> "zzzzzzzz" )
@unary-32-test
;buf #0002 ADD2 ;read-long JSR2
ROT2 JSR2 ;emit-long JSR2
NEWLINE RESET-POS BRK
( format: ". xxxxxxxx" -> "zz" )
@unary-32-8-test
;buf #0002 ADD2 ;read-long JSR2
ROT2 JSR2 ;emit-byte JSR2
NEWLINE RESET-POS BRK
( format: ". xxxxxxxx yyyyyyyy" -> "zzzzzzzz" )
@binary-32-test
;buf #0002 ADD2 ;read-long JSR2
ROT2
;buf #000b ADD2 ;read-long JSR2
ROT2 JSR2 ;emit-long JSR2
NEWLINE RESET-POS BRK
( format: ". xxxxxxxx yy" -> "zzzzzzzz" )
@binary-32-8-32-test
;buf #0002 ADD2 ;read-long JSR2
ROT2
;buf #000b ADD2 ;read-byte JSR2
TOR JSR2 ;emit-long JSR2
NEWLINE RESET-POS BRK
( format: ". xxxxxxxx yyyyyyyy" -> "zz" )
@binary-32-32-8-test
;buf #0002 ADD2 ;read-long JSR2
ROT2
;buf #000b ADD2 ;read-long JSR2
ROT2 JSR2 ;emit-byte JSR2
NEWLINE RESET-POS BRK
( different test executors )
@test-add32 ;add32 ;binary-32-test JMP2
@test-mul32 ;mul32 ;binary-32-test JMP2
@test-sub32 ;sub32 ;binary-32-test JMP2
@test-div32 ;div32 ;binary-32-test JMP2
@test-left-shift ;left-shift ;binary-32-8-32-test JMP2
@test-right-shift ;right-shift ;binary-32-8-32-test JMP2
@test-bitcount32 ;bitcount32 ;unary-32-8-test JMP2
@test-and32 ;and32 ;binary-32-test JMP2
@test-or32 ;or32 ;binary-32-test JMP2
@test-xor32 ;xor32 ;binary-32-test JMP2
@test-complement32 ;complement32 ;unary-32-test JMP2
@test-negate32 ;negate32 ;unary-32-test JMP2
@test-eq32 ;eq32 ;binary-32-32-8-test JMP2
@test-ne32 ;ne32 ;binary-32-32-8-test JMP2
@test-is-zero32 ;is-zero32 ;unary-32-8-test JMP2
@test-non-zero32 ;non-zero32 ;unary-32-8-test JMP2
@test-lt32 ;lt32 ;binary-32-32-8-test JMP2
@test-lteq32 ;lteq32 ;binary-32-32-8-test JMP2
@test-gt32 ;gt32 ;binary-32-32-8-test JMP2
@test-gteq32 ;gteq32 ;binary-32-32-8-test JMP2
@emit-long ( hi* lo* -> )
SWP2
SWP EMIT-BYTE EMIT-BYTE
SWP EMIT-BYTE EMIT-BYTE
RTN
( @emit-short ( x* -> )
( SWP EMIT-BYTE EMIT-BYTE
RTN )
@emit-byte ( x^ -> )
EMIT-BYTE
RTN
( convenience for less branching when printing hex )
@digits
30 31 32 33 34 35 36 37
38 39 61 62 63 64 65 66