Why is this seemingly slower C loop actually twice as fast as the other way?

If you want fast vectorized code, don’t do short-circuit evaluation, and don’t branch in general. You want the compiler to be able to do 16 or 32 elements at once with SIMD operations, using 8-bit elements. (Compilers can optimize ifs to branchless code if it’s safe to do the work unconditionally, including dereferences, and there … Read more

Why don’t compilers merge redundant std::atomic writes?

The C++11 / C++14 standards as written do allow the three stores to be folded/coalesced into one store of the final value. Even in a case like this: y.store(1, order); y.store(2, order); y.store(3, order); // inlining + constant-folding could produce this in real code The standard does not guarantee that an observer spinning on y … Read more

Is a C compiler allowed to coalesce sequential assignments to volatile variables?

No, the compiler is absolutely not allowed to optimize those two writes into a single double word write. It’s kind of hard to quote the standard since the part regarding optimizations and side effects is so fuzzily written. The relevant parts are found in C17 5.1.2.3: The semantic descriptions in this International Standard describe the … Read more

Why is there no implicit parallelism in Haskell?

This is a long studied topic. While you can implicitly derive parallelism in Haskell code, the problem is that there is too much parallelism, at too fine a grain, for current hardware. So you end up spending effort on book keeping, not running things faster. Since we don’t have infinite parallel hardware, it is all … Read more

Compiler optimizations may cause integer overflow. Is that okay?

As Miles hinted: The C++ code text is bound by the rules of the C++ language (integer overflow = bad), but the compiler is only bound by the rules of the cpu (overflow=ok). It is allowed to make optimizations that the code isn’t allowed to. But don’t take this as an excuse to get lazy. … Read more

Can the compiler optimize from heap to stack allocation?

Yes, it’s legal. expr.new/10 of C++14: An implementation is allowed to omit a call to a replaceable global allocation function (18.6.1.1, 18.6.1.2). When it does so, the storage is instead provided by the implementation or provided by extending the allocation of another new-expression. expr.delete/7: If the value of the operand of the delete-expression is not … Read more

Why does MSVS not optimize away +0? [duplicate]

The compiler cannot eliminate the addition of a floating-point positive zero because it is not an identity operation. By IEEE 754 rules, the result of adding +0. to −0. is not −0.; it is +0. The compiler may eliminate the subtraction of +0. or the addition of −0. because those are identity operations. For example, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)