4 ms·
My guess: since overflowing int is UB, and the only value of n that stops the recursion is zero, the compiler assumes that n must be zero and checks accordingly
by barsonme 5y ago
My guess: since overflowing int is UB, and the only value of n that stops the recursion is zero, the compiler assumes that n must be zero and checks accordingly.
That doesn’t explain why it uses test dil, 1 instead of test dil, dil or cmp 0 or whatever.
- eMSF 5y agoThe compiler cannot assume that much, because the argument is a signed integer (negative integers will not overflow and do have well-defined behaviour).
- davemp 5y agoThe rabbit hole goes deeper than that: https://gcc.godbolt.org/z/Tc1MTa6nj https://gcc.godbolt.org/z/Tc1MTa6nj
- rocqua 5y agoThat is a well defined function. And indeed implements isEven. Because unsigned int has defined overflow semantics. Essentially, it will eventually overflow and hit the correct base-case for 0.