Questions tagged [constantfolding]

Constant folding is related compiler optimizations used by many modern compilers. Constant folding is process of recognizing & evaluating a static expression at compile rather than computing them at runtime .

43 questions
75
votes
3 answers

Why 0.1 + 0.2 == 0.3 in D?

assert(0.1 + 0.2 != 0.3); // shall be true is my favorite check that a language uses native floating point arithmetic. C++ #include int main() { printf("%d\n", (0.1 + 0.2 != 0.3)); return…
Stas
  • 11,571
  • 9
  • 40
  • 58
57
votes
7 answers

Why an expression instead of a constant, in a C for-loop's conditional?

In many programming competitions I have seen people write this type of for-loop for(i = 0; i < (1 << 7); i++) Unless I am missing something, that's the same as for(i = 0; i < 128; i++) Why use the (1 << 7) version? Isn't calculating the condition…
harrythomas
  • 1,407
  • 1
  • 13
  • 17
47
votes
1 answer

Why does GCC implement isnan() more efficiently for C++ than C ?

Here's my code: int f(double x) { return isnan(x); } If I #include 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…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
45
votes
2 answers

log(10.0) can compile but log(0.0) cannot with undefined reference?

For the following C source code: #include int main(void) { double x; x = log(0.0); return 0; } When I compile with gcc -lm, I got: /tmp/ccxxANVH.o: In function `main': a.c:(.text+0xd): undefined reference to…
xuhdev
  • 8,018
  • 2
  • 41
  • 69
13
votes
2 answers

variable length array folded to constant array

const int buf_length = 255; char buf[ buf_length + 1 ]; snprintf(buf, buf_length, "%d Next on [%s] %s:", channel, station_channel(channel), station_name(channel)); strncat(buf, "(", buf_length - strlen (buf)); strncat(buf, station_country( xmltv…
user1840007
  • 615
  • 1
  • 10
  • 19
10
votes
1 answer

C++ constant folding a loop for prime numbers

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 using namespace std; inline bool is_prime(int n) { …
ar2015
  • 5,558
  • 8
  • 53
  • 110
10
votes
1 answer

why to avoid constant folding in Java? When?

I saw some codes in slf4j as show below. I don't know why to avoid constant folding in here. Is it necessary to do that? or just best practice. what's the benefit of doing this? Thanks. /** * Declare the version of the SLF4J API this…
easycoder
  • 305
  • 2
  • 3
  • 12
9
votes
2 answers

What is constant folding in java compiler?

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?
giri
  • 26,773
  • 63
  • 143
  • 176
9
votes
4 answers

gcc complex constant folding

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) +…
8
votes
2 answers

How does O=Deparse work, and does Perl have and fold constant arrays?

I'm wondering, does -MO=Deparse show you all of the Perl optimizations, and why doesn't this get folded in Perl 5.10? $ perl -MO=Deparse -e'[qw/foo bar baz/]->[0]' ['foo', 'bar', 'baz']->[0]; -e syntax OK Some on IRC thought that O=Deparse might…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
8
votes
2 answers

Subtracting uint and int and constant folding

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…
InBetween
  • 32,319
  • 3
  • 50
  • 90
7
votes
1 answer

What are the specific rules for constant folding?

I just realized that CPython seems to treat constant expressions, which represent the same value, differently with respect to constant folding. For example: >>> import dis >>> dis.dis('2**66') 1 0 LOAD_CONST 0 (2) …
a_guest
  • 34,165
  • 12
  • 64
  • 118
6
votes
1 answer

A trick to prevent the compiler from constant folding out an expression

I have a string literal in my program, I'm trying to create an amateur checksum to ensure the string literal was not replaced in the portable executable. To do this, I create a hash of the string literal, and store it as a integer literal in the…
Thomas
  • 6,032
  • 6
  • 41
  • 79
5
votes
3 answers

Does Java Compiler include String Constant Folding?

I found out that Java supports constant folding of primitive types, but what about Strings? Example If I create the following source code out.write("" + "" + "" + "Easier to read if it is split into multiple…
700 Software
  • 85,281
  • 83
  • 234
  • 341
5
votes
1 answer

Constant folding in the preprocessing stage

I have a piece of C code I need to deobfuscate. It contains a bunch of tricky macros. I ran the code through C preprocessor and indent and now it looks similar to this: switch (9263 + 1505) { case 1505 + 41131 + 6729 + 2347: ... case…
Pavel Zaichenkov
  • 835
  • 5
  • 12
1
2 3