The expression a==b?0:a-b is not stupid, btw. It's essentially the only correct way to write a-b when a and b are pointers and both being null is a possibility you want to allow for.
-
-
Show this thread
-
-
A similar case, also needed to deal safely with null pointers, gets optimized correctly: writing a<b as a!=b && a<b.
Show this thread -
Even more surprisingly, gcc fails to collapse the branch out of this: ptrdiff_t diff(char *a, char *b) { return a!=b ? a-b : (intptr_t)a-(intptr_t)b; } clang gets it right, though.
Show this thread -
Oh, fun. gcc <= 7 gets it right too. Only gcc 8 fails to collapse it. I wonder if this is new logic to prevent wrongly applying "pointer provenance" stuff to pointers cast to integers.
Show this thread
End of conversation
New conversation -
-
-
-
It is when you expect https://gcc.godbolt.org/z/M2JzTF and it's in an extreme hot path.
-
I may regret asking, but what kind of code has this in a hot path?
-
For the related comparison issue, getc, scanf, etc. The subtraction is probably less hot.
End of conversation
New conversation -
-
-
The overall thing is bad enough, but with that mov edx,0 I feel like GCC is just trolling us.
Thanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.