7

How can I check whether my app is compiled in 32-bit or 64-bit ?

This is helpful to debug low level code (working with buffers for example).

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
Harry
  • 119
  • 1
  • 6
  • 3
    Why do you need to know? Properly written code should work fine either way. – dpassage Sep 27 '13 at 06:20
  • I agree with @dpassage. Though if you need to know check the device model. https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instp/UIDevice/model – Desdenova Sep 27 '13 at 06:22
  • @dpassage Very naive comment. True for simple things, but definitely not, when you want to optimize your code for different CPU architectures. – Léo Natan Sep 27 '13 at 06:22
  • That's not quite right because, as I understand it, the iPhone 5S will still run 32-bit apps. – dpassage Sep 27 '13 at 06:24
  • @dpassage but if you want to optimize for 64-bit, and have a fat binary, where both 32-bit and 64-bit code can launch, you may need to know specifically what the current architecture is to call the correct optimized code. – Léo Natan Sep 27 '13 at 06:26
  • 1
    There is little need to 'optimize' for 64-bit processors in most cases, especially in a dynamically-typed object-oriented language like Objective-C where inlining is impossible. The only reason you'd ever need a check like this is for serious math (which would be better suited to Accelerate, or NEON), or defining architecture-agnostic types, which are fairly well represented in the STLs of most languages. – CodaFi Sep 27 '13 at 06:34
  • Need more explanation about your issue to help you out.. – Kundan Sep 27 '13 at 06:37
  • @dpassage Not really, On iOS 7.0.x Core face detection does not work if a 32bit app is run on a 64bit device (5s). It is a bug in iOS. – Tibidabo Feb 13 '14 at 12:41

3 Answers3

19

A compile time check would involve #ifdef'ing for __LP64__, which is ARM's data type size standard. A runtime solution would involve checking the size of pointers, like so:

if (sizeof(void*) == 4) {
    // Executing in a 32-bit environment
} else if (sizeof(void*) == 8) {
   // Executing in a 64-bit environment
}

Thankfully, pointer sizes are the one thing that the different standards for compiling 64-bit code seem to agree on.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • 6
    Just FYI, both the `__LP64__` and `sizeof()` solutions are compile-time. Evaluating sizeof is part of the compile process, and not a function. The compiler basically replaces sizeof(type) with the size of that type on the system you're compiling for. Ideally, the #ifdef and if/else will end up compiling identically because the compiler will see that 8 is always == 8 and optimize out the 8 == 4 branch. – ultramiraculous Jan 08 '14 at 19:19
  • Technically, you're correct (but your argument is founded on the assumption of the optimizing compiler). Perhaps [this](http://stackoverflow.com/a/7984249/945847) would fit a more rigorous definition of "runtime check". – CodaFi Jan 09 '14 at 01:04
16
#ifdef __LP64__
    NSLog(@"64-bit\t");
#else
    NSLog(@"32-bit\t");
#endif
GW.Rodriguez
  • 1,181
  • 8
  • 18
2

You could check the size of a pointer. I think on 32bit it is 4bytes and on 64 it should be 8.

if( sizeof(void*) == 4 ) then 32bit else 64bit
rcpfuchs
  • 792
  • 1
  • 12
  • 26