Files
symphony_stdlib/src/imath.asm
T

92 lines
1.9 KiB
NASM

; Multiplies r1 and r2, returning the lower part of the result
; Arguments:
; r1 - The first value
; r2 - The second value
; Result:
; r1 - The lower 32 bits of the result
; Clobbers: r2, r3, r4, r5
pub mul_low:
add r3, r2, r2 ; r3 = r2 * 2
mov r4, 0 ; r4 has the result
mul_low_loop:
and r5, r1, 1 ; a0
neg r5, r5 ; mask
and r5, r2, r5 ; a0 ? r2 : 0
add r4, r4, r5
and r5, r1, 2 ; a1 is now 0 or 2
lsr r5, r5, 1 ; normalize to 0/1
neg r5, r5 ; mask
and r5, r3, r5 ; a1 ? r2 * 2 : 0
add r4, r4, r5
lsl r2, r2, 2
lsl r3, r3, 2
lsr r1, r1, 2
cmp r1, zr
jne mul_low_loop
mov r1, r4
jmp r13
; Calculates the absolute value of the value provided in the r1 register
; Arguments:
; r1 - The value for which we want the absolute value
; Result:
; r1 - The calculated absolute value
; Clobbers: r2
; Info: Based on Stanford's BitHacks
pub abs: ; SHOULD BE INLINED
; mask = v >> 31
asr r2, r1, 31
; v + mask
add r1, r1, r2
; return (v + mask) ^ mask
xor r1, r1, r2
jmp r13
; Calculates the minimum value of the two values provided in the r1 and r2 registers
; Arguments:
; r1 - The first value
; r2 - The second value
; Result:
; r1 - The smaller value
; Clobbers: Nothing
; Info: Based on Stanford's BitHacks
pub min: ; SHOULD BE INLINED
; x < y
cmp r1, r2
lsr flags, flags, 2
; -(x < y)
neg flags, flags
; x ^ y
xor r1, r1, r2
; (x ^ y) & -(x < y)
and r1, r1, flags
; return y ^ ((x ^ y) & -(x < y))
xor r1, r2, r1
jmp r13
; Calculates the maximum value of the two values provided in the r1 and r2 registers
; Arguments:
; r1 - The first value
; r2 - The second value
; Result:
; r1 - The smaller value
; Clobbers: r2
; Info: Based on Stanford's BitHacks
pub max: ; SHOULD BE INLINED
; x < y
cmp r1, r2
lsr flags, flags, 2
; -(x < y)
neg flags, flags
; x ^ y
xor r2, r1, r2
; (x ^ y) & -(x < y)
and r2, r2, flags
; return x ^ ((x ^ y) & -(x < y))
xor r1, r1, r2
jmp r13