26

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.

3
  • 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. Commented May 29, 2014 at 21:52
  • 1
    I would like to report the target architecture. Commented May 30, 2014 at 9:10
  • For Microsoft Visual Studio see stackoverflow.com/questions/37244202/… whose answer mentions the _M_ARM64 defined constant beginning with VS2017. Commented Aug 14, 2022 at 18:22

3 Answers 3

42

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.

Sign up to request clarification or add additional context in comments.

Comments

17

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_*).

1 Comment

__ARM_ARCH picks up both 32-bit and 64-bit ARM machines. I believe Bram's answer is closer to the correct answer.
11

__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.

3 Comments

It's the question or answer? Please elaborate.
__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 You found it in "this". Where was the source?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.