unsigned types c backend

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5018
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

unsigned types c backend

Post by idle »

using unsigned types with c backend

To use unsigned types you have to declare your unsigned variables in the global scope and then declare them with inline c within your procedures and you can use them as you would normally thereafter.

As far as c is concerned your unsigned vars are locally scoped as they are being defined in either main or your procedures
As far as Pb fasm is concerned though the variables remain global.

Code: Select all

 
;pbcompilerc -d Unsigned.pb 

;note this only works with global variables, the globals are declared outside of all functions
;PB fasm sees them as global / while c sees them as local, as the inline c is scoped in your procedures
;or the c main { } procedure, so you are just ricking the syntax checker that your PB tokens exist    

CompilerIf #PB_Compiler_Backend = #PB_Backend_C 
!unsigned int v_a =0;  
!unsigned int v_b =0;
!unsigned int v_e =0;
CompilerEndIf 

Global a.l, b.l,c.l,d.l,e.l   
Global gfooa.l,gfoob.l   

a = 4294967295 ;a is unsigned 
c = 4294967295 ;c is signed  

b = a / 2

If a > b 
  Debug "a is unsigned"
Else 
  Debug "a is signed"
EndIf   

Debug a 
Debug b 

d = c / 2 

If c > d 
  Debug "c is unsigned"
Else 
  Debug "c is signed"
EndIf   

Debug c 
Debug d  

Procedure.l foo(a.l,b.l) 
  
  CompilerIf #PB_Compiler_Backend = #PB_Backend_C 
    !unsigned int v_gfooa =  v_a;  
    !unsigned int v_gfoob =  v_b;
  CompilerElse
    gfooa = a 
    gfoob = b 
  CompilerEndIf 
    
  If gfoob > 0 
    ProcedureReturn gfooa -1
  EndIf 
EndProcedure  

a = 4294967295 
b = a / 2 
e = foo(a,b)
Debug e