Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



The compiler cannot assume that much, because the argument is a signed integer (negative integers will not overflow and do have well-defined behaviour).


The rabbit hole goes deeper than that: https://gcc.godbolt.org/z/Tc1MTa6nj


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: