Page 2 of 2

Re: [SOLVED] Trying to make AbsInt()

Posted: Fri Oct 15, 2021 2:56 am
by Rinzwind
There is no speed difference between the simplest and hardest way (didn't check the asm since 32 bit only). Heck, even just Val manages just fine in practice. Not a useful place to optimize since you need a zillion loop to even measure it. Anyway, interesting read.

Re: [SOLVED] Trying to make AbsInt()

Posted: Fri Oct 22, 2021 5:18 pm
by mk-soft
Long time later ...

Update - Add AbsInt

Code: Select all

;-TOP my mk-soft

Macro AbsL(Value)
  (Value * (Value >> 31) | 1)
EndMacro

Macro AbsQ(Value)
  (Value * (Value >> 63) | 1)
EndMacro

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  Macro AbsInt(Value)
    (Value * (Value >> 31) | 1)
  EndMacro
CompilerElse
  Macro AbsInt(Value)
    (Value * (Value >> 63) | 1)
  EndMacro
CompilerEndIf

; ----

a.l = -2000001
r1.q = AbsL(a)
Debug r1

b.q = -200000000001
r1.q = AbsQ(b)
Debug r1

c.i = -12345678
r1.q = AbsInt(c)
Debug r1

c.i = 1234
r1.q = AbsInt(c)
Debug r1
ASM
; a.l = -2000001
MOV dword [v_a],-2000001
; r1.q = AbsL(a)
MOVSXD r15,dword [v_a]
MOVSXD r14,dword [v_a]
SAR r14,31
OR r14,1
IMUL r15,r14
MOV qword [v_r1],r15
; Debug r1
;
; b.q = -200000000001
MOV rax,-200000000001
MOV qword [v_b],rax
; r1.q = AbsQ(b)
MOV r15,qword [v_b]
MOV r14,qword [v_b]
SAR r14,63
OR r14,1
IMUL r15,r14
MOV qword [v_r1],r15
; Debug r1
;
;