by dsfuoi · 10y ago · 0 comments
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.
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).