compiler-optimization

What do gcc's auto-vectorization messages mean?

大城市里の小女人 提交于 2019-12-10 16:38:32
问题 I have some code that I would like to run fast, so I was hoping I could persuade gcc (g++) to vectorise some of my inner loops. My compiler flags include -O3 -msse2 -ffast-math -ftree-vectorize -ftree-vectorizer-verbose=5 but gcc fails to vectorize the most important loops, giving me the following not-really-very-verbose-at-all messages: Not vectorized: complicated access pattern. and Not vectorized: unsupported use in stmt. My questions are (1) what exactly do these mean? (How complicated

java optimization by compiler or JIT

倖福魔咒の 提交于 2019-12-10 16:00:35
问题 From time to time I see code like this: if (id.split(":").length > 1) { sub_id = id.split(":")[1]; parent_id = id.split(":")[0]; } Wouldn't it be better (and faster) to do something like String [] ids = id.split(":"); if (ids.length > 1) { sub_id = ids[1]; parent_id = ids[0]; } This way you don't have to call 'split()' multiple times, or will the compiler/JIT do such optimizations? 回答1: I certainly wouldn't expect either the JIT or the compiler do perform such optimizations. It would have to

Why doesn't GCC throw a warning in this example

北城余情 提交于 2019-12-10 14:55:36
问题 With -Wsequence-point enabled, GCC should warn user when undefined behavior code is spotted. For example b = a + ++a; should be noticed by GCC and should be reported as "undefined behavior" code (because ISO C doesn't specify the order of evaluating operands for addition). However, I played with the syntax and I tried this one: int *a = malloc(sizeof(int) * 2); a[0] = 1; printf("%d\n", *(a + (*a)++ - *a)); Of course, I got the warning warning: operation on '*a' may be undefined [-Wsequence

C - how would compiler optimization affect a for loop with no body?

十年热恋 提交于 2019-12-10 14:20:10
问题 I have some legacy code where a timewasting loop has been included to allow time for an eeprom read to complete (bad practice): for(i = 0; i < 50; i++); However, peculiar things happen when compiler optimizations are switched on for speed. It is not necessarily connected with that statement, but I would like to know if the compiler might just optimize the time delay away 回答1: It depends on the type of i . If it is just a plain integer type that isn't used apart from inside the loop, there are

C++ while loop optimization not working properly

我是研究僧i 提交于 2019-12-10 14:03:37
问题 I have this code segment: #include <stdio.h> int main(int argc, const char** argv) { int a = argv[0][0]; int b = argv[0][1]; while ((a >= 0) && (a < b)) { printf("a = %d\n", a); a++; } return 0; } and I'm compiling it with gcc-4.5 -02 -Wstrict-overflow=5 . The compiler yells at me warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C1 +- C2 What does this mean exactly? If i am correct, this loop will never cause an overflow, because for a to be incremented,

In Rust, is Option compiled to a runtime check or an instruction jump?

做~自己de王妃 提交于 2019-12-10 13:18:05
问题 In Rust, Option is defined as: pub enum Option<T> { None, Some(T), } Used like so: fn may_return_none() -> Option<i32> { if is_full_moon { None } else { Some(1) } } fn main() { let optional = may_return_none(); match optional { None => println!("None"), Some(v) => println!("Some"), } } I'm not familiar with Rust internals, but initially I assumed it might work similar to Nullable in .NET, so the compiled logic of my above Rust code would be like so: // occupies `sizeof(T) + 1` memory space,

Is there any guarantee about whether code with UB should be reachable?

◇◆丶佛笑我妖孽 提交于 2019-12-10 13:07:51
问题 I have a code snippet from here: volatile int volatileInt; int usualInt; void function (unsigned x, unsigned y, unsigned z) { volatileInt = 0; usualInt = (x % y) / z; } int main() { function(rand(), rand(), rand()); } which I compile with Visual C++ 10 with /O2 and get this disassembly: 00403940 push ebx 00403941 push esi 276: function(rand(), rand(), rand()); 00403942 mov esi,dword ptr [__imp__rand (4050C0h)] 00403948 push edi 00403949 call esi 0040394B mov edi,eax 0040394D call esi 0040394F

Is there a -ffast-math flag equivalent for the Visual Studio C++ compiler

◇◆丶佛笑我妖孽 提交于 2019-12-10 12:57:46
问题 I'm working with the default C++ compiler (I guess it's called the "Visual Studio C++ compiler") that comes with Visual Studio 2013 with the flag /Ox (Full Optimization). Due to floating point side effects, I must disable the -ffast-math flag when using the gcc compiler. Is there an equivalent option for this flag in the configuration of the Visual Studio C++ compiler? 回答1: You are looking for /fp:precise , although that is also the default. If you need the strictest floating point

Does the C++ standard force capture-by-reference of local variables to be inefficient? [duplicate]

戏子无情 提交于 2019-12-10 12:35:53
问题 This question already has answers here : When a C++ lambda expression has a lot of captures by reference, the size of the unnamed function object becomes large (3 answers) Closed 4 years ago . I recently needed a lambda that captured multiple local variables by reference, so I made a test snippet to investigate its efficiency, and compiled it with -O3 using clang 3.6: void do_something_with(void*); void test() { int a = 0, b = 0, c = 0; auto func = [&] () { a++; b++; c++; }; do_something_with

Why doesn't GCC optimize this call to printf?

主宰稳场 提交于 2019-12-10 12:31:23
问题 #include <stdio.h> int main(void) { int i; scanf("%d", &i); if(i != 30) { return(0); } printf("i is equal to %d\n", i); } It appears that the resulting string will always be "i is equal to 30", so, why doesn't GCC optimize this call to printf with a call to puts() , or write() , for example? (Just checked the generated assembly, with gcc -O3 (version 5.3.1), or on the Godbolt Compiler Explorer) 回答1: First of all, the problem is not the if ; as you saw, gcc sees through the if and manages to