0

When I perform integer division by zero on Windows 11 on Arm64, it raises a SEH exception.

That is rather surprising, considering Arm by default does not trap zero division.

My question is: does the observed behavior always happen on Windows 10 / Windows 11 for Arm64?

9
  • 1
    relevant: x.com/brucedang/status/1021448368352923648 Commented Oct 30 at 14:56
  • 2
    so specify what language and compiler you are using Commented Oct 30 at 14:56
  • 1
    @BenVoigt it appears to be correct: godbolt.org/z/TWf7zEfxc Commented Oct 30 at 15:12
  • 1
    That's a convincing example, looks like you are ready to write your own answer. Commented Oct 30 at 15:15
  • 1
    AFAIK, ARM has no way at all to have the hardware trap on integer divide by zero, so the only way to get it is with an explicit software test. Commented Oct 31 at 14:51

1 Answer 1

1

Apart from the zero division, the division is also undefined for INT_MIN / -1, which is signed overflow: the positive result does not fit int.

On x86 and x86-64, both division errors are trapped by the CPU. The interrupt is then translated by the OS to exception STATUS_INTEGER_DIVIDE_BY_ZERO (0xC0000094) or STATUS_INTEGER_OVERFLOW (0xC0000095) correspondingly.

On Arm64, the zero trap is added by Visual C++. It is zero check and brk instruction if it is zero. It does not add any trap for signed integer division overflow though.

See https://godbolt.org/z/a1hPbhc5G.

As the trap on Arm64 is inserted by the compiler, and is not a CPU feature, it does not depend on OS or specific CPU. It is always present in the compiled program.

Only MSVC implements this behavior on Arm64, clang-clon Arm64 does not implement this, and does not trap.

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

6 Comments

It is trying to trap divide by zero.
Please file a bug at aka.ms/vsfeedback pointing out this difference. Note that `/arch:armv8.1" doesn't change the codegen.
I really have no idea what is the bug here. The compilers behavior may diverge here, it is undefined in C++
It would be interesting to know if MSVC has a switch to disable the check. And conversely, if clang has a switch to enable it. (I would have at least thought -ftrapv would add a check, but it does not; indeed, if you read the GCC docs carefully, it says -ftrapv only traps on signed overflow in addition, subtraction and multiplication).
@ChuckWalbourn: I am also not sure why /arch:armv8.1 should be significant. The behavior of divide instructions didn't change between ARM architecture versions, AFAIK.
@ChuckWalbourn: Are you saying that Microsoft intended that MSVC and clang-cl should behave consistently to each other in this respect, despite it being UB from the language standpoint? (Given your profile, I guess you would know...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.