19

I would like to write code depending on whether the target architecture is e.g. armv7, armv7s, or arm64.

The reason that I can't use sysctlbyname is that this would give me the underlying architecture at runtime, but when arm64 e.g. simulates armv7, sysctl (seemingly) still reports arm64.

Community
  • 1
  • 1
oleks
  • 788
  • 2
  • 8
  • 19
  • Why's that a problem? 64-bit code knows it's running on v8 in AArch64 state by definition. If 32-bit code sees "arm64" or any other (inaccurate) v8 synonym then it can deduce it's running on v8 in AArch32 state. If you're not doing dynamic dispatch at runtime then your target architecture is whatever you choose to compile for. – Notlikethat May 29 '14 at 21:52
  • 1
    I would like to report the target architecture. – oleks May 30 '14 at 09:10
  • For Microsoft Visual Studio see https://stackoverflow.com/questions/37244202/detect-arm-64-in-preprocessor whose answer mentions the `_M_ARM64` defined constant beginning with VS2017. – Richard Chambers Aug 14 '22 at 18:22

3 Answers3

32

Although this is not a 100% answer to the question, but may be useful:

When using clang, you can discern between 32 bit arm and 64 bit arm using:

__arm__ which is defined for 32bit arm, and 32bit arm only.

__aarch64__ which is defined for 64bit arm, and 64bit arm only.

Bram
  • 7,440
  • 3
  • 52
  • 94
13

clang --target=... -mcpu=... -E - -dM </dev/null will output all the pre-defined preprocessor macros (similar works for gcc, too)

I don't see single macro that provides the answer, but you can probably use some combination of __ARM_ARCH and defined(__ARM_ARCH_*).

scott
  • 728
  • 4
  • 8
  • 2
    `__ARM_ARCH` picks up both 32-bit and 64-bit ARM machines. I believe Bram's answer is closer to the correct answer. – jww Jun 05 '19 at 00:28
10

__ARM_ARCH_ISA_A64 is predefined if it's target is arm64,

__ARM_ARCH_7S__ for armv7s,

__ARM_ARCH_7A__ for armv7.

Use: clang -arch arm64 -E -dM - < /dev/null which can output preprocess macro.

kenorb
  • 155,785
  • 88
  • 678
  • 743
fengxing
  • 374
  • 1
  • 4
  • 10
  • It's the question or answer? Please elaborate. – kenorb Mar 02 '15 at 10:55
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – kenorb Mar 02 '15 at 10:55
  • `__ARM_ARCH_ISA_A64` is predefined if it's target is arm64. `__ARM_ARCH_7S__` for armv7s, `__ARM_ARCH_7A__` for armv7. Use `clang -arch arm64 -E -dM - < /dev/null` can output Preprocess marco, I found it in this. – fengxing Mar 03 '15 at 11:25
  • @fengxing You found it in "this". Where was the source? – auspicious99 Feb 22 '21 at 16:09