It is incredibly useful to talk about a loop's trip count or the values an induction variable can have.
It turns out that taking advantage of signed overflow lets us do that in more places, a boon to all the programs which don't dynamically execute signed overflow.
The tradeoff, however, is that the programing model gets way more complicated. I have talked to the engineer who introduced this optimizing power to our compiler and he sorta regrets it.
IMO, we need better, higher level ways of talking about a loop's iteration space. Today, in C or C++, we are forced to represent the iteration space of a loop using the language's algebra. This is severely limiting when each operation on a signed type might introduce UB. With a higher level mechanism, we can separate the behavior that we want for loops from the behavior we want for everything else in a nice, clean way.
Yes! One decisive advantage that high-level iteration constructs have over for loops is that they're easier to optimize.
One obvious example here is bounds checks: there's no need to have infrastructure to optimize out bounds checks over arrays during loops if you have a construct that eliminates the indexing entirely.
I think this would apply to whatever mechanism is designed to describe a loop's iteration space.
assert( B < B_TYPE_MAX - 4 );
Or use an if statement if it could trigger at runtime.Also the code is clearer on intent.
No, it's pretty clear where the problem is with C, programmers.
Ok let me get this clear, you're saying that the language is at fault because the programmers don't write correct code. I have no words.
When designing tools for use by humans, you need to accept the fact that humans, with all our failings, will be the ones using the tools. So tools should be made resilient against typical ways in which humans fail.
We've been paying for shortcomings in C's design for decades with bugs and security failures that simply don't happen in other languages. That you refuse to see this is baffling to me.
Yes.
A good language makes the correct thing the easy thing.
Arguably the designers of amd64 should have caught this before releasing the ABI or the language design could have been specified so this wasn't an issue in the first place.
I've noticed that memcpy(x, y, 4) on x86 can generate very efficient code (register move), but on ARM it expands to something much more verbose because the addresses might not be aligned.
Could this effectively function as a way of promising to the compiler that the addresses are aligned?
void move4_aligned(void *dst, const void *src) {
assert(((uintptr_t)dst & 0x3) == 0);
assert(((uintptr_t)src & 0x3) == 0);
memcpy(dst, src, 4);
}Trivially.
#ifdef NDEBUG
# define assert_and_assume(cond) if(!(cond))
__builtin_unreachable((cond))
#else
# define assert_and_assume assert
#endifYes, but it would have the potentially surprising behavior that compiling your release version with -DNDEBUG might slow it down. We ran into this a couple years ago: https://plus.google.com/+DanielLemirePhD/posts/BTSams19Ero
Recent compilers support some extensions that can do it better. GCC and CLang use __builtin_assume_aligned(), and ICC uses __assume_aligned() (haven't tested the synonym).
Special case checking for this case is a pain for various kinds of loop unrolling / peeling, and also removing empty loops if they don't contain any code (which often comes up in practice with C++ templates).