1

Is it possible in iOS to detect the current processor of the device. The project I am working on requires, programmatically, to check if the processor is ArmV7 or ArmV7s.

Fred West
  • 25
  • 4

4 Answers4

3

I don't know for certain that this will provide the information you'd like but I would try sysctl:

int32_t value = 0;
size_t length = sizeof(value);
sysctlbyname("hw.cpusubtype", &value, &length, NULL, 0);

The values for subtype are in mach/machine.h.

/*
 *  ARM subtypes
 */
#define CPU_SUBTYPE_ARM_ALL             ((cpu_subtype_t) 0)
#define CPU_SUBTYPE_ARM_V4T             ((cpu_subtype_t) 5)
#define CPU_SUBTYPE_ARM_V6              ((cpu_subtype_t) 6)
#define CPU_SUBTYPE_ARM_V5TEJ           ((cpu_subtype_t) 7)
#define CPU_SUBTYPE_ARM_XSCALE      ((cpu_subtype_t) 8)
#define CPU_SUBTYPE_ARM_V7      ((cpu_subtype_t) 9)
#define CPU_SUBTYPE_ARM_V7F     ((cpu_subtype_t) 10) /* Cortex A9 */
#define CPU_SUBTYPE_ARM_V7K     ((cpu_subtype_t) 12) /* Kirkwood40 */
sbooth
  • 16,646
  • 2
  • 55
  • 81
  • check mach/machine.h to get cpu subtypes values. – Emmanuel May 17 '13 at 15:56
  • Fun question 1: what does your code do when `sysctlbyname("hw.cpusubtype", ...)` gives you an unexpected value? Fun question 2: what do you think happens when your app runs on a device released after your app? Conclude. – Pierre Lebeaupin Jun 02 '15 at 10:16
  • There is no issue except that `value` will contain a number corresponding to an unknown processor subtype. – sbooth Jun 02 '15 at 12:01
  • No issue?!! Here is your issue: given an unknown value, either you decide this means the processor supports ARMv7s, and you risk a crash if Apple introduces a new processor that does not necessarily support all the features of their latest processor, or you decide this means the processor does not support ARMv7s, in which case you risk having worse performance on a newly introduced device that does support it. And before you say you'll react fast in either case, imagine an European developer following your advice, and having an issue on a new device that is US-only the first few months… – Pierre Lebeaupin Jun 03 '15 at 19:18
  • 1
    My response answers the OP's question, namely how to identify the device's current processor. Whether this is good practice in certain cases is a discussion not suitable for comments. – sbooth Jun 05 '15 at 02:16
1

I'm not aware of any API to check this, but you could perhaps write one yourself by providing v7 and v7s assembler implementations for the same symbol that simply return true or false as required.

Assuming that the v7s implementation will be used if and only if the processor supports v7s, it should work.

Chris Devereux
  • 5,453
  • 1
  • 26
  • 32
0

This way:

sysctlbyname("hw.cpusubtype", &value, &length, NULL, 0);

is good, but there are more easy way:

#include <mach-o/ldsyms.h>

int processorType = _mh_execute_header.cputype;
int processorSubtype = _mh_execute_header.cpusubtype;

Although, it shows you which cpu used in compile time for current code..

Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
-2

You don't.

That is, it is not advisable to programmatically detect ARM architecture version support at runtime on iOS. This is the answer I got from an Apple engineer when I specifically asked this question, and given the pitfalls (e.g. what do you do when you get an unexpected value?), I can believe them.

(yes, sometimes the correct answer to an "How do I?" question is "You don't.")

Pierre Lebeaupin
  • 1,103
  • 8
  • 20