how to determine if compiler is 32 or 64 bit at compile time

Just starting out? Need help? Post your questions and find answers here.
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

how to determine if compiler is 32 or 64 bit at compile time

Post by jack »

suppose you need to pad a structure if compiling in 64 bit but not if compiling in 32 bit,
how do you determine if compiler is 32 or 64 bit at compile time?
for example in libtommath ( http://libtom.org/ ) you have the mp_int structure

Code: Select all

Structure mp_int
	used.l 
	alloc.l
	sign.l
	*dp
EndStructure
the size of the structure in 64-bit is 24 and 16 in 32-bit so I need to pad an extra 4 bytes if compiling in 64-bit, something like this

Code: Select all

Structure mp_int
	used.l
	alloc.l
	sign.l
	*dp
	pad.l
EndStructure
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: how to determine if compiler is 32 or 64 bit at compile

Post by kenmo »

I use something like this:

Code: Select all

CompilerIf (SizeOf(INTEGER) = 8)
  #PB_Compiler_64bit = #True
CompilerElse
  #PB_Compiler_64bit = #False
CompilerEndIf

Debug #PB_Compiler_64bit
Because of course PB integers are 8 bytes in 64-bit mode and 4 bytes in 32-bit mode (same as pointers).

Which makes me wonder: how is that structure 24 bytes in 64-bit mode? Wouldn't it be 3x4 (the Longs) + 8 (the 64-bit pointer)?
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: how to determine if compiler is 32 or 64 bit at compile

Post by jack »

thank you for your help, as for the structure being 24 bytes instead of 20 my guess is that the compiler (llvm-g++) pads the structure,
my 64-bit test was done on Mac OS x with latest OS and command line tools, need to find out what the case may be on Windows x64.
Little John
Addict
Addict
Posts: 4780
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: how to determine if compiler is 32 or 64 bit at compile

Post by Little John »

jack wrote:how do you determine if compiler is 32 or 64 bit at compile time?

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: how to determine if compiler is 32 or 64 bit at compile

Post by Josh »

Are you sure, that you have padded the structure correct?
jack wrote:

Code: Select all

Structure mp_int
	used.l
	alloc.l
	sign.l
	*dp
	pad.l
EndStructure
If it is a C-Structure, the correct padding for 64 bit would be:

Code: Select all

Structure mp_int
	used.l
	alloc.l
	sign.l
	pad.l
	*dp
EndStructure
Better use Align and you have no trouble with correct padding and no problems with 32 or 64 compilation:

Code: Select all

Structure mp_int Align #PB_Structure_AlignC
   used.l 
   alloc.l
   sign.l
   *dp
EndStructure
P.S.: Be careful. In some cases Align gives wrong results (Pb 5.11), but no problem in this structure. I think rest will be corrected with the next Pb version
sorry for my bad english
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: how to determine if compiler is 32 or 64 bit at compile

Post by jack »

thanks Josh, good tips.
@Little John, I had tried your suggestion but without #PB_Compiler_Processor and it failed but your code works, thanks.
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: how to determine if compiler is 32 or 64 bit at compile

Post by kenmo »

Little John wrote:
jack wrote:how do you determine if compiler is 32 or 64 bit at compile time?

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
I didn't know about that compiler constant :P
Little John
Addict
Addict
Posts: 4780
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: how to determine if compiler is 32 or 64 bit at compile

Post by Little John »

kenmo wrote:I didn't know about that compiler constant :P
See at the end of this page. :-)
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: how to determine if compiler is 32 or 64 bit at compile

Post by jack »

josh, I just tested my program with Align #PB_Structure_AlignC and it works, thank you.
Post Reply