Page 1 of 1
how to determine if compiler is 32 or 64 bit at compile time
Posted: Mon Apr 22, 2013 1:01 am
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
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 2:01 am
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)?
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 2:11 am
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.
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 5:41 am
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
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 6:48 am
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
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 7:19 pm
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.
Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 10:12 pm
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

Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 10:15 pm
by Little John
kenmo wrote:I didn't know about that compiler constant

See at the end of
this page.

Re: how to determine if compiler is 32 or 64 bit at compile
Posted: Mon Apr 22, 2013 10:19 pm
by jack
josh, I just tested my program with Align #PB_Structure_AlignC and it works, thank you.