Share your advanced PureBasic knowledge/code with the community.
-
mk-soft
- Always Here
- Posts: 5406
- Joined: Fri May 12, 2006 6:51 pm
- Location: Germany
Post
by mk-soft »
Simple Unsigned Macro...
The compiler takes over the optimization
Little John´s code is shorter...
Jump
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
Last edited by
mk-soft on Sat Jul 27, 2019 6:46 pm, edited 2 times in total.
-
Tenaja
- Addict
- Posts: 1949
- Joined: Tue Nov 09, 2010 10:15 pm
Post
by Tenaja »
Do you not get the same answer as multiplying by -1? The macros do not work with positive numbers anyway.
-
Little John
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Post
by Little John »
@mk-soft:
Cool!
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
-
mk-soft
- Always Here
- Posts: 5406
- Joined: Fri May 12, 2006 6:51 pm
- Location: Germany
Post
by mk-soft »
Little John wrote:@mk-soft:
Cool!
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
-
Little John
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Post
by Little John »
Well, the compiler can't improve readability of the code ...
-
mk-soft
- Always Here
- Posts: 5406
- Joined: Fri May 12, 2006 6:51 pm
- Location: Germany
Post
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.