nxu/math32.tal

284 lines
10 KiB
Tal
Raw Normal View History

2022-02-09 21:24:36 -05:00
( math32.tal )
( )
( This library supports arithmetic on 32-bit unsigned integers, )
( also known as long values. )
( )
( 32-bit long values are represented by two 16-bit short values: )
( )
( decimal hexadecimal uxn literals )
( 0 0x00000000 #0000 #0000 )
( 1 0x00000001 #0000 #0001 )
( 4660 0x00001234 #0000 #1234 )
( 65535 0x0000ffff #0000 #ffff )
( 65536 0x00010000 #0001 #0000 )
( 16777215 0x00ffffff #00ff #ffff )
( 4294967295 0xffffffff #ffff #ffff )
( )
2021-12-28 21:16:21 -05:00
( The most significant 16-bit, the "high bits", are stored first. )
2022-02-09 21:24:36 -05:00
( We document long values as x** -- equivalent to xhi* xlo*. )
( )
( Operations supported: )
( )
( NAME STACK EFFECT DEFINITION )
( add32 x** y** -> z** x + y )
( sub32 x** y** -> z** x - y )
( mul16 x* y* -> z** x * y )
( mul32 x** y** -> z** x * y )
( div32 x** y** -> q** x / y )
( mod32 x** y** -> r** x % y )
( divmod32 x** y** -> q** r** x / y, x % y )
2023-11-01 00:06:54 -04:00
( gcd32 x** y** -> z** gcd[x, y] )
2022-02-09 21:24:36 -05:00
( negate32 x** -> z** -x )
( lshift32 x** n^ -> z** x<<n )
( rshift32 x** n^ -> z** x>>n )
( and32 x** y** -> z** x & y )
( or32 x** y** -> z** x | y )
( xor32 x** y** -> z** x ^ y )
( complement32 x** -> z** ~x )
( eq32 x** y** -> bool^ x == y )
( ne32 x** y** -> bool^ x != y )
( is-zero32 x** -> bool^ x == 0 )
( non-zero32 x** -> bool^ x != 0 )
( lt32 x** y** -> bool^ x < y )
( gt32 x** y** -> bool^ x > y )
( lteq32 x** y** -> bool^ x <= y )
( gteq32 x** y** -> bool^ x >= y )
2023-11-01 00:06:54 -04:00
( bitcount8 x^ -> bool^ floor[log2[x]]+1 )
( bitcount16 x* -> bool^ floor[log2[x]]+1 )
( bitcount32 x** -> bool^ floor[log2[x]]+1 )
2022-02-09 21:24:36 -05:00
( )
2023-11-01 00:06:54 -04:00
( bitcount: number of bits needed to represent the number. )
( this is equivalent to floor[log2[x]] + 1 )
2021-12-26 00:47:13 -05:00
2021-12-24 16:33:59 -05:00
@bitcount8 ( x^ -> n^ )
2023-10-28 17:05:34 -04:00
LITr 00 &loop DUP ?{ POP STHr JMP2r } #01 SFT INCr !&loop
2021-12-24 16:33:59 -05:00
@bitcount16 ( x* -> n^ )
2023-10-28 17:05:34 -04:00
LITr 00 &loop ORAk ?{ POP2 STHr JMP2r } #01 SFT2 INCr !&loop
2021-12-24 16:33:59 -05:00
@bitcount32 ( x** -> n^ )
2023-10-28 17:05:34 -04:00
SWP2 bitcount16 DUP ?{ POP !bitcount16 } #10 NIP2 ADD JMP2r
2021-12-24 16:33:59 -05:00
2023-11-01 00:06:54 -04:00
( -- equality )
2021-12-26 21:01:22 -05:00
2021-12-28 16:43:05 -05:00
( x == y )
2021-12-23 00:43:10 -05:00
@eq32 ( xhi* xlo* yhi* ylo* -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 EQU2 STH EQU2 STHr AND JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x != y )
2021-12-23 00:43:10 -05:00
@ne32 ( xhi* xlo* yhi* ylo* -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 NEQ2 STH NEQ2 STHr ORA JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x == 0 )
@is-zero32 ( x** -> bool^ )
2022-02-09 21:24:36 -05:00
ORA2 #0000 EQU2 JMP2r
2021-12-28 16:43:05 -05:00
( x != 0 )
2021-12-26 01:56:43 -05:00
@non-zero32 ( x** -> bool^ )
2022-09-10 13:31:35 -04:00
ORA2 ORA JMP2r
2021-12-24 16:33:59 -05:00
2023-11-01 00:06:54 -04:00
( -- comparisons )
2021-12-26 21:01:22 -05:00
( x < y )
@lt32 ( x** y** -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 SWP2 LTH2 ?{ LTH2 JMP2r } GTH2 #00 EQU JMP2r
2021-12-26 21:01:22 -05:00
( x <= y )
@lteq32 ( x** y** -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 SWP2 GTH2 ?{ GTH2 #00 EQU JMP2r } LTH2 JMP2r
2021-12-26 21:01:22 -05:00
( x > y )
@gt32 ( x** y** -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 SWP2 GTH2 ?{ GTH2 JMP2r } LTH2 #00 EQU JMP2r
2021-12-26 21:01:22 -05:00
( x > y )
@gteq32 ( x** y** -> bool^ )
2023-10-28 17:05:34 -04:00
ROT2 SWP2 LTH2 ?{ LTH2 #00 EQU JMP2r } GTH2 JMP2r
2021-12-26 21:01:22 -05:00
2023-11-01 00:06:54 -04:00
( -- bitwise operations )
2021-12-26 21:01:22 -05:00
2021-12-28 16:43:05 -05:00
( x & y )
2023-11-01 00:06:54 -04:00
@and32 ( xhi* xlo* yhi* ylo* -> xhi&yhi* xlo&ylo* )
2022-02-09 21:24:36 -05:00
ROT2 AND2 STH2 AND2 STH2r JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x | y )
2021-12-23 00:43:10 -05:00
@or32 ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
2022-02-09 21:24:36 -05:00
ROT2 ORA2 STH2 ORA2 STH2r JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x ^ y )
2023-11-01 00:06:54 -04:00
@xor32 ( xhi* xlo* yhi* ylo* -> xhi^yhi* xlo^ylo* )
2022-02-09 21:24:36 -05:00
ROT2 EOR2 STH2 EOR2 STH2r JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( ~x )
2023-11-01 00:06:54 -04:00
@complement32 ( x** -> ~xhi* ~xlo* )
2022-09-10 13:31:35 -04:00
SWP2 #ffff EOR2 SWP2 #ffff EOR2 JMP2r
2023-11-01 00:06:54 -04:00
( -- bit-shifting )
2021-12-28 16:43:05 -05:00
( x >> n )
2023-10-28 17:05:34 -04:00
@rshift32 ( x** n^ -> x>>n )
DUP #08 LTH ?shift32-0 ( x n )
DUP #10 LTH ?rshift32-1 ( x n )
DUP #18 LTH ?rshift32-2 ( x n )
!rshift32-3 ( x n )
( shift by 0-7 bits; used by both lshift and rshift )
@shift32-0 ( x** n^ -> x>>n )
STH DUP2 STHkr SFT2 ,&z2 STR2
POP DUP2 STHkr SFT2 ,&z2 LDR ORA ,&z2 STR ,&z1 STR
POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR
LIT [ &z1 $1 ] LIT2 [ &z2 $2 ] JMP2r
2021-12-26 01:56:43 -05:00
( shift right by 8-15 bits )
2023-10-28 17:05:34 -04:00
@rshift32-1 ( x** n^ -> x>>n )
#08 SUB STH ( stash [n>>8] )
POP DUP2 STHkr SFT2 ,&z2 STR2
POP STHr SFT2 ,&z2 LDR ORA ,&z2 STR
#00 SWP LIT2 [ &z2 $2 ] JMP2r
2021-12-26 01:56:43 -05:00
( shift right by 16-23 bits )
2023-10-28 17:05:34 -04:00
@rshift32-2 ( x** n^ -> x>>n )
#10 SUB STH ( stash [n>>16] )
POP2 STHr SFT2 #0000 SWP2 JMP2r
2021-12-26 01:56:43 -05:00
( shift right by 16-23 bits )
2023-10-28 17:05:34 -04:00
@rshift32-3 ( x** n^ -> x>>n )
#18 SUB STH ( stash [n>>24] )
POP2 POP STH SWPr SFTr #00 #0000 STHr JMP2r
2021-12-26 01:56:43 -05:00
2021-12-28 16:43:05 -05:00
( x << n )
2021-12-29 01:00:07 -05:00
@lshift32 ( x** n^ -> x<<n )
2023-10-28 17:05:34 -04:00
DUP #08 LTH ?lshift32-0 ( x n )
DUP #10 LTH ?lshift32-1 ( x n )
DUP #18 LTH ?lshift32-2 ( x n )
!lshift32-3 ( x n )
2021-12-24 16:33:59 -05:00
( shift left by 0-7 bits )
2021-12-29 01:00:07 -05:00
@lshift32-0 ( x** n^ -> x<<n )
2023-10-28 17:05:34 -04:00
#40 SFT !shift32-0
2021-12-24 16:33:59 -05:00
( shift left by 8-15 bits )
2021-12-29 01:00:07 -05:00
@lshift32-1 ( x** n^ -> x<<n )
2021-12-28 15:39:20 -05:00
#08 SUB #40 SFT STH ( stash [n-8]<<4 )
2023-10-28 17:05:34 -04:00
DUP2 STHkr SFT2 ,&z1 STR2
POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR
NIP LIT2 [ &z1 $1 &z2 $1 ] #00 JMP2r
2021-12-24 16:33:59 -05:00
( shift left by 16-23 bits )
2021-12-29 01:00:07 -05:00
@lshift32-2 ( x** n^ -> x<<n )
2021-12-28 15:45:15 -05:00
#10 SUB #40 SFT STH ( stash [n-16]<<4 )
2023-10-28 17:05:34 -04:00
NIP2 STHr SFT2 #0000 JMP2r
2021-12-24 16:33:59 -05:00
( shift left by 24-31 bits )
2021-12-29 01:00:07 -05:00
@lshift32-3 ( x** n^ -> x<<n )
2023-10-28 17:05:34 -04:00
#18 SUB #40 SFT ( stash [n-24]<<4 )
SFT NIP2 NIP #0000 #00 JMP2r
2021-12-24 16:33:59 -05:00
2023-11-01 00:06:54 -04:00
( -- arithmetic )
2021-12-26 21:01:22 -05:00
2021-12-28 16:43:05 -05:00
( x + y )
2021-12-23 00:43:10 -05:00
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
2023-10-28 17:05:34 -04:00
ROT2 STH2k ADD2 STH2k ROT2 ROT2 GTH2r #00 STHr ADD2 ADD2 SWP2 JMP2r
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( -x )
2021-12-26 21:01:22 -05:00
@negate32 ( x** -> -x** )
2023-10-28 17:05:34 -04:00
complement32 INC2 ORAk ?{ SWP2 INC2 SWP2 } JMP2r
2021-12-26 21:01:22 -05:00
2021-12-28 16:43:05 -05:00
( x - y )
@sub32 ( x** y** -> z** )
2023-10-28 17:05:34 -04:00
ROT2 STH2k SWP2 SUB2 STH2k ROT2 ROT2 LTH2r #00 STHr ADD2 SUB2 SWP2 JMP2r
2021-12-27 15:51:59 -05:00
( 16-bit multiplication )
2021-12-23 00:43:10 -05:00
@mul16 ( x* y* -> z** )
2023-10-28 17:05:34 -04:00
,&y1 STR ,&y0 STR ( save ylo, yhi )
,&x1 STR ,&x0 STR ( save xlo, xhi )
#0000 ,&z1 STR ,&w0 STR ( reset z1 and w0 )
2021-12-23 00:43:10 -05:00
( x1 * y1 => z1z2 )
2023-10-28 17:05:34 -04:00
LIT2 00 [ &x1 $1 ] LIT2 00 [ &y1 $1 ] MUL2 ,&z3 STR ,&z2 STR
2021-12-23 00:43:10 -05:00
( x0 * y1 => z0z1 )
2023-10-28 17:05:34 -04:00
#00 ,&x0 LDR #00 ,&y1 LDR MUL2 ,&z1 LDR2 ADD2 ,&z1 STR2
2021-12-23 00:43:10 -05:00
2021-12-29 01:02:52 -05:00
( x1 * y0 => w1w2 )
2023-10-28 17:05:34 -04:00
#00 ,&x1 LDR #00 ,&y0 LDR MUL2 ,&w2 STR ,&w1 STR
2021-12-23 00:43:10 -05:00
2021-12-29 01:02:52 -05:00
( x0 * y0 => w0w1 )
2023-10-28 17:05:34 -04:00
LIT2 00 [ &x0 $1 ] LIT2 00 [ &y0 $1 ] MUL2 ,&w0 LDR2 ADD2 ,&w0 STR2
2021-12-23 00:43:10 -05:00
2021-12-23 01:01:09 -05:00
( add z and a<<8 )
2023-10-28 17:05:34 -04:00
#00 LIT2 [ &z1 $1 &z2 $1 ] LIT [ &z3 $1 ]
LIT2 [ &w0 $1 &w1 $1 ] LIT [ &w2 $1 ] #00
!add32
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x * y )
2022-03-07 21:31:47 -05:00
@mul32 ( x** y** -> z** )
2023-10-28 17:05:34 -04:00
ROT2k ( x0* x1* y0* y1* y0* y1* x1* )
mul16 ,&z1 STR2 ,&z0 STR2 POP2 ( x0* x1* y0* y1* ; sum = [x1*y1] )
STH2 ROT2 STH2 ( x1* y0* [y1* x0*] )
MUL2r MUL2 STH2r ADD2 ( x1*y0+y1*x0* )
2021-12-23 00:43:10 -05:00
( [x0*y0]<<32 will completely overflow )
2023-10-28 17:05:34 -04:00
LIT2 [ &z0 $2 ] ADD2 ( sum += [x0*y1+x1*y0]<<16 )
LIT2 [ &z1 $2 ] JMP2r
2021-12-23 00:43:10 -05:00
2023-11-01 00:06:54 -04:00
( x / y )
2021-12-27 01:20:24 -05:00
@div32 ( x** y** -> q** )
2023-10-28 17:05:34 -04:00
z_divmod32 ;z_divmod32/quo0 LDA2 ;z_divmod32/quo1 LDA2 JMP2r
2021-12-29 16:17:01 -05:00
2023-11-01 00:06:54 -04:00
( x % y )
2021-12-29 16:17:01 -05:00
@mod32 ( x** y** -> r** )
2023-10-28 17:05:34 -04:00
z_divmod32 ;z_divmod32/rem0 LDA2 ;z_divmod32/rem1 LDA2 JMP2r
2021-12-29 16:17:01 -05:00
2023-11-01 00:06:54 -04:00
( x / y, x % y )
2021-12-29 16:17:01 -05:00
@divmod32 ( x** y** -> q** r** )
2023-10-28 17:05:34 -04:00
z_divmod32
;z_divmod32/quo0 LDA2 ;z_divmod32/quo1 LDA2
;z_divmod32/rem0 LDA2 ;z_divmod32/rem1 LDA2
2022-02-09 21:24:36 -05:00
JMP2r
2021-12-29 16:17:01 -05:00
2023-11-01 00:06:54 -04:00
( private: calculate and store x / y and x % y )
2023-10-28 17:05:34 -04:00
@z_divmod32 ( x** y** -> )
2023-11-01 00:06:54 -04:00
( ; store y and x for repeated use )
#0000 DUP2 ,&quo0 STR2 ,&quo1 STR2 ( x** y** ; quo<-0 )
STH2k ,&div1 STR2 STH2k ,&div0 STR2 ( x** [ylo* yhi*] ; div<-y )
OVR2 OVR2 ,&rem1 STR2 ,&rem0 STR2 ( x** [ylo* yhi*] ; rem<-x )
OVR2 OVR2 STH2r STH2r ( x** x** y** )
OVR2 OVR2 STH2 STH2 ( x** x** y** [ylo* yhi*] )
gteq32 ?{ POP2 POP2 POP2r POP2r JMP2r } ( x** [ylo* yhi*] ; return if x < y )
( ; bitcount[x] - bitcount[y] determines largest multiple of y to try )
bitcount32 STH2r STH2r bitcount32 SUB ( shift=rbits-dits^ )
#00 DUP2 ( shift^ 0^ shift^ 0^ )
#0000 INC2k ROT2 POP ( shift^ 0^ 0* 1* shift^ )
lshift32 ,&cur1 STR2 ,&cur0 STR2 ( shift^ 0^ ; cur<-1<<shift )
,&div0 LDR2 ,&div1 LDR2 ROT2 POP ( div** shift^ )
lshift32 ,&div1 STR2 ,&div0 STR2 ( ; div<-div<<shift )
2021-12-29 16:17:01 -05:00
&loop
2023-11-01 00:06:54 -04:00
( ; if rem >= cur [current divisor], we can subtract it and add to quotient )
( ; otherwise, skip that iteration and reduce cur. )
LIT2 [ &rem0 $2 ] LIT2 [ &rem1 $2 ] ,&div0 LDR2 ,&div1 LDR2
lt32 ?{
( ; since rem >= div, we have found a multiple of y that divides x )
,&rem0 LDR2 ,&rem1 LDR2 ( rem** )
LIT2 [ &div0 $2 ] LIT2 [ &div1 $2 ] ( rem** div** )
sub32 ,&rem1 STR2 ,&rem0 STR2 ( ; rem<-rem-div** )
LIT2 [ &quo0 $2 ] LIT2 [ &quo1 $2 ] ( quo** )
LIT2 [ &cur0 $2 ] LIT2 [ &cur1 $2 ] ( quo** cur** )
add32 ,&quo1 STR2 ,&quo0 STR2 ( ; quo<-quo+cur** )
}
,&div0 LDR2 ,&div1 LDR2 #01 rshift32 ( div>>1** )
,&div1 STR2 ,&div0 STR2 ( ; div<-div>>1 )
,&cur0 LDR2 ,&cur1 LDR2 #01 rshift32 ( cur>>1** )
OVR2 OVR2 ,&cur1 STR2 ,&cur0 STR2 ( cur>>1** ; cur<-cur>>1 )
non-zero32 ?&loop JMP2r ( ; loop if cur>0, else we're done )
2021-12-29 17:34:07 -05:00
( greatest common divisor - euclidean algorithm )
@gcd32 ( x** y** -> z** )
2023-10-28 17:05:34 -04:00
&loop OVR2 OVR2 is-zero32 ?{ ( x** y** )
OVR2 OVR2 STH2 STH2 ( x** y** [y**] )
mod32 ( r=x%y** [y**] )
STH2r ROT2 ROT2 ( yhi* rhi* rlo* [ylo*] )
STH2r ROT2 ROT2 !&loop ( y** r** )
} POP2 POP2 JMP2r ( z** )