Page 1 of 1

Macro Unsigned

Posted: Sat Jul 27, 2019 3:05 pm
by mk-soft
Simple Unsigned Macro...
The compiler takes over the optimization :wink:

Little John´s code is shorter... Jump :wink:

Code: Select all

;-TOP

; Unsigned - The compiler takes over the optimization

; by mk-soft, v1.01, 27.07.2019

; Integer and quad only for save result

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  Macro Unsigned(Value)
    ((Bool(TypeOf(Value) = #PB_Byte) * $FF) | 
     (Bool(TypeOf(Value) = #PB_Ascii) * $FF) | 
     (Bool(TypeOf(Value) = #PB_Word) * $FFFF) | 
     (Bool(TypeOf(Value) = #PB_Unicode) * $FFFF) | 
     (Bool(TypeOf(Value) = #PB_Long) * $FFFFFFFF) |
     (Bool(TypeOf(Value) = #PB_Integer) * $FFFFFFFF) |
     (Bool(TypeOf(Value) = #PB_Quad) * $FFFFFFFFFFFFFFFF) & Value)
  EndMacro
CompilerElse
  Macro Unsigned(Value)
    ((Bool(TypeOf(Value) = #PB_Byte) * $FF) | 
     (Bool(TypeOf(Value) = #PB_Ascii) * $FF) | 
     (Bool(TypeOf(Value) = #PB_Word) * $FFFF) | 
     (Bool(TypeOf(Value) = #PB_Unicode) * $FFFF) | 
     (Bool(TypeOf(Value) = #PB_Long) * $FFFFFFFF) |
     (Bool(TypeOf(Value) = #PB_Integer) * $FFFFFFFFFFFFFFFF) |
     (Bool(TypeOf(Value) = #PB_Quad) * $FFFFFFFFFFFFFFFF) & Value)
  EndMacro
CompilerEndIf 

b.b = -2
s.s = Hex(Unsigned(b)) + " = " + Str(Unsigned(b))
Debug s

w.w = -2
s.s = Hex(Unsigned(w)) + " = " + Str(Unsigned(w))
Debug s

l.l = -2
s.s = Hex(Unsigned(l)) + " = " + Str(Unsigned(l))
Debug s


Re: Macro Unsigned

Posted: Sat Jul 27, 2019 4:38 pm
by Tenaja
Do you not get the same answer as multiplying by -1? The macros do not work with positive numbers anyway.

Re: Macro Unsigned

Posted: Sat Jul 27, 2019 5:51 pm
by Little John
@mk-soft:
Cool! 8)

However, it seems that it can be implemented a bit shorter: :)

Code: Select all

Macro Unsigned (_value_)
   (((1 << (8*SizeOf(_value_))) - 1) & _value_)
EndMacro


; -- Demo
b.b = -2
s.s = Hex(Unsigned(b)) + " = " + Str(Unsigned(b))
Debug s

w.w = -2
s.s = Hex(Unsigned(w)) + " = " + Str(Unsigned(w))
Debug s

l.l = -2
s.s = Hex(Unsigned(l)) + " = " + Str(Unsigned(l))
Debug s

l.l = 254
s.s = Hex(Unsigned(l)) + " = " + Str(Unsigned(l))
Debug s

Re: Macro Unsigned

Posted: Sat Jul 27, 2019 6:07 pm
by mk-soft
Little John wrote:@mk-soft:
Cool! 8)

However, it seems that it can be implemented a bit shorter: :)

Code: Select all

Macro Unsigned (_value_)
   (((1 << (8*SizeOf(_value_))) - 1) & _value_)
EndMacro
The compiler takes over the optimization too :wink:

Re: Macro Unsigned

Posted: Sat Jul 27, 2019 6:14 pm
by Little John
Well, the compiler can't improve readability of the code ... :)

Re: Macro Unsigned

Posted: Sat Jul 27, 2019 6:29 pm
by mk-soft
That's right...

SizeOf and TypeOf are compiler commands and the compiler calculates the values before and inserts the result as constant value.
; l.l = -2
MOV dword [v_l],-2
; r1.i = Unsigned(l)
MOVSXD r15,dword [v_l]
MOV rax,4294967295
AND r15,rax
MOV qword [v_r1],r15
Here you can also see that PureBasic always works Signed.
The long value is loaded with 'MOVSXD' and the signs are filled up.