constantfolding

Why does GCC implement isnan() more efficiently for C++ <cmath> than C <math.h>?

陌路散爱 提交于 2019-12-17 22:22:32
问题 Here's my code: int f(double x) { return isnan(x); } If I #include <cmath> I get this assembly: xorl %eax, %eax ucomisd %xmm0, %xmm0 setp %al This is reasonably clever: ucomisd sets the parity flag if the comparison of x with itself is unordered, meaning x is NAN. Then setp copies the parity flag into the result (only a single byte, hence the initial clear of %eax ). But if I #include <math.h> I get this assembly: jmp __isnan Now the code is not inline, and the __isnan function is certainly

Avoid switching on types to allow constant folding

不打扰是莪最后的温柔 提交于 2019-12-12 05:29:10
问题 I am trying to find a class hierarchy that permits to implement place holders for processor registers and operations on it. It should also allow for constants to be folded at run time. For sake of simplicity I'll only look at one operation, here multiplication. Place holders and constants should be accessible uniformly, i.e. have a common base class. The code below defines the following classes: class A : Base class for place holders (registers) and constants class B : Place holder for a

Subtracting uint and int and constant folding

安稳与你 提交于 2019-12-08 17:06:41
问题 Based on this interesting question: Addition of int and uint and toying around with constant folding as mentioned in Nicholas Carey's answer, I've stumbled upon a seemingly inconsistent behavior of the compiler: Consider the following code snippet: int i = 1; uint j = 2; var k = i - j; Here the compiler correctly resolves k to long . This particular behavior is well defined in the specifications as explained in the answers to the previously referred question. What was surprising to me, is

C++ constant folding a loop for prime numbers

╄→гoц情女王★ 提交于 2019-12-03 12:06:54
Having had a look at previous questions 1 , 2 , I was wondering if I can force the compiler to perform a constant folding for the following code which prints prime numbers. #include <iostream> using namespace std; inline bool is_prime(int n) { if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } int main() { for(int i=0;i<20;i++) if(is_prime(i)) cout<<i<<endl; return 0; } And I build it via: g++ -O3 -S main.cpp -o main.asm As the result are a few: 2,3,5,7,11,13,17,19 I would like to force compiler look at the code similar to for(int x:{2,3,5,7,11,13,17,19})

gcc complex constant folding

风流意气都作罢 提交于 2019-12-01 03:48:51
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 the string one character longer return DJBHash("012345678901234567"); it does not fold it any more and

What is constant folding in java compiler? [duplicate]

梦想与她 提交于 2019-11-30 20:20:28
Possible Duplicate: is there any concept called “Constant Folding” in java? Hi I have come across line Java compiler uses something known as Constant Folding.What is this? and how Does it affect? Constant folding is where the compiler finds expressions that contain compile-time constants and replaces them with the result effectively removing redundant runtime calculations. // code static final int a = 2; int b = 30 * a; // folding would create int b = 60; Constant folding is the process of simplifying constant expressions at compile time. Terms in constant expressions are typically simple

Why 0.1 + 0.2 == 0.3 in D?

假装没事ソ 提交于 2019-11-30 05:50:26
问题 assert(0.1 + 0.2 != 0.3); // shall be true is my favorite check that a language uses native floating point arithmetic. C++ #include <cstdio> int main() { printf("%d\n", (0.1 + 0.2 != 0.3)); return 0; } Output: 1 http://ideone.com/ErBMd Python print(0.1 + 0.2 != 0.3) Output: True http://ideone.com/TuKsd Other examples Java: http://ideone.com/EPO6X C#: http://ideone.com/s14tV Why is this not true for D? As understand D uses native floating point numbers. Is this a bug? Do they use some specific

What is constant folding in java compiler? [duplicate]

我与影子孤独终老i 提交于 2019-11-30 04:29:14
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: is there any concept called “Constant Folding” in java? Hi I have come across line Java compiler uses something known as Constant Folding.What is this? and how Does it affect? 回答1: Constant folding is where the compiler finds expressions that contain compile-time constants and replaces them with the result effectively removing redundant runtime calculations. // code static final int a = 2; int b = 30 * a; //

Why does GCC implement isnan() more efficiently for C++ <cmath> than C <math.h>?

可紊 提交于 2019-11-28 18:34:25
Here's my code: int f(double x) { return isnan(x); } If I #include <cmath> I get this assembly: xorl %eax, %eax ucomisd %xmm0, %xmm0 setp %al This is reasonably clever: ucomisd sets the parity flag if the comparison of x with itself is unordered, meaning x is NAN. Then setp copies the parity flag into the result (only a single byte, hence the initial clear of %eax ). But if I #include <math.h> I get this assembly: jmp __isnan Now the code is not inline, and the __isnan function is certainly no faster the the ucomisd instruction, so we have incurred a jump for no benefit. I get the same thing

Does Java Compiler include String Constant Folding?

南笙酒味 提交于 2019-11-28 02:01:53
I found out that Java supports constant folding of primitive types , but what about String s? Example If I create the following source code out.write("" + "<markup>" + "<nested>" + "Easier to read if it is split into multiple lines" + "</nested>" + "</markup>" + ""); What goes into the compiled code? Combined Version? out.write("<markup><nested>Easier to read if it is split into multiple lines</nested></markup>"); Or the less efficient run-time concatenation version? out.write(new StringBuilder("").append("<markup>").append("<nested>").append("Easier to read if it is split into multiple lines"