UPCOMING VECTORIZATION WORKSHOPSAVX Vectorization Workshop: 4 half days, May 26th to May 29th, 11 AM – 3 PM (US East Coast) 8 AM – 12 PM (US West Coast) 5 PM – 9 PM CET (Europe)NEON Vectorization Workshop: TBD, send e-mail to info@johnnysswlab.com to express interestMore info…Early in their career every C/C++ developer has had an eureka moment: the discovery of optimization options in their compiler. A discovery that there is GCC compiler offers -O0 optimization lever for regular debugging and -O3 for fast release code will be one of the moment I will never forget. I just needed to compile my program with -O3 and everything would run faster without any effort on my side.Many years have passed since and I believe everyone compiles their code in the similar way: we compile our .c or .cpp files using a compiler, with a certain optimization option (-O0 or O3) to get object files (.o). Then linker takes all our object files and merges them into one big executable or a library.In this story, linker is the last piece of the puzzle, and compared to what the compiler is doing, its job is much simpler. But doing things this way we are missing out on some important optimization opportunities. So, ladies and gentlemen, allow me to explain.MotivationAs I said earlier, compiler makes many optimizations on the code. Allow me to present two of them: inlining and loop merging. Have a look at the following code:int find_min(const int* array, const int len) { int min = a[0]; for (int i = 1; i < len; i++) { if (a[i] < min) { min = a[i]; } } return min; } int find_max(const int* array, const int len) { int max = a[0]; for (int i = 1; i < len; i++) { if (a[i] > max) { max = a[i]; } } return max; } void main() { int* array, len, min, max; initialize_array(array, &len); min = find_min(array, len); max = find_max(array, len); ... }This is a very simple code that initializes an array and then finds its minimum and maximum. Please pay attention to functions find_min and find_max and notice th...
First seen: 2025-05-21 16:21
Last seen: 2025-05-21 18:21