efficiency improvements, subtraction, etc

This commit is contained in:
~d6 2021-12-23 19:50:46 -05:00
parent 1f2ee6cef4
commit 729a4fa70e
1 changed files with 44 additions and 14 deletions

View File

@ -18,6 +18,8 @@
%Z { #fedc #ba98 }
|0100
( run a bunch of test cases )
#ffff #ffff #2a ;mul16 ;emit-short ;test16 JSR2
#0001 #ffff #2a ;mul16 ;emit-short ;test16 JSR2
#ffff #0001 #2a ;mul16 ;emit-short ;test16 JSR2
@ -36,6 +38,14 @@
#fedc #0000 #0000 #0000 #2b ;add32 ;emit-long ;test32 JSR2
#0000 #0000 #0000 #0000 #2b ;add32 ;emit-long ;test32 JSR2
NEWLINE
#1234 #1234 #0000 #0001 #2d ;sub32 ;emit-long ;test32 JSR2
#1234 #1234 #0000 #1234 #2d ;sub32 ;emit-long ;test32 JSR2
#1234 #1234 #1234 #0001 #2d ;sub32 ;emit-long ;test32 JSR2
#1234 #1234 #1000 #0000 #2d ;sub32 ;emit-long ;test32 JSR2
#1234 #1234 #ffff #ffff #2d ;sub32 ;emit-long ;test32 JSR2
NEWLINE
X Y #26 ;and32 ;emit-long ;test32 JSR2
X Y #7c ;or32 ;emit-long ;test32 JSR2
X Y #5e ;xor32 ;emit-long ;test32 JSR2
@ -109,9 +119,23 @@ RTN
ROT2 EOR2 TOR2 EOR2 SWP2
RTN
@left-by-16 ( x** -> x<<16** )
SWP2 ( x2 x3 x0 x1 )
POP2 #0000 ( x2 x3 0000 )
%COMPLEMENT32 { SWP2 #ffff EOR2 SWP2 #ffff EOR2 }
@complement32 ( x** -> ~x** )
COMPLEMENT32
RTN
@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
@left-by-16 ( xhi* xlo* -> xlo* 0000 )
SWP2 POP2 #0000
RTN
@add32 ( xhi* xlo* yhi* ylo* -> zhi* zlo* )
@ -137,12 +161,15 @@ RTN
( 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 ]
@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 )
@ -174,31 +201,34 @@ RTN
@mul32 ( x** y** -> z** )
,&y1 STR2 ,&y0 STR2 ( save ylo, yhi )
,&x1 STR2 ,&x0 STR2 ( save xlo, xhi )
,&y1 LDR2 ,&x1 LDR2 ;mul16 JSR2 ( sum = [x1*y1] )
,&y1 LDR2 ,&x0 LDR2 ;mul16 JSR2 ;left-by-16 JSR2 ;add32 JSR2 ( sum += [x0*y1]<<16 )
,&y0 LDR2 ,&x1 LDR2 ;mul16 JSR2 ;left-by-16 JSR2 ;add32 JSR2 ( sum += [x1*y0]<<16 )
,&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 ]
@emit-long ( hi* lo* -> )
SWP2
SWP2 ( lo* hi* )
;emit-short JSR2
;emit-short JSR2
RTN
%EMIT-BYTE { DUP #04 SFT DIGIT #0f AND DIGIT }
@emit-short ( x* -> )
SWP ( lo^ hi^ )
DUP #04 SFT DIGIT ( emit hi>>4 )
#0f AND DIGIT ( emit hi&f )
DUP #04 SFT DIGIT ( emit lo>>4 )
#0f AND DIGIT ( emit lo&f )
EMIT-BYTE EMIT-BYTE
RTN
@emit-byte ( x^ -> )
DUP #04 SFT DIGIT ( emit hi>>4 )
#0f AND DIGIT ( emit hi&f )
EMIT-BYTE
RTN
@digits