add std_divide (WIP)
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
DataHoarder 2021-08-03 15:48:52 +02:00
parent 38ba3ceb45
commit 6f8d12ee32

View file

@ -83,21 +83,56 @@
.constant R15 std_EPHEMERAL_REGISTER_15
;; FASTCALL uint std_multiply(uint A, uint B). Return value on RRET
;; Uses simple addition
;; TODO: use add + shift
; FASTCALL uint std_multiply(uint A, uint B). Return value on RRET
; Uses simple addition
; TODO: use add + shift
.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
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
JUMP @.loop ; Skip first iteration
.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
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
; FASTCALL uint std_divide(uint dividend, uint divisor). Return quotient on RRET, remainder on RRET_X
; Uses shift + add division
; TODO: check validity
.constant std_div @std_divide
.constant std_divide @std_divide
std_divide:
BNE P1, 0, 0xffffffff, @.normal_division ; Handle divisor = 0
WRITE RRET, 0xffffffff, 0xffffffff ; Return both values 0xffffffff
JUMP @.return
.normal_division:
PUSH BSM_COUNTER_0 ; Save old value of counter
MOV BSM_COUNTER_0, 32 ; Move bits left to counter (8 * 4)
MOV RRET, P0 ; Move dividend to RRET (quotient)
MOV RRET_X, 0 ; Move remainder to RRET_X
MOV R0, 0
.divide:
MOV R0, RRET ; Move quotient to temp R0
ADD RRET, RRET, RRET ; quotient = quotient + quotient
ADD RRET_X, RRET_X, RRET_X ; remainder = remainder + remainder + ...
SHL R0, RRET, R0 ; quot << R0
ADD RRET_X, RRET_X, R0 ; remainder = ... + (quot << R0)
SUB R0, RRET_X, P1 ; compare rem >= divisor
BNE R0, 0, 1 <31, @.no_reduce
SUB RRET_X, RRET_X, P1 ; remainder = remainder - divisor
ADD RRET, RRET, 1 ; quotient = quotient + 1
.no_reduce:
LOOP @.divide ; Check if 0, if not, decrease bits left and go back. Use COUNTER[0]
POP BSM_COUNTER_0 ; Restore old value of counter
.return: RET ; Return, no need to get rid of stack as fastcall passed them on registers
;; x86-like definitions