uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 51c422c9cb7a5f84b2fa39c79185b265c921ba34
parent b5669a52be177df8849c31a4534668c4e6e30eea
Author: neauoire <aliceffekt@gmail.com>
Date:   Mon,  7 Feb 2022 19:35:07 -0800

(math32.tal) Removed globals

Diffstat:
Mprojects/library/math32.tal | 185+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 91 insertions(+), 94 deletions(-)

diff --git a/projects/library/math32.tal b/projects/library/math32.tal @@ -54,9 +54,6 @@ ( - mul32 memory, 12 bytes ) ( - _divmod32 memory, 16 bytes ) -%DEBUG { #ff #0e DEO } -%RTN { JMP2r } -%TOR { ROT ROT } ( a b c -> c a b ) %COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 } %DUP4 { OVR2 OVR2 } %POP4 { POP2 POP2 } @@ -74,7 +71,7 @@ ,&loop JMP &done POP ( n ) - RTN + JMP2r @bitcount16 ( x* -> n^ ) SWP ( xlo xhi ) @@ -82,40 +79,40 @@ DUP #00 NEQ ( xlo nhi nhi!=0 ) ,&hi-set JCN ( xlo nhi ) SWP ;bitcount8 JSR2 ADD ( nhi+nlo ) - RTN + JMP2r &hi-set SWP POP #08 ADD ( nhi+8 ) - RTN + JMP2r @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 ) + ROT ROT ;bitcount16 JSR2 ADD JMP2r ( nhi+nlo ) &hi-set - TOR POP2 #10 ADD ( nhi+16 ) - RTN + ROT ROT POP2 #10 ADD ( nhi+16 ) + JMP2r ( equality ) ( x == y ) @eq32 ( xhi* xlo* yhi* ylo* -> bool^ ) ROT2 EQU2 STH - EQU2 STHr AND RTN + EQU2 STHr AND JMP2r ( x != y ) @ne32 ( xhi* xlo* yhi* ylo* -> bool^ ) ROT2 NEQ2 STH - NEQ2 STHr ORA RTN + NEQ2 STHr ORA JMP2r ( x == 0 ) @is-zero32 ( x** -> bool^ ) - ORA2 #0000 EQU2 RTN + ORA2 #0000 EQU2 JMP2r ( x != 0 ) @non-zero32 ( x** -> bool^ ) - ORA2 #0000 NEQ2 RTN + ORA2 #0000 NEQ2 JMP2r ( comparisons ) @@ -123,58 +120,58 @@ @lt32 ( x** y** -> bool^ ) ROT2 SWP2 ( xhi yhi xlo ylo ) LTH2 ,&lt-lo JCN ( xhi yhi ) - LTH2 RTN + LTH2 JMP2r &lt-lo - GTH2 #00 EQU RTN + GTH2 #00 EQU JMP2r ( x <= y ) @lteq32 ( x** y** -> bool^ ) ROT2 SWP2 ( xhi yhi xlo ylo ) GTH2 ,&gt-lo JCN ( xhi yhi ) - GTH2 #00 EQU RTN + GTH2 #00 EQU JMP2r &gt-lo - LTH2 RTN + LTH2 JMP2r ( x > y ) @gt32 ( x** y** -> bool^ ) ROT2 SWP2 ( xhi yhi xlo ylo ) GTH2 ,&gt-lo JCN ( xhi yhi ) - GTH2 RTN + GTH2 JMP2r &gt-lo - LTH2 #00 EQU RTN + LTH2 #00 EQU JMP2r ( x > y ) @gteq32 ( x** y** -> bool^ ) ROT2 SWP2 ( xhi yhi xlo ylo ) LTH2 ,&lt-lo JCN ( xhi yhi ) - LTH2 #00 EQU RTN + LTH2 #00 EQU JMP2r &lt-lo - GTH2 RTN + GTH2 JMP2r ( bitwise operations ) ( x & y ) @and32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) - ROT2 AND2 STH2 AND2 STH2r RTN + ROT2 AND2 STH2 AND2 STH2r JMP2r ( x | y ) @or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) - ROT2 ORA2 STH2 ORA2 STH2r RTN + ROT2 ORA2 STH2 ORA2 STH2r JMP2r ( x ^ y ) @xor32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* ) - ROT2 EOR2 STH2 EOR2 STH2r RTN + ROT2 EOR2 STH2 EOR2 STH2r JMP2r ( ~x ) @complement32 ( x** -> ~x** ) - COMPLEMENT32 RTN + COMPLEMENT32 JMP2r ( temporary registers ) ( shared by most operations, except mul32 and div32 ) -[ @x0 $1 @x1 $1 @x2 $1 @x3 $1 - @y0 $1 @y1 $1 @y2 $1 @y3 $1 - @z0 $1 @z1 $1 @z2 $1 @z3 $1 - @w0 $1 @w1 $1 @w2 $2 ] +@m32 [ &x0 $1 &x1 $1 &x2 $1 &x3 $1 + &y0 $1 &y1 $1 &y2 $1 &y3 $1 + &z0 $1 &z1 $1 &z2 $1 &z3 $1 + &w0 $1 &w1 $1 &w2 $2 ] ( bit shifting ) @@ -184,40 +181,40 @@ DUP #10 LTH ;rshift32-1 JCN2 ( x n ) DUP #18 LTH ;rshift32-2 JCN2 ( x n ) ;rshift32-3 JMP2 ( x n ) - RTN + JMP2r ( shift right by 0-7 bits ) @rshift32-0 ( x** n^ -> x<<n ) - STHk SFT ;z3 STA ( write z3 ) - #00 STHkr SFT2 #00 ;z3 LDA ORA2 ;z2 STA2 ( write z2,z3 ) - #00 STHkr SFT2 #00 ;z2 LDA ORA2 ;z1 STA2 ( write z1,z2 ) - #00 STHr SFT2 #00 ;z1 LDA ORA2 ( compute z0,z1 ) - ;z2 LDA2 - RTN + STHk SFT ;m32/z3 STA ( write z3 ) + #00 STHkr SFT2 #00 ;m32/z3 LDA ORA2 ;m32/z2 STA2 ( write z2,z3 ) + #00 STHkr SFT2 #00 ;m32/z2 LDA ORA2 ;m32/z1 STA2 ( write z1,z2 ) + #00 STHr SFT2 #00 ;m32/z1 LDA ORA2 ( compute z0,z1 ) + ;m32/z2 LDA2 + JMP2r ( shift right by 8-15 bits ) @rshift32-1 ( x** n^ -> x<<n ) #08 SUB STH POP - STHkr SFT ;z3 STA ( write z3 ) - #00 STHkr SFT2 #00 ;z3 LDA ORA2 ;z2 STA2 ( write z2,z3 ) - #00 STHr SFT2 #00 ;z2 LDA ORA2 ( compute z1,z2 ) - #00 TOR ;z3 LDA - RTN + STHkr SFT ;m32/z3 STA ( write z3 ) + #00 STHkr SFT2 #00 ;m32/z3 LDA ORA2 ;m32/z2 STA2 ( write z2,z3 ) + #00 STHr SFT2 #00 ;m32/z2 LDA ORA2 ( compute z1,z2 ) + #00 ROT ROT ;m32/z3 LDA + JMP2r ( shift right by 16-23 bits ) @rshift32-2 ( x** n^ -> x<<n ) #10 SUB STH POP2 - STHkr SFT ;z3 STA ( write z3 ) - #00 STHr SFT2 #00 ;z3 LDA ORA2 ( compute z2,z3 ) + STHkr SFT ;m32/z3 STA ( write z3 ) + #00 STHr SFT2 #00 ;m32/z3 LDA ORA2 ( compute z2,z3 ) #0000 SWP2 - RTN + JMP2r ( shift right by 16-23 bits ) @rshift32-3 ( x** n^ -> x<<n ) #18 SUB STH POP2 POP ( x0 ) #00 SWP #0000 SWP2 ( 00 00 00 x0 ) STHr SFT - RTN + JMP2r ( x << n ) @lshift32 ( x** n^ -> x<<n ) @@ -225,70 +222,70 @@ DUP #10 LTH ;lshift32-1 JCN2 ( x n ) DUP #18 LTH ;lshift32-2 JCN2 ( x n ) ;lshift32-3 JMP2 ( x n ) - RTN + JMP2r ( shift left by 0-7 bits ) @lshift32-0 ( x** n^ -> x<<n ) #40 SFT STH ( stash n<<4 ) - #00 SWP STHkr SFT2 ;z2 STA2 ( store z2,z3 ) - #00 SWP STHkr SFT2 #00 ;z2 LDA ORA2 ;z1 STA2 ( store z1,z2 ) - #00 SWP STHkr SFT2 #00 ;z1 LDA ORA2 ;z0 STA2 ( store z0,z1 ) - STHr SFT ;z0 LDA ORA ( calculate z0 ) - ;z1 LDA ;z2 LDA2 - RTN + #00 SWP STHkr SFT2 ;m32/z2 STA2 ( store z2,z3 ) + #00 SWP STHkr SFT2 #00 ;m32/z2 LDA ORA2 ;m32/z1 STA2 ( store z1,z2 ) + #00 SWP STHkr SFT2 #00 ;m32/z1 LDA ORA2 ;m32/z0 STA2 ( store z0,z1 ) + STHr SFT ;m32/z0 LDA ORA ( calculate z0 ) + ;m32/z1 LDA ;m32/z2 LDA2 + JMP2r ( shift left by 8-15 bits ) @lshift32-1 ( x** n^ -> x<<n ) #08 SUB #40 SFT STH ( stash [n-8]<<4 ) - #00 SWP STHkr SFT2 ;z1 STA2 ( store z1,z2 ) - #00 SWP STHkr SFT2 #00 ;z1 LDA ORA2 ;z0 STA2 ( store z0,z1 ) - STHr SFT ;z0 LDA ORA ( calculate z0 ) + #00 SWP STHkr SFT2 ;m32/z1 STA2 ( store z1,z2 ) + #00 SWP STHkr SFT2 #00 ;m32/z1 LDA ORA2 ;m32/z0 STA2 ( store z0,z1 ) + STHr SFT ;m32/z0 LDA ORA ( calculate z0 ) SWP POP ( x0 unused ) - ;z1 LDA2 #00 - RTN + ;m32/z1 LDA2 #00 + JMP2r ( shift left by 16-23 bits ) @lshift32-2 ( x** n^ -> x<<n ) #10 SUB #40 SFT STH ( stash [n-16]<<4 ) - #00 SWP STHkr SFT2 ;z0 STA2 ( store z0,z1 ) - STHr SFT ;z0 LDA ORA ( calculate z0 ) + #00 SWP STHkr SFT2 ;m32/z0 STA2 ( store z0,z1 ) + STHr SFT ;m32/z0 LDA ORA ( calculate z0 ) STH POP2 STHr - ;z1 LDA #0000 - RTN + ;m32/z1 LDA #0000 + JMP2r ( shift left by 24-31 bits ) @lshift32-3 ( x** n^ -> x<<n ) #18 SUB #40 SFT ( x0 x1 x2 x3 r=[n-24]<<4 ) SFT ( x0 x1 x2 x3<<r ) SWP2 POP2 SWP POP #0000 #00 - RTN + JMP2r ( arithmetic ) ( x + y ) @add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* ) - ;y2 STA2 ;y0 STA2 ( save ylo, yhi ) - ;x2 STA2 ;x0 STA2 ( save xlo, xhi ) - #0000 #0000 ;z0 STA2 ;z2 STA2 ( reset zhi, zlo ) + ;m32/y2 STA2 ;m32/y0 STA2 ( save ylo, yhi ) + ;m32/x2 STA2 ;m32/x0 STA2 ( save xlo, xhi ) + #0000 #0000 ;m32/z0 STA2 ;m32/z2 STA2 ( reset zhi, zlo ) ( x3 + y3 => z2z3 ) - #00 ;x3 LDA #00 ;y3 LDA ADD2 ;z2 STA2 + #00 ;m32/x3 LDA #00 ;m32/y3 LDA ADD2 ;m32/z2 STA2 ( x2 + y2 + z2 => z1z2 ) - #00 ;x2 LDA ;z1 LDA2 ADD2 ;z1 STA2 - #00 ;y2 LDA ;z1 LDA2 ADD2 ;z1 STA2 + #00 ;m32/x2 LDA ;m32/z1 LDA2 ADD2 ;m32/z1 STA2 + #00 ;m32/y2 LDA ;m32/z1 LDA2 ADD2 ;m32/z1 STA2 ( x1 + y1 + z1 => z0z1 ) - #00 ;x1 LDA ;z0 LDA2 ADD2 ;z0 STA2 - #00 ;y1 LDA ;z0 LDA2 ADD2 ;z0 STA2 + #00 ;m32/x1 LDA ;m32/z0 LDA2 ADD2 ;m32/z0 STA2 + #00 ;m32/y1 LDA ;m32/z0 LDA2 ADD2 ;m32/z0 STA2 ( x0 + y0 + z0 => z0 ) - ;x0 LDA ;z0 LDA ADD ;z0 STA - ;y0 LDA ;z0 LDA ADD ;z0 STA + ;m32/x0 LDA ;m32/z0 LDA ADD ;m32/z0 STA + ;m32/y0 LDA ;m32/z0 LDA ADD ;m32/z0 STA ( load zhi,zlo ) - ;z0 LDA2 ;z2 LDA2 - RTN + ;m32/z0 LDA2 ;m32/z2 LDA2 + JMP2r ( -x ) @negate32 ( x** -> -x** ) @@ -298,36 +295,36 @@ ,&done JCN ( xlo non-zero => don't inc hi ) SWP2 INC2 SWP2 ( -xhi -xlo ) &done - RTN + JMP2r ( x - y ) @sub32 ( x** y** -> z** ) - ;negate32 JSR2 ;add32 JSR2 RTN + ;negate32 JSR2 ;add32 JSR2 JMP2r ( 16-bit multiplication ) @mul16 ( x* y* -> z** ) - ;y1 STA ;y0 STA ( save ylo, yhi ) - ;x1 STA ;x0 STA ( save xlo, xhi ) - #0000 #00 ;z1 STA2 ;z3 STA ( reset z1,z2,z3 ) - #0000 #00 ;w0 STA2 ;w2 STA ( reset w0,w1,w2 ) + ;m32/y1 STA ;m32/y0 STA ( save ylo, yhi ) + ;m32/x1 STA ;m32/x0 STA ( save xlo, xhi ) + #0000 #00 ;m32/z1 STA2 ;m32/z3 STA ( reset z1,z2,z3 ) + #0000 #00 ;m32/w0 STA2 ;m32/w2 STA ( reset w0,w1,w2 ) ( x1 * y1 => z1z2 ) - #00 ;x1 LDA #00 ;y1 LDA MUL2 ;z2 STA2 + #00 ;m32/x1 LDA #00 ;m32/y1 LDA MUL2 ;m32/z2 STA2 ( x0 * y1 => z0z1 ) - #00 ;x0 LDA #00 ;y1 LDA MUL2 ;z1 LDA2 ADD2 ;z1 STA2 + #00 ;m32/x0 LDA #00 ;m32/y1 LDA MUL2 ;m32/z1 LDA2 ADD2 ;m32/z1 STA2 ( x1 * y0 => w1w2 ) - #00 ;x1 LDA #00 ;y0 LDA MUL2 ;w1 STA2 + #00 ;m32/x1 LDA #00 ;m32/y0 LDA MUL2 ;m32/w1 STA2 ( x0 * y0 => w0w1 ) - #00 ;x0 LDA #00 ;y0 LDA MUL2 ;w0 LDA2 ADD2 ;w0 STA2 + #00 ;m32/x0 LDA #00 ;m32/y0 LDA MUL2 ;m32/w0 LDA2 ADD2 ;m32/w0 STA2 ( add z and a<<8 ) - #00 ;z1 LDA2 ;z3 LDA - ;w0 LDA2 ;w2 LDA #00 + #00 ;m32/z1 LDA2 ;m32/z3 LDA + ;m32/w0 LDA2 ;m32/w2 LDA #00 ;add32 JSR2 - RTN + JMP2r ( x * y ) @mul32 ( x** y** -> z** ) @@ -340,7 +337,7 @@ ( [x0*y0]<<32 will completely overflow ) ADD2 ,&z0 LDR2 ADD2 ( sum += x0*y1<<16 + x1*y0<<16 ) ,&z1 LDR2 - RTN + JMP2r [ &x0 $2 &x1 $2 &y0 $2 &y1 $2 &z0 $2 &z1 $2 ] @@ -348,18 +345,18 @@ @div32 ( x** y** -> q** ) ;_divmod32 JSR2 ;_divmod32/quo0 LDA2 ;_divmod32/quo1 LDA2 - RTN + JMP2r @mod32 ( x** y** -> r** ) ;_divmod32 JSR2 ;_divmod32/rem0 LDA2 ;_divmod32/rem1 LDA2 - RTN + JMP2r @divmod32 ( x** y** -> q** r** ) ;_divmod32 JSR2 ;_divmod32/quo0 LDA2 ;_divmod32/quo1 LDA2 ;_divmod32/rem0 LDA2 ;_divmod32/rem1 LDA2 - RTN + JMP2r ( calculate and store x / y and x % y ) @_divmod32 ( x** y** -> ) @@ -372,7 +369,7 @@ ,&div0 LDR2 ,&div1 LDR2 ;lt32 JSR2 ,&is-zero JCN ,&not-zero JMP &is-zero - #0000 ,&quo0 STR2 #0000 ,&quo1 STR2 RTN + #0000 ,&quo0 STR2 #0000 ,&quo1 STR2 JMP2r ( x >= y so the answer is >= 1 ) &not-zero @@ -412,7 +409,7 @@ ,&div0 LDR2 ,&div1 LDR2 #01 ;rshift32 JSR2 ,&div1 STR2 ,&div0 STR2 ( div >>= 1 ) ,&cur0 LDR2 ,&cur1 LDR2 #01 ;rshift32 JSR2 ,&cur1 STR2 ,&cur0 STR2 ( cur >>= 1 ) ,&cur0 LDR2 ,&cur1 LDR2 ;non-zero32 JSR2 ,&loop JCN ( if cur>0, loop. else we're done ) - RTN + JMP2r ( greatest common divisor - euclidean algorithm ) @gcd32 ( x** y** -> z** ) @@ -432,4 +429,4 @@ ,&loop JMP &done POP4 ( x ) - RTN + JMP2r