Tag: compiler-optimization
-
GCC removes a bounds check in the right operand of &&, but not in the left operand, why?
18 I have the following C/C++ code snippet: #define ARRAY_LENGTH 666 int g_sum = 0; extern int *g_ptrArray[ ARRAY_LENGTH ]; void test() { unsigned int idx = 0; // either enable or disable the check "idx < ARRAY_LENGTH" in the while loop while( g_ptrArray[ idx ] != nullptr /* && idx < ARRAY_LENGTH */ )…
-
Why is optimization forbidden if a C compiler cannot prove lack of UB?
10 If a C program has undefined behavior, anything can happen. Therefore compilers may assume that any given program does not contain UB. So, suppose our program contains the following: x += 5; /* Do something else without x in the meantime. */ x += 7; Of course, this can be optimized to /* Do…
-
Why gcc is so much worse at std::vector vectorization of a conditional multiply than clang?
25 Consider following float loop, compiled using -O3 -mavx2 -mfma for (auto i = 0; i < a.size(); ++i) { a[i] = (b[i] > c[i]) ? (b[i] * c[i]) : 0; } Clang done perfect job at vectorizing it. It uses 256-bit ymm registers and understands the difference between vblendps/vandps for the best performance possible.…
-
Why is initializing a string to “” more efficient than the default constructor?
11 Generally, the default constructor should be the fastest way of making an empty container. That’s why I was surprised to see that it’s worse than initializing to an empty string literal: #include <string> std::string make_default() { return {}; } std::string make_empty() { return ""; } This compiles to: (clang 16, libc++) make_default(): mov rax,…