compiler-optimization

Automatic code deduplication of assembly language?

夙愿已清 提交于 2020-01-22 14:39:50
问题 I've been going through some Assembly Programming Videos to get a better understanding of how to manually optimize the *.s files left after compiling with gcc/g++ -S ... One of the topics covered was Refactoring Redundant Code that demonstrates how to move redundant code to its own labeled block ending with a ret and replacing it with a call. The example given in the video is 2 blocks containing: mov eax,power mul ebx mov power,eax inc count which it replaces with call CalculateNextPower and

embed string via header that cannot be optimized away

瘦欲@ 提交于 2020-01-22 13:49:08
问题 While developing a header-only library, I'd like to make sure that a given string is embedded in all binaries that use my header, even if the compiler is configured to optimize away unused constants, and the binary gets stripped. The embedding shouldn't have any side-effects (apart from making the resulting binary a little bit bigger). I don't know how people are going to use the headers, but the headers might get included in multiple compilation units, all linked together into a single

embed string via header that cannot be optimized away

扶醉桌前 提交于 2020-01-22 13:49:01
问题 While developing a header-only library, I'd like to make sure that a given string is embedded in all binaries that use my header, even if the compiler is configured to optimize away unused constants, and the binary gets stripped. The embedding shouldn't have any side-effects (apart from making the resulting binary a little bit bigger). I don't know how people are going to use the headers, but the headers might get included in multiple compilation units, all linked together into a single

Can Compiler Optimize Loop with Variable Length?

家住魔仙堡 提交于 2020-01-15 05:07:14
问题 Can the compiler optimize loops if the last index of the loops ( a and b in the following example) are not known at compile time? Unoptimized: int* arr = new int[a*b]; for (i = 0; i < a; ++i){ for(j = 0; j < b; ++j){ arr[i*b+j] *= 8; } } //delete arr after done. More Optimized: (assuming a and b are large...) int c = a*b; int* arr = new int[c]; for (i = 0; i < c; ++i){ arr[c] *= 8; } //delete arr after done. 回答1: If you treat the array as linear space, gcc (and presumably others) will

C++ Can constant class data be optimized out of class by compiler?

回眸只為那壹抹淺笑 提交于 2020-01-14 14:23:13
问题 I know that constant variables outside classes can be optimized directly into function calls by the compiler, but is it legal for the compiler to do the same for constant class variables? If there is a class declared like this: class A { public: const int constVar; //other, modifiable variables A(int val): constVar(val) { //code to initialize modifiable variables } }; and I create an instance of A and call a function like this: A obj(-2); int absoluteVal = std::abs(A.constVar); is the

Why ret disappear with optimization?

亡梦爱人 提交于 2020-01-14 10:36:08
问题 int suma(int* array, int len) { asm(" xor %eax, %eax # resultado = 0 \n" " xor %edx, %edx # i = 0 \n" "1: add (%rdi,%rdx,4), %eax # res += array[i] \n" " inc %edx # ++i \n" " cmp %edx,%esi # i < len? \n" " jne 1b # repetir \n" // " ret \n" ); } int main() { int v[100]; return suma(v, 100); } Why is it that gcc inserts ret at the end of suma() on -O0 , but I have to add it myself on -O3 ? From gcc -v : gcc version 8.2.1 20181011 (Red Hat 8.2.1-4) (GCC) 回答1: I assume 64bit..., array in rdi, len

Is this a missed optimization opportunity or not

邮差的信 提交于 2020-01-13 13:45:56
问题 I posted this answer. Code: #include <atomic> #include <utility> void printImpl(...); std::atomic<bool> printLog = false; class Log { public: template <typename T> const auto& operator<<(T&& t) { if (printLog) { ulog.active = true; return ulog << std::forward<T>(t); } else { ulog.active = false; return ulog; } } private: struct unchecked_log { template <typename T> const auto& operator<<(T&& t) const { if (active) { printImpl(std::forward<T>(t)); } return *this; } bool active{false}; };

.Max() vs OrderByDescending().First()

半腔热情 提交于 2020-01-12 14:03:27
问题 This is purely for my own knowledge, if I were going to write the code I would just use .Max() . At first thought .Max() only has to do a single pass through numbers to find the max, while the second way has to sort the entire thing enumerable then find the first one. So it's O(n) vs O(n lg n) . But then I was thinking maybe it knows it only needs the highest and just grabs it. Question: Is LINQ and/or the compiler smart enough to figure out that it doesn't need to sort the entire enumerable

Are C# anonymous types redundant in C# 7

a 夏天 提交于 2020-01-12 13:59:27
问题 Since C# 7 introduces value tuples, is there a meaningful scenario where they are better suited than tuples? For example, the following line collection.Select((x, i) => (x, i)).Where(y => arr[y.i].f(y.x)).ToArray(); makes the following line collection.Select((x, i) => new {x, i}).Where(y => arr[y.i].f(y.x)).ToArray(); redundant. What would be the use case where one is better used over the other (for either performance reasons or optimization)? Obviously, if there is a need for more than six

Do compilers automatically optimise repeated calls to mathematical functions?

…衆ロ難τιáo~ 提交于 2020-01-12 12:08:26
问题 Say I had this snippet of code: #include <cmath> // ... float f = rand(); std::cout << sin(f) << " " << sin(f); As sin(f) is a well defined function there is an easy optimisation: float f = rand(); float sin_f = sin(f); std::cout << sin_f << " " << sin_f; Is this an optimisation that it's reasonable to expect a modern C++ compiler to do by itself? Or is there no way for the compiler to determine that sin(f) should always return the same value for an equal value of f ? 回答1: Using g++ built