compiler-optimization

Why is memcmp(a, b, 4) only sometimes optimized to a uint32 comparison?

£可爱£侵袭症+ 提交于 2019-12-31 10:56:58
问题 Given this code: #include <string.h> int equal4(const char* a, const char* b) { return memcmp(a, b, 4) == 0; } int less4(const char* a, const char* b) { return memcmp(a, b, 4) < 0; } GCC 7 on x86_64 introduced an optimization for the first case (Clang has done it for a long time): mov eax, DWORD PTR [rsi] cmp DWORD PTR [rdi], eax sete al movzx eax, al But the second case still calls memcmp() : sub rsp, 8 mov edx, 4 call memcmp add rsp, 8 shr eax, 31 Could a similar optimization be applied to

Can I tell javac to ignore the lack of `import foo.Bar`?

与世无争的帅哥 提交于 2019-12-31 00:43:51
问题 I'm using reflection to load MyClass.class (an external file) at runtime. MyClass.class uses the library Bar , which would mean that I need to place import foo.Bar; at the top of the file. However, the Bar library is already loaded in the main class loading MyClass . Is there a way for me to tell javac to ignore that Bar doesn't exist and just compile without it? 回答1: No this is not possible. When compiling a class, the compiler has no "memory" of which classes were already "loaded" (don't

gcc complex constant folding

和自甴很熟 提交于 2019-12-30 08:26:10
问题 It seems that gcc has some limitation on complex constant folding. Here is an example: static inline unsigned int DJBHash(const char *str) { int i; unsigned int hash = 5381; for(i = 0; i < strlen(str); i++) { hash = ((hash << 5) + hash) + str[i]; } return hash; } int f(void) { return DJBHash("01234567890123456"); } When running with -O3 optimization level (gcc 4.8), it unfolds the loop in DJBHash nicely and calculates the hash value for that string during compile time. However, when making

How do optimizing compilers decide when and how much to unroll a loop?

十年热恋 提交于 2019-12-30 08:17:16
问题 When a compiler performs a loop-unroll optimization, how does it determined by which factor to unroll the loop or whether to unroll the whole loop? Since this is a space-performance trade-off, on average how effictive is this optimization technique in making the program perform better? Also, under what conditions is it recommended to use this technique (i.e certain operations or calculations)? This doesn't have to be specific to a certain compiler. It can be any explanation outlining the idea

Does C# Compiler calculate math on constants?

断了今生、忘了曾经 提交于 2019-12-30 08:13:31
问题 Given the following code: const int constA = 10; const int constB = 10; function GetX(int input) { int x = constA * constB * input; ... return x; } Will the .Net compiler 'replace' the expression and put 1000 so the calculation won't be repeated over and over? In what siutation will the code run fastest: int x = constA * constB * input; int x = 10 * 10 * input; int x = 100 * input; I guess option 3 will be the faster then 2 but sometimes not the most readable option. Does the compiler

Does JavaScript compile the function every time it is invoked?

为君一笑 提交于 2019-12-30 07:34:57
问题 Say I have this function: function A() { function B() { return 1; } return 1 + B(); } Does function B gets compiled every time when I call function A ? I remember someones says it won't. But my memory gets rusty, and I cannot find the reference. 回答1: The JavaScript standard states that a JavaScript execution environment must parse the function and produce early errors refusing to execute any code in the script if they exist (such as missing close quotes, unmatched curly braces etc.). It says

I got 'duplicate section' errors when compiling boost_regex with size optimization (-Os)

▼魔方 西西 提交于 2019-12-30 03:11:05
问题 compiler: http://sourceforge.net/projects/mingwbuilds/files/host-windows/releases/4.7.2/32-bit/threads-posix/sjlj/x32-4.7.2-release-posix-sjlj-rev6.7z boost: http://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.7z (both on D: drive) boost_regex compiled with: b2 --prefix=D:\boost toolset=gcc --with-regex --layout=tagged release code: #include <boost\regex.hpp> int main() { boost::regex reg("[a-z]+"); } compiled with parameters: g++ -I "d:\boost" -Os -o test.exe test.cpp

Integer division by 7

ε祈祈猫儿з 提交于 2019-12-30 02:00:07
问题 Source my answer in: Is this expression correct in C preprocessor I'm a little bit out of my forte here, and I'm trying to understand how this particular optimization works. As mentioned in the answer, gcc will optimize integer division by 7 to: mov edx, -1840700269 mov eax, edi imul edx lea eax, [rdx+rdi] sar eax, 2 sar edi, 31 sub eax, edi Which translates back into C as: int32_t divideBySeven(int32_t num) { int32_t temp = ((int64_t)num * -015555555555) >> 32; temp = (temp + num) >> 2;

benchmarking, code reordering, volatile

浪子不回头ぞ 提交于 2019-12-29 14:23:18
问题 I decide I want to benchmark a particular function, so I naïvely write code like this: #include <ctime> #include <iostream> int SlowCalculation(int input) { ... } int main() { std::cout << "Benchmark running..." << std::endl; std::clock_t start = std::clock(); int answer = SlowCalculation(42); std::clock_t stop = std::clock(); double delta = (stop - start) * 1.0 / CLOCKS_PER_SEC; std::cout << "Benchmark took " << delta << " seconds, and the answer was " << answer << '.' << std::endl; return 0

benchmarking, code reordering, volatile

萝らか妹 提交于 2019-12-29 14:21:05
问题 I decide I want to benchmark a particular function, so I naïvely write code like this: #include <ctime> #include <iostream> int SlowCalculation(int input) { ... } int main() { std::cout << "Benchmark running..." << std::endl; std::clock_t start = std::clock(); int answer = SlowCalculation(42); std::clock_t stop = std::clock(); double delta = (stop - start) * 1.0 / CLOCKS_PER_SEC; std::cout << "Benchmark took " << delta << " seconds, and the answer was " << answer << '.' << std::endl; return 0