nxu/math32.tal

284 lines
11 KiB
Tal
Raw Permalink 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 )
2024-09-09 13:23:24 -04:00
( u32-add x** y** -> z** x + y )
( u32-sub x** y** -> z** x - y )
( u32-mul x** y** -> z** x * y )
( u32-mul16 x* y* -> z** x * y )
( u32-div x** y** -> q** x / y )
( u32-mod x** y** -> r** x % y )
( u32-divmod x** y** -> q** r** x / y, x % y )
( u32-gcd x** y** -> z** gcd[x, y] )
( u32-negate x** -> z** -x )
( u32-lshift x** n^ -> z** x<<n )
( u32-rshift x** n^ -> z** x>>n )
( u32-and x** y** -> z** x & y )
( u32-or x** y** -> z** x | y )
( u32-xor x** y** -> z** x ^ y )
( u32-complement x** -> z** ~x )
( u32-eq x** y** -> bool^ x == y )
( u32-ne x** y** -> bool^ x != y )
( u32-is-zero x** -> bool^ x == 0 )
( u32-non-zero x** -> bool^ x != 0 )
( u32-lt x** y** -> bool^ x < y )
( u32-gt x** y** -> bool^ x > y )
( u32-lteq x** y** -> bool^ x <= y )
( u32-gteq x** y** -> bool^ x >= y )
( u8-bitcount x^ -> bool^ floor[log2[x]]+1 )
( u16-bitcount x* -> bool^ floor[log2[x]]+1 )
( u32-bitcount 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
2024-09-09 13:23:24 -04:00
@u8-bitcount ( 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
2024-09-09 13:23:24 -04:00
@u16-bitcount ( 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
2024-09-09 13:23:24 -04:00
@u32-bitcount ( x** -> n^ )
SWP2 u16-bitcount DUP ?{ POP !u16-bitcount } #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 )
2024-09-09 13:23:24 -04:00
@u32-eq ( 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 )
2024-09-09 13:23:24 -04:00
@u32-ne ( 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 )
2024-09-09 13:23:24 -04:00
@u32-is-zero ( x** -> bool^ )
2022-02-09 21:24:36 -05:00
ORA2 #0000 EQU2 JMP2r
2021-12-28 16:43:05 -05:00
( x != 0 )
2024-09-09 13:23:24 -04:00
@u32-non-zero ( 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 )
2024-09-09 13:23:24 -04:00
@u32-lt ( 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 )
2024-09-09 13:23:24 -04:00
@u32-lteq ( 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 )
2024-09-09 13:23:24 -04:00
@u32-gt ( 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 )
2024-09-09 13:23:24 -04:00
@u32-gteq ( 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 )
2024-09-09 13:23:24 -04:00
@u32-and ( 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 )
2024-09-09 13:23:24 -04:00
@u32-or ( 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 )
2024-09-09 13:23:24 -04:00
@u32-xor ( 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 )
2024-09-09 13:23:24 -04:00
@u32-complement ( 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 )
2024-09-09 13:23:24 -04:00
@u32-rshift ( x** n^ -> x>>n )
DUP #08 LTH ?u32-shift-0 ( x n )
DUP #10 LTH ?u32-rshift-1 ( x n )
DUP #18 LTH ?u32-rshift-2 ( x n )
!u32-rshift-3 ( x n )
2023-10-28 17:05:34 -04:00
( shift by 0-7 bits; used by both lshift and rshift )
2024-09-09 13:23:24 -04:00
@u32-shift-0 ( x** n^ -> x>>n )
2023-10-28 17:05:34 -04:00
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 )
2024-09-09 13:23:24 -04:00
@u32-rshift-1 ( x** n^ -> x>>n )
2023-10-28 17:05:34 -04:00
#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 )
2024-09-09 13:23:24 -04:00
@u32-rshift-2 ( x** n^ -> x>>n )
2023-10-28 17:05:34 -04:00
#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 )
2024-09-09 13:23:24 -04:00
@u32-rshift-3 ( x** n^ -> x>>n )
2023-10-28 17:05:34 -04:00
#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 )
2024-09-09 13:23:24 -04:00
@u32-lshift ( x** n^ -> x<<n )
DUP #08 LTH ?u32-lshift-0 ( x n )
DUP #10 LTH ?u32-lshift-1 ( x n )
DUP #18 LTH ?u32-lshift-2 ( x n )
!u32-lshift-3 ( x n )
2021-12-24 16:33:59 -05:00
( shift left by 0-7 bits )
2024-09-09 13:23:24 -04:00
@u32-lshift-0 ( x** n^ -> x<<n )
#40 SFT !u32-shift-0
2021-12-24 16:33:59 -05:00
( shift left by 8-15 bits )
2024-09-09 13:23:24 -04:00
@u32-lshift-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 )
2024-09-09 13:23:24 -04:00
@u32-lshift-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 )
2024-09-09 13:23:24 -04:00
@u32-lshift-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 )
2024-09-09 13:23:24 -04:00
@u32-add ( 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 )
2024-09-09 13:23:24 -04:00
@u32-negate ( x** -> -x** )
u32-complement INC2 ORAk ?{ SWP2 INC2 SWP2 } JMP2r
2021-12-26 21:01:22 -05:00
2021-12-28 16:43:05 -05:00
( x - y )
2024-09-09 13:23:24 -04:00
@u32-sub ( 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 )
2024-09-09 13:23:24 -04:00
@u32-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
2024-09-09 13:23:24 -04:00
!u32-add
2021-12-23 00:43:10 -05:00
2021-12-28 16:43:05 -05:00
( x * y )
2024-09-09 13:23:24 -04:00
@u32-mul ( x** y** -> z** )
2023-10-28 17:05:34 -04:00
ROT2k ( x0* x1* y0* y1* y0* y1* x1* )
2024-09-09 13:23:24 -04:00
u32-mul16 ,&z1 STR2 ,&z0 STR2 POP2 ( x0* x1* y0* y1* ; sum = [x1*y1] )
2023-10-28 17:05:34 -04:00
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 )
2024-09-09 13:23:24 -04:00
@u32-div ( x** y** -> q** )
z_u32-divmod ;z_u32-divmod/quo0 LDA2 ;z_u32-divmod/quo1 LDA2 JMP2r
2021-12-29 16:17:01 -05:00
2023-11-01 00:06:54 -04:00
( x % y )
2024-09-09 13:23:24 -04:00
@u32-mod ( x** y** -> r** )
z_u32-divmod ;z_u32-divmod/rem0 LDA2 ;z_u32-divmod/rem1 LDA2 JMP2r
2021-12-29 16:17:01 -05:00
2023-11-01 00:06:54 -04:00
( x / y, x % y )
2024-09-09 13:23:24 -04:00
@u32-divmod ( x** y** -> q** r** )
z_u32-divmod
;z_u32-divmod/quo0 LDA2 ;z_u32-divmod/quo1 LDA2
;z_u32-divmod/rem0 LDA2 ;z_u32-divmod/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 )
2024-09-09 13:23:24 -04:00
@z_u32-divmod ( 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*] )
2024-09-09 13:23:24 -04:00
u32-gteq ?{ POP2 POP2 POP2r POP2r JMP2r } ( x** [ylo* yhi*] ; return if x < y )
2023-11-01 00:06:54 -04:00
( ; bitcount[x] - bitcount[y] determines largest multiple of y to try )
2024-09-09 13:23:24 -04:00
u32-bitcount STH2r STH2r u32-bitcount SUB ( shift=rbits-dits^ )
2023-11-01 00:06:54 -04:00
#00 DUP2 ( shift^ 0^ shift^ 0^ )
#0000 INC2k ROT2 POP ( shift^ 0^ 0* 1* shift^ )
2024-09-09 13:23:24 -04:00
u32-lshift ,&cur1 STR2 ,&cur0 STR2 ( shift^ 0^ ; cur<-1<<shift )
2023-11-01 00:06:54 -04:00
,&div0 LDR2 ,&div1 LDR2 ROT2 POP ( div** shift^ )
2024-09-09 13:23:24 -04:00
u32-lshift ,&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
2024-09-09 13:23:24 -04:00
u32-lt ?{
2023-11-01 00:06:54 -04:00
( ; 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** )
2024-09-09 13:23:24 -04:00
u32-sub ,&rem1 STR2 ,&rem0 STR2 ( ; rem<-rem-div** )
2023-11-01 00:06:54 -04:00
LIT2 [ &quo0 $2 ] LIT2 [ &quo1 $2 ] ( quo** )
LIT2 [ &cur0 $2 ] LIT2 [ &cur1 $2 ] ( quo** cur** )
2024-09-09 13:23:24 -04:00
u32-add ,&quo1 STR2 ,&quo0 STR2 ( ; quo<-quo+cur** )
2023-11-01 00:06:54 -04:00
}
2024-09-09 13:23:24 -04:00
,&div0 LDR2 ,&div1 LDR2 #01 u32-rshift ( div>>1** )
2023-11-01 00:06:54 -04:00
,&div1 STR2 ,&div0 STR2 ( ; div<-div>>1 )
2024-09-09 13:23:24 -04:00
,&cur0 LDR2 ,&cur1 LDR2 #01 u32-rshift ( cur>>1** )
2023-11-01 00:06:54 -04:00
OVR2 OVR2 ,&cur1 STR2 ,&cur0 STR2 ( cur>>1** ; cur<-cur>>1 )
2024-09-09 13:23:24 -04:00
u32-non-zero ?&loop JMP2r ( ; loop if cur>0, else we're done )
2021-12-29 17:34:07 -05:00
( greatest common divisor - euclidean algorithm )
2024-09-09 13:23:24 -04:00
@u32-gcd ( x** y** -> z** )
&loop OVR2 OVR2 u32-is-zero ?{ ( x** y** )
2023-10-28 17:05:34 -04:00
OVR2 OVR2 STH2 STH2 ( x** y** [y**] )
2024-09-09 13:23:24 -04:00
u32-mod ( r=x%y** [y**] )
2023-10-28 17:05:34 -04:00
STH2r ROT2 ROT2 ( yhi* rhi* rlo* [ylo*] )
STH2r ROT2 ROT2 !&loop ( y** r** )
} POP2 POP2 JMP2r ( z** )