PB vs C

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: PB vs C

Post by useful »

Unsigned integers and types in pointers in PB are all I need to be happy with. :oops:
Dawn will come inevitably.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB vs C

Post by idle »

useful wrote: Sun Sep 29, 2024 5:19 am Unsigned integers and types in pointers in PB are all I need to be happy with. :oops:
you can hack longs in c backend but not integers

Code: Select all

Global x.l,y.l 
;redefine x to unsigned 
!unsigned int g_x;   

x = -1
y =  1 

If x > y   ;if x is unsigend its 4294967295
  Debug x 
  Debug Hex(x)
Else       ;if x is signed it's -1     
  Debug x 
EndIf   
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: PB vs C

Post by useful »

It is clear that the built-in C gives us a lot of convenient!

But I would like to see unsigned integers and pointers with the type directly in the PB.

Code: Select all

Global x.l
;redefine x to unsigned 
!unsigned int g_x;   
x = MakeIPAddress(255,255,255,255)
Debug x 
Debug Hex(x)

Code: Select all

Global x.lu, *px.lu
*px = @x
x = MakeIPAddress(255,255,255,255)
Debug x 
Debug Hex(x)
Debug *px
Debug Hex(*px)
Dawn will come inevitably.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB vs C

Post by idle »

useful wrote: Sun Sep 29, 2024 6:55 am It is clear that the built-in C gives us a lot of convenient!

But I would like to see unsigned integers and pointers with the type directly in the PB.

Code: Select all

Global x.l
;redefine x to unsigned 
!unsigned int g_x;   
x = MakeIPAddress(255,255,255,255)
Debug x 
Debug Hex(x)

Code: Select all

Global x.lu, *px.lu
*px = @x
x = MakeIPAddress(255,255,255,255)
Debug x 
Debug Hex(x)
Debug *px
Debug Hex(*px)
yes but you still need to dereference a pointer by it's type as in debug *px\lu
because you may need to check that the pointer is set as in IF *px <> 0 ...
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: PB vs C

Post by useful »

Code: Select all

EnableExplicit
Global x.l
Global y.i
!unsigned int g_x;
!unsigned long long g_y;
x = 2147483647 + 1 ; max signed + 1
Debug x 
y = 9223372036854775807 + 1 ; max signed + 1
Debug y ; UuuuPpppSsss -9223372036854775808
;but
Debug y - 9223372036854775807 ; 1
;!!!
y = 9223372036854775807 + 256   ; max signed + 256
Debug Hex(y)
unsigned long long [int] 0 - 18 446 744 073 709 551 615

Code: Select all

EnableExplicit
Define y.i
!unsigned long long v_y;
!v_y = 18446744073709551614;
Debug Hex(y)
y+1
Debug Hex(y)
Last edited by useful on Wed Oct 23, 2024 5:06 pm, edited 1 time in total.
Dawn will come inevitably.
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: PB vs C

Post by skywalk »

Yes, always a bit of angst when interfacing a C lib that uses UINT, ULONGLONG. :cry:

Code: Select all

  ;          ; Type,   Bytes,  Min,                     Max,                     C Type
  l.l[0]     ; Long,       4, -2147483648,              2147483647,              long, int (long & $FFFFFFFF = unsigned)
  ;;ul.ul[0] ; ULong,      4,  0,                       4294967295,              unsigned long, unsigned int, DWORD(C++)
  i.i[0]     ; Integer,    4, -2147483648,              2147483647,              long, int(x86 or 32-bit),sizeof*
  ;i.i[0]    ; Integer,    8, -9223372036854775808,     9223372036854775807,     long long(x64 or 64-bit),sizeof*
  q.q[0]     ; Quad,       8, -9223372036854775808,     9223372036854775807,     long long
  ;;uq.uq[0] ; UQuad,      8,  0,                       18446744073709551615,    unsigned long long, ULONGLONG
It's been a while since I asked. What was the reason to not have native support?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB vs C

Post by idle »

skywalk wrote: Wed Oct 23, 2024 4:47 pm Yes, always a bit of angst when interfacing a C lib that uses UINT, ULONGLONG. :cry:
It's been a while since I asked. What was the reason to not have native support?
It does complicate things sometimes, ROR longs can't be done in PB because of the << >> SAL and SAR when you want SHL SHR and MOV instead of MOVSXD

Code: Select all

Procedure RORL(t.l,n) ; easy enough to do with inline asm and c 

  CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm  
    !mov rcx, [p.v_n]
    !ror dword [p.v_t] , cl 
  CompilerElse 
    !unsigned int tt =  (unsigned int) v_t;  
    !v_t = (unsigned int) ((tt >> v_n) | (tt << ((32) - v_n))); 
  CompilerEndIf   
  
  ProcedureReturn t 
  
EndProcedure 

;or use fasm macros to do unsigned shifts in PB  
CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm   
  Macro EnableUnsigned() 
    !macro SAR reg,var 
    !{
    !shr reg,var 
    !} 
    
    !macro SAL reg,var 
    !{ 
    !shl reg,var 
    !} 
    
    !macro MOVSXD reg,var  
    !{ 
    !match =dword x , var 
    !\{ mov eax, var 
    !mov reg,rax \} 
    !} 
  EndMacro 
  
  Macro DisableUnsigned() 
    !purge SAR
    !purge SAL 
    !purge MOVSXD
  EndMacro   
  
CompilerElse 
  
  Macro EnableUnsigned() : EndMacro 
  Macro DisableUnsigned() : EndMacro 
  
CompilerEndIf 

Macro _RORL(x,n)   ;test ror long with fasm macros 
  EnableUnsigned()
   x = ((x >> n) | (x << ((32) - n)))
  DisableUnsigned() 
EndMacro   

Define x.l
x = 1 << 31 
Debug rorl(x,8) ;use asm and inline c 
;8388608 
x = 1 << 31 
_rorl(x,8)      ;patch fasm to do unsigned 
;8388608  asm 
;-8388608 c backend 
Debug x 


Post Reply