Anyone want to guess what gcc generates for a==b?0:a-b ? Any ideas why?
-
Show this thread
-
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.
3 replies 3 retweets 6 likesShow this thread -
I can't get clang to collapse the branch out either.
1 reply 0 retweets 1 likeShow this thread -
A similar case, also needed to deal safely with null pointers, gets optimized correctly: writing a<b as a!=b && a<b.
1 reply 0 retweets 3 likesShow 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.
3 replies 1 retweet 1 likeShow this thread -
Replying to @RichFelker
(ptrdiff_t)((uintptr_t)a - (uintptr_t)b) generates the assembly code that you expect.
1 reply 0 retweets 2 likes -
Replying to @volatile_void @RichFelker
This is only convenient for pointers to char types, and it makes assumptions about the representation of pointers, but I think that musl is making these assumptions elsewhere already.
1 reply 0 retweets 1 like
Yes, but I like to compartmentalize assumptions like this. This isn't documented well now, but there shouldn't be a reason that stdio can't be reused unmodified on a system with different address space model & pointer representation.
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.