compiler-optimization

Are compilers allowed to optimize floating point constant multiplication

↘锁芯ラ 提交于 2020-01-05 08:21:18
问题 the questionable Code is the following: float32_t f = someValueFromSomewhere; f = f * 4; will compiler optimize this? According to C-Standard (if i understood it correctly) the second operand has to be promoted to float32_t ; So the multiplication has to be done using the FPU (or fp emulation). Theoretically the operation could be done in a normal hardware register by just adding an immediate (and may be overflow checking ). Are compiler allowed to do this optimization? Are there compiler

Is it viable to mix two static libraries with different optimization levels?

扶醉桌前 提交于 2020-01-05 05:49:07
问题 I have two static libraries named libx.a and liby.a . libx.a is compiled with gcc -g ; while liby.a is compiled with gcc -O3 . I want to link them two into a single executable. Is it viable? Is it harmful? 回答1: Yes, it is viable, it isn't harmful as long as the optimizations don't change the ABI (of function calls, or of floating point arithmetic/representation, etc.). Although even in those cases, I believe all necessary information is already compiled in or the linker resolves the issues.

Why do we even need assembler when we have compiler?

放肆的年华 提交于 2020-01-04 07:09:10
问题 If compiler converts high-level language to machine code, why do we even need assembler? Are there any assembly level language and we can't use compiler for that? 回答1: why do we even need assembler? Many people don't need to know assembly language. It exists so we can talk about / analyze machine code, and write/debug compilers more easily. Compilers have to be written by humans . As @old_timer points out, when designing a new CPU architecture, you always give names to the opcodes and

Does the compiler take care the useless codes like if(0)?

泪湿孤枕 提交于 2020-01-03 16:00:00
问题 Recently, I am compiling the ffmpeg codes under windows use the VS2010 with intel compiler. For the following codes: void ff_dcadsp_init(DCADSPContext *s) { s->lfe_fir = dca_lfe_fir_c; if (ARCH_ARM) ff_dcadsp_init_arm(s); } the macro ARCH_ARM is defined as 0 . When I compile it under linux, there is no function in ff_dcadsp_init_arm() in the object file, while it does under windows? So I want to make sure if the compiler will do something about the useless codes and how to set it for INTEL

Does the compiler take care the useless codes like if(0)?

烈酒焚心 提交于 2020-01-03 15:59:03
问题 Recently, I am compiling the ffmpeg codes under windows use the VS2010 with intel compiler. For the following codes: void ff_dcadsp_init(DCADSPContext *s) { s->lfe_fir = dca_lfe_fir_c; if (ARCH_ARM) ff_dcadsp_init_arm(s); } the macro ARCH_ARM is defined as 0 . When I compile it under linux, there is no function in ff_dcadsp_init_arm() in the object file, while it does under windows? So I want to make sure if the compiler will do something about the useless codes and how to set it for INTEL

Force GCC not to optimize away an unused variable?

安稳与你 提交于 2020-01-03 14:16:58
问题 One of the namespaces in my program is spread between two files. One provides the "engine", the other uses the "engine" to perform various commands. All of the initializations are performed on the "engine" side, including caching parameters fetched from setup library. So, there's engine.cpp with: #include <stdio.h> #include "ns.h" namespace MyNS { unsigned char variable = 0; void init() { variable = 5; printf("Init: var = %d\n",variable); } void handler() { // printf("Handler: var = %d\n"

Force GCC not to optimize away an unused variable?

江枫思渺然 提交于 2020-01-03 14:15:22
问题 One of the namespaces in my program is spread between two files. One provides the "engine", the other uses the "engine" to perform various commands. All of the initializations are performed on the "engine" side, including caching parameters fetched from setup library. So, there's engine.cpp with: #include <stdio.h> #include "ns.h" namespace MyNS { unsigned char variable = 0; void init() { variable = 5; printf("Init: var = %d\n",variable); } void handler() { // printf("Handler: var = %d\n"

Performance difference of signed and unsigned integers of non-native length

一笑奈何 提交于 2020-01-03 06:04:40
问题 There is this talk, CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior...", where Mr. Carruth shows an example from the bzip code. They have used uint32_t i1 as an index. On a 64-bit system the array access block[i1] will then do *(block + i1) . The issue is that block is a 64-bit pointer whereas i1 is a 32-bit number. The addition might overflow and since unsigned integers have defined overflow behavior the compiler needs to add extra instructions to

Performance difference of signed and unsigned integers of non-native length

好久不见. 提交于 2020-01-03 06:04:10
问题 There is this talk, CppCon 2016: Chandler Carruth “Garbage In, Garbage Out: Arguing about Undefined Behavior...", where Mr. Carruth shows an example from the bzip code. They have used uint32_t i1 as an index. On a 64-bit system the array access block[i1] will then do *(block + i1) . The issue is that block is a 64-bit pointer whereas i1 is a 32-bit number. The addition might overflow and since unsigned integers have defined overflow behavior the compiler needs to add extra instructions to

Should infinite loop condition variables always be declared as volatile?

ぃ、小莉子 提交于 2020-01-03 04:48:26
问题 Consider, such type of code, while( !cond ) ; If cond is not declared volatile, the compiler can optimize it by caching it in a register. In that case, the while loop will continue even after cond is set. Now does that mean that any such kind of variable should always be declared volatile ? Why aren't the compilers smart enough to realize that it should not cache such variables? 回答1: Why would it not cache the variable? You don't change it in the loop, so it's equivalent, in C's mind, to