32

Does anyone know of an easy way to tell if an iOS7 device has 32- or 64-bit hardware? I don't mean programmatically, I just mean via settings, model number, 3rd-party app, etc.

I'm having a problem that I suspect is 64-bit related. Apple's advice is to test on the 64-bit simulator but also on an actual 64-bit device, but then doesn't say anything about how to determine that. I can write a test app to check sizeof(int) or whatever, but there's got to be some way for, say, tech support to know what they're working with.

Eric

Eric McNeill
  • 1,784
  • 1
  • 18
  • 29
  • 2
    You could know by the model. iPhone 5s, iPad mini Retina and iPad Air are 64-bit. Other ones are 32-bit. – Marcelo Nov 20 '13 at 18:57
  • Using the model is the conclusion I've come to as well. I guess with the model number that will be sufficient when two years from now I'm trying to figure out exactly what a customer is running ("let's see, was the 5C 32-bit...?") – Eric McNeill Nov 20 '13 at 19:25
  • In runtime you can detect device arch by next solution https://stackoverflow.com/questions/20104403/determine-if-ios-device-is-32-or-64-bit/47636895#47636895 – Andrei Pachtarou Dec 04 '17 at 15:44

7 Answers7

39

There is no other "official" way to determine it. You can determine it using this code:

if (sizeof(void*) == 4) {
    NSLog(@"32-bit App");
} else if (sizeof(void*) == 8) {
    NSLog(@"64-bit App");
}
Arun Kumar
  • 788
  • 5
  • 15
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • 9
    This checks if the *application* is running in 32- or 64-bit mode, not if the *hardware* is 32- or 64-bit. – Martin R Nov 20 '13 at 19:10
  • 5
    According to Apple both 32-bit and 64-bit binaries are included in the .ipa file when you compile for 64-bit too. So if the app is running in 64-bit mode then the hardware is 64-bit. – Nikos M. Nov 20 '13 at 19:18
  • @NikosM., do you have a source? – John Riselvato Nov 20 '13 at 19:21
  • 2
    @NikosM.: Yes, but a 32-bit app will run on 64-bit hardware as well. (Did not downvote btw.) – Martin R Nov 20 '13 at 19:22
  • It is the first sentence in the official apple documentations: https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaTouch64BitGuide/ConvertingYourAppto64-Bit/ConvertingYourAppto64-Bit.html#//apple_ref/doc/uid/TP40013501-CH3-SW1 "At a high level, here are the steps to create an app that targets both the 32-bit and the 64-bit runtime environments:" – Nikos M. Nov 20 '13 at 19:24
  • @MartinR you are right, but I am guessing that if you want to check if the device is 64-bit, you are probably building for 64-bit architecture too. – Nikos M. Nov 20 '13 at 19:27
  • Yeah, I think this is a reliable approach for a programmatic solution. When I run it in a test app compiled for both I get 64-bit on an iPad Air and 32-bit on an older Mini. – Eric McNeill Nov 20 '13 at 19:27
  • 2
    If the app is compiled as 32/64-bit "universal" binary then both answers can be used, because the 64-bit slice will run on 64-bit hardware, and the 32-bit slice will run on 32-bit hardware. I just wanted to point out that `sizeof(void *)` or `#ifdef __LP64__` is decided by the compiler, not by the hardware. – Martin R Nov 20 '13 at 19:29
  • @MartinR totally right, but the only other way of doing it is to check the device model, but you have to update your code every time Apple releases a new device. – Nikos M. Nov 20 '13 at 19:31
  • Am accepting this as the answer keeping in mind the caveats mentioned in the comments. Its not a perfect solution. – Eric McNeill Jan 29 '14 at 19:48
10

Below is the method is64bitHardware. It returns YES if the hardware is a 64-bit hardware and works on a real iOS device and in an iOS Simulator. Here is source.

#include <mach/mach.h>

+ (BOOL) is64bitHardware
{
#if __LP64__
    // The app has been compiled for 64-bit intel and runs as 64-bit intel
    return YES;
#endif

    // Use some static variables to avoid performing the tasks several times.
    static BOOL sHardwareChecked = NO;
    static BOOL sIs64bitHardware = NO;

    if(!sHardwareChecked)
    {
        sHardwareChecked = YES;

#if TARGET_IPHONE_SIMULATOR
        // The app was compiled as 32-bit for the iOS Simulator.
        // We check if the Simulator is a 32-bit or 64-bit simulator using the function is64bitSimulator()
        // See http://blog.timac.org/?p=886
        sIs64bitHardware = is64bitSimulator();
#else
        // The app runs on a real iOS device: ask the kernel for the host info.
        struct host_basic_info host_basic_info;
        unsigned int count;
        kern_return_t returnValue = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count);
        if(returnValue != KERN_SUCCESS)
        {
            sIs64bitHardware = NO;
        }

        sIs64bitHardware = (host_basic_info.cpu_type == CPU_TYPE_ARM64);

#endif // TARGET_IPHONE_SIMULATOR
    }

    return sIs64bitHardware;
}
Dragos
  • 1,050
  • 16
  • 21
9

Totally untested, but you should be able to get the CPU via sysctl like this:

#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/machine.h>

void foo() {
    size_t size;
    cpu_type_t type;

    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);

    if (type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}

In the iOS 7 SDK, CPU_TYPE_ARM64 is defined in <mach/machine.h> as:

#define CPU_TYPE_ARM64          (CPU_TYPE_ARM | CPU_ARCH_ABI64)

A different way seems to be:

#include <mach/mach_host.h>

void foo() {
    host_basic_info_data_t hostInfo;
    mach_msg_type_number_t infoCount;

    infoCount = HOST_BASIC_INFO_COUNT;
    host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, &infoCount);

    if (hostInfo.cpu_type == CPU_TYPE_ARM64) {
        // ARM 64-bit CPU
    } else if (hostInfo.cpu_type == CPU_TYPE_ARM) {
        // ARM 32-bit CPU
    } else {
        // Something else.
    }
}
DarkDust
  • 90,870
  • 19
  • 190
  • 224
6

If you are compiling with clang, there is another way: just check if __arm__ or __arm64__ is defined.

The example code below is not tested but it should illustrate what I mean by that:

#if defined(__arm__)
    NSLog(@"32-bit App");
#elif defined(__arm64__)
    NSLog(@"64-bit App");
#else
    NSLog(@"Not running ARM");
#endif

Note that this relies on the fact that current iOS application binaries contain both, 32bit and 64bit binaries in a single container and they will be correctly selected depending on whether your app supports executing 64bit.

aignas
  • 1,044
  • 12
  • 19
5

You can use bitWidth on Int https://developer.apple.com/documentation/swift/int/2885648-bitwidth

static var is32Bit: Bool {
    return Int.bitWidth == 32
}

static var is64Bit: Bool {
    return Int.bitWidth == 64
}
jherg
  • 1,696
  • 2
  • 13
  • 15
3

In runtime you can use something like this

extension UIDevice {
    static let is64Bit = MemoryLayout<Int>.size == MemoryLayout<Int64>.size
}
3

I use this in swift 4, not sure if it's the best solution but it works.

   func getCPUArch()
   {
      #if arch(arm)
         print("this is a 32bit system")
      #elseif arch(arm64)
          print("this is a 64bit system")
      #endif
   }
Sam Trent
  • 395
  • 3
  • 15