stack-machine/asm/lib/signed_mult.txt

33 lines
631 B
Plaintext

## Uses the built-in unsigned multiplier to do signed 32-bit multiplication:
##
## (x+(2**32)x[31])(y+(2**32)y[31]) =
##
## xy + (2**32)(x[31]y + y[31]x) + (2**64)x[31]y[31]
##
## Anything above bit 64 is ignored, so we just need to subtract
##
## x[31]y + y[31]x
##
## from the MSW (bits 32 to 63) of the unsigned result. The LSW is fine as-is.
## The interface is the same as the normal 'mult' instruction.
# ( x y -- lsw msw )
signed_mult:
jump .ypos rel ge
over
jump .xtest rel
.ypos:
immed 0
.xtest:
push
swap
jump .xpos rel ge
over
pop
add
push
.xpos:
mult
pop
sub return