nxu/math32.tal

284 lines
11 KiB
Tal

( 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 )
( )
( The most significant 16-bit, the "high bits", are stored first. )
( We document long values as x** -- equivalent to xhi* xlo*. )
( )
( Operations supported: )
( )
( NAME STACK EFFECT DEFINITION )
( 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 )
( )
( bitcount: number of bits needed to represent the number. )
( this is equivalent to floor[log2[x]] + 1 )
@u8-bitcount ( x^ -> n^ )
LITr 00 &loop DUP ?{ POP STHr JMP2r } #01 SFT INCr !&loop
@u16-bitcount ( x* -> n^ )
LITr 00 &loop ORAk ?{ POP2 STHr JMP2r } #01 SFT2 INCr !&loop
@u32-bitcount ( x** -> n^ )
SWP2 u16-bitcount DUP ?{ POP !u16-bitcount } #10 NIP2 ADD JMP2r
( -- equality )
( x == y )
@u32-eq ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 EQU2 STH EQU2 STHr AND JMP2r
( x != y )
@u32-ne ( xhi* xlo* yhi* ylo* -> bool^ )
ROT2 NEQ2 STH NEQ2 STHr ORA JMP2r
( x == 0 )
@u32-is-zero ( x** -> bool^ )
ORA2 #0000 EQU2 JMP2r
( x != 0 )
@u32-non-zero ( x** -> bool^ )
ORA2 ORA JMP2r
( -- comparisons )
( x < y )
@u32-lt ( x** y** -> bool^ )
ROT2 SWP2 LTH2 ?{ LTH2 JMP2r } GTH2 #00 EQU JMP2r
( x <= y )
@u32-lteq ( x** y** -> bool^ )
ROT2 SWP2 GTH2 ?{ GTH2 #00 EQU JMP2r } LTH2 JMP2r
( x > y )
@u32-gt ( x** y** -> bool^ )
ROT2 SWP2 GTH2 ?{ GTH2 JMP2r } LTH2 #00 EQU JMP2r
( x > y )
@u32-gteq ( x** y** -> bool^ )
ROT2 SWP2 LTH2 ?{ LTH2 #00 EQU JMP2r } GTH2 JMP2r
( -- bitwise operations )
( x & y )
@u32-and ( xhi* xlo* yhi* ylo* -> xhi&yhi* xlo&ylo* )
ROT2 AND2 STH2 AND2 STH2r JMP2r
( x | y )
@u32-or ( xhi* xlo* yhi* ylo* -> xhi|yhi* xlo|ylo* )
ROT2 ORA2 STH2 ORA2 STH2r JMP2r
( x ^ y )
@u32-xor ( xhi* xlo* yhi* ylo* -> xhi^yhi* xlo^ylo* )
ROT2 EOR2 STH2 EOR2 STH2r JMP2r
( ~x )
@u32-complement ( x** -> ~xhi* ~xlo* )
SWP2 #ffff EOR2 SWP2 #ffff EOR2 JMP2r
( -- bit-shifting )
( x >> n )
@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 )
( shift by 0-7 bits; used by both lshift and rshift )
@u32-shift-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
( shift right by 8-15 bits )
@u32-rshift-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
( shift right by 16-23 bits )
@u32-rshift-2 ( x** n^ -> x>>n )
#10 SUB STH ( stash [n>>16] )
POP2 STHr SFT2 #0000 SWP2 JMP2r
( shift right by 16-23 bits )
@u32-rshift-3 ( x** n^ -> x>>n )
#18 SUB STH ( stash [n>>24] )
POP2 POP STH SWPr SFTr #00 #0000 STHr JMP2r
( x << n )
@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 )
( shift left by 0-7 bits )
@u32-lshift-0 ( x** n^ -> x<<n )
#40 SFT !u32-shift-0
( shift left by 8-15 bits )
@u32-lshift-1 ( x** n^ -> x<<n )
#08 SUB #40 SFT STH ( stash [n-8]<<4 )
DUP2 STHkr SFT2 ,&z1 STR2
POP STHr SFT2 ,&z1 LDR ORA ,&z1 STR
NIP LIT2 [ &z1 $1 &z2 $1 ] #00 JMP2r
( shift left by 16-23 bits )
@u32-lshift-2 ( x** n^ -> x<<n )
#10 SUB #40 SFT STH ( stash [n-16]<<4 )
NIP2 STHr SFT2 #0000 JMP2r
( shift left by 24-31 bits )
@u32-lshift-3 ( x** n^ -> x<<n )
#18 SUB #40 SFT ( stash [n-24]<<4 )
SFT NIP2 NIP #0000 #00 JMP2r
( -- arithmetic )
( x + y )
@u32-add ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
ROT2 STH2k ADD2 STH2k ROT2 ROT2 GTH2r #00 STHr ADD2 ADD2 SWP2 JMP2r
( -x )
@u32-negate ( x** -> -x** )
u32-complement INC2 ORAk ?{ SWP2 INC2 SWP2 } JMP2r
( x - y )
@u32-sub ( x** y** -> z** )
ROT2 STH2k SWP2 SUB2 STH2k ROT2 ROT2 LTH2r #00 STHr ADD2 SUB2 SWP2 JMP2r
( 16-bit multiplication )
@u32-mul16 ( x* y* -> z** )
,&y1 STR ,&y0 STR ( save ylo, yhi )
,&x1 STR ,&x0 STR ( save xlo, xhi )
#0000 ,&z1 STR ,&w0 STR ( reset z1 and w0 )
( x1 * y1 => z1z2 )
LIT2 00 [ &x1 $1 ] LIT2 00 [ &y1 $1 ] MUL2 ,&z3 STR ,&z2 STR
( x0 * y1 => z0z1 )
#00 ,&x0 LDR #00 ,&y1 LDR MUL2 ,&z1 LDR2 ADD2 ,&z1 STR2
( x1 * y0 => w1w2 )
#00 ,&x1 LDR #00 ,&y0 LDR MUL2 ,&w2 STR ,&w1 STR
( x0 * y0 => w0w1 )
LIT2 00 [ &x0 $1 ] LIT2 00 [ &y0 $1 ] MUL2 ,&w0 LDR2 ADD2 ,&w0 STR2
( add z and a<<8 )
#00 LIT2 [ &z1 $1 &z2 $1 ] LIT [ &z3 $1 ]
LIT2 [ &w0 $1 &w1 $1 ] LIT [ &w2 $1 ] #00
!u32-add
( x * y )
@u32-mul ( x** y** -> z** )
ROT2k ( x0* x1* y0* y1* y0* y1* x1* )
u32-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* )
( [x0*y0]<<32 will completely overflow )
LIT2 [ &z0 $2 ] ADD2 ( sum += [x0*y1+x1*y0]<<16 )
LIT2 [ &z1 $2 ] JMP2r
( x / y )
@u32-div ( x** y** -> q** )
z_u32-divmod ;z_u32-divmod/quo0 LDA2 ;z_u32-divmod/quo1 LDA2 JMP2r
( x % y )
@u32-mod ( x** y** -> r** )
z_u32-divmod ;z_u32-divmod/rem0 LDA2 ;z_u32-divmod/rem1 LDA2 JMP2r
( x / y, x % y )
@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
JMP2r
( private: calculate and store x / y and x % y )
@z_u32-divmod ( x** y** -> )
( ; 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*] )
u32-gteq ?{ POP2 POP2 POP2r POP2r JMP2r } ( x** [ylo* yhi*] ; return if x < y )
( ; bitcount[x] - bitcount[y] determines largest multiple of y to try )
u32-bitcount STH2r STH2r u32-bitcount SUB ( shift=rbits-dits^ )
#00 DUP2 ( shift^ 0^ shift^ 0^ )
#0000 INC2k ROT2 POP ( shift^ 0^ 0* 1* shift^ )
u32-lshift ,&cur1 STR2 ,&cur0 STR2 ( shift^ 0^ ; cur<-1<<shift )
,&div0 LDR2 ,&div1 LDR2 ROT2 POP ( div** shift^ )
u32-lshift ,&div1 STR2 ,&div0 STR2 ( ; div<-div<<shift )
&loop
( ; 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
u32-lt ?{
( ; 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** )
u32-sub ,&rem1 STR2 ,&rem0 STR2 ( ; rem<-rem-div** )
LIT2 [ &quo0 $2 ] LIT2 [ &quo1 $2 ] ( quo** )
LIT2 [ &cur0 $2 ] LIT2 [ &cur1 $2 ] ( quo** cur** )
u32-add ,&quo1 STR2 ,&quo0 STR2 ( ; quo<-quo+cur** )
}
,&div0 LDR2 ,&div1 LDR2 #01 u32-rshift ( div>>1** )
,&div1 STR2 ,&div0 STR2 ( ; div<-div>>1 )
,&cur0 LDR2 ,&cur1 LDR2 #01 u32-rshift ( cur>>1** )
OVR2 OVR2 ,&cur1 STR2 ,&cur0 STR2 ( cur>>1** ; cur<-cur>>1 )
u32-non-zero ?&loop JMP2r ( ; loop if cur>0, else we're done )
( greatest common divisor - euclidean algorithm )
@u32-gcd ( x** y** -> z** )
&loop OVR2 OVR2 u32-is-zero ?{ ( x** y** )
OVR2 OVR2 STH2 STH2 ( x** y** [y**] )
u32-mod ( r=x%y** [y**] )
STH2r ROT2 ROT2 ( yhi* rhi* rlo* [ylo*] )
STH2r ROT2 ROT2 !&loop ( y** r** )
} POP2 POP2 JMP2r ( z** )