Implement std_multiply using shift + add
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
DataHoarder 2021-08-03 18:20:08 +02:00
parent c4a98361b1
commit 436d251cfd

View file

@ -90,20 +90,22 @@
; FASTCALL uint std_multiply(uint A, uint B). Return value on RRET
; Uses simple addition
; TODO: use add + shift
; Uses shift + add multiplication
.constant std_mul @std_multiply
.constant std_multiply @std_multiply
std_multiply:
PUSH BSM_COUNTER_0 ; Save old value of counter
MOV BSM_COUNTER_0, P0 ; Move A to counter
MOV RRET, 0
JUMP @.loop ; Skip first iteration
MOV S0, P0 ; S0 = A
MOV S1, P1 ; S1 = B
MOV RRET, 0 ; result = 0
.multiply:
ADD RRET, RRET, P1 ; Add B to itself A times
.loop: LOOP @.multiply ; Check if 0, if not, decrease A and go back. Use COUNTER[0]
POP BSM_COUNTER_0 ; Restore old value of counter
RET ; Return, no need to get rid of stack as fastcall passed them on registers
BEQ S0, 0, 0xFFFFFFFF, @.return ; continue if S0 != 0
BNE S0, 1, 0x1, @.skipBit ; If the lowest order bit is set in A?
ADD RRET, RRET, S1 ; result = result + b
.skipBit:
SHR S0, S0, 1 ; a = a >> 1, no sign copy
ADD S1, S1, S1 ; b = b + b
JUMP @.multiply
.return: RET ; Return, no need to get rid of stack as fastcall passed them on registers
; FASTCALL uint std_divide(uint dividend, uint divisor). Return quotient on RRET, remainder on RRET_X