17

Is there a way to identify the iOS device CPU architecture in runtime?

Thank you.

George Taskos
  • 7,808
  • 18
  • 76
  • 140
  • Try to set defines for device models: http://stackoverflow.com/questions/11197509/ios-iphone-get-device-model-and-make – TUNER88 Nov 08 '13 at 12:47
  • Thank you but I think I wasn't clear, or maybe I mislead the question, that is fine as far as it detects the model, how can I get if it's ARMv6, ARM, ARMv7, i386? – George Taskos Nov 08 '13 at 12:53
  • 1
    Hmmm I think I get what you mean, I should get the model and set defines due to the model to find the architecture. – George Taskos Nov 08 '13 at 12:55

5 Answers5

26

You can use sysctlbyname :

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

NSString *getCPUType(void)
{
    NSMutableString *cpu = [[NSMutableString alloc] init];
    size_t size;
    cpu_type_t type;
    cpu_subtype_t subtype;
    size = sizeof(type);
    sysctlbyname("hw.cputype", &type, &size, NULL, 0);

    size = sizeof(subtype);
    sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);

    // values for cputype and cpusubtype defined in mach/machine.h
    if (type == CPU_TYPE_X86)
    {
            [cpu appendString:@"x86 "];
             // check for subtype ...

    } else if (type == CPU_TYPE_ARM)
    {
            [cpu appendString:@"ARM"];
            switch(subtype)
            {
                    case CPU_SUBTYPE_ARM_V7:
                    [cpu appendString:@"V7"];
                    break;
                    // ...
            }
    }
    return [cpu autorelease];
}
Emmanuel
  • 2,867
  • 1
  • 13
  • 15
  • 3
    For iPhone 5s device or later, most devices support 64bit and we could add CPU_TYPE_ARM64 as a new case for type & subtype. – Itachi Jan 28 '15 at 04:33
  • @Emmanuel thank you for the answer. But I want to know if we can get the name of the processor like "Intel i5" – Vikas Bansal Jul 28 '15 at 06:57
  • 1
    @VikasBansal Hi, using sysctlbyname with "machdep.cpu.brand_string" should give you what you want. [Here a complete answer](http://stackoverflow.com/questions/5650659/obtaining-cpu-descriptions-on-mac-os-x) – Emmanuel Jul 29 '15 at 12:30
12

I think this is the better way,

#import <mach-o/arch.h>

NXArchInfo *info = NXGetLocalArchInfo();
NSString *typeOfCpu = [NSString stringWithUTF8String:info->description];
//typeOfCpu = "arm64 v8"
Mahmut Şahin
  • 523
  • 5
  • 12
6

Just adding more to @Emmanuel's answer:

- (NSString *)getCPUType {
      NSMutableString *cpu = [[NSMutableString alloc] init];
      size_t size;
      cpu_type_t type;
      cpu_subtype_t subtype;
      size = sizeof(type);
      sysctlbyname("hw.cputype", &type, &size, NULL, 0);

      size = sizeof(subtype);
      sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);

      // values for cputype and cpusubtype defined in mach/machine.h
      if (type == CPU_TYPE_X86_64) {
          [cpu appendString:@"x86_64"];
      } else if (type == CPU_TYPE_X86) {
          [cpu appendString:@"x86"];
      } else if (type == CPU_TYPE_ARM) {
          [cpu appendString:@"ARM"];
          switch(subtype)
          {
              case CPU_SUBTYPE_ARM_V6:
                  [cpu appendString:@"V6"];
                  break;
              case CPU_SUBTYPE_ARM_V7:
                  [cpu appendString:@"V7"];
                  break;
              case CPU_SUBTYPE_ARM_V8:
                  [cpu appendString:@"V8"];
                  break;
          }
      }
      return cpu; 
  }
Mukund Agarwal
  • 557
  • 5
  • 14
  • Thanks! You're answer is missing CPU_TYPE_ARM64 type checking which can be followed by subtype checking for CPU_SUBTYPE_ARM64_V8 (currently it will always be V8, but might change in future) – brkeyal Jun 03 '18 at 14:12
5

Here is the swift version of @Mahmut's answer.

import MachO

private func getArchitecture() -> NSString {
    let info = NXGetLocalArchInfo()
    return NSString(utf8String: (info?.pointee.description)!)!
}

print(getArchitecture() ?? "No architecture found")

Results

  • iPhone 12: ARM64E What is ARM64E?
  • Mac Mini M1
    • iOS 13 emulator: Intel 80486
    • iOS 14 emulator: ARM64E
  • MacBook Pro 16" x86_64 Intel:
    • iOS 13 emulator: `` (I don't have the emulators downloaded here)
    • iOS 14 emulator: Intel x86-64h Haswell

Feel free to update this.

Ben Butterworth
  • 13,650
  • 4
  • 61
  • 95
2

I feel that this is the best answer for Swift yet:

import MachO

func getArch() -> String? {
    guard let archRaw = NXGetLocalArchInfo().pointee.name else {
        return nil
    }
    return String(cString: archRaw)
}
print("Current device architecture: \(getArch() ?? "Unknown Archetiture")")

For A11 (iPhone X, 8, 8+) and lower devices, the function above will return arm64, However for A12 (iPhone Xs, Xs Max, XR) and newer devices, it will return arm64e

Serena
  • 53
  • 1
  • 6