Re: un-requested compiler optimisations

2011-04-14 Thread David Nadlinger

On 4/14/11 8:24 PM, spir wrote:

If it does optimise, then it is definitely a compiler bug. Since you
*explicitely* ask for a double reverse, it *must* just do it.
Suppressing them is here just breaking the language's semantics!
[…]


Sigh… First, as Steve already pointed out, retro() specifically 
shortcuts reversing a range twice to the original range.


Second, your remarks about optimization are not quite correct, at least 
for most of today's high-level languages. A compiler is free to optimize 
a program in whatever possible way, as long as it can prove the result 
equivalent to the original. And as burning CPU-cycles is not part of the 
program semantics, a modern optimizing compiler is free to optimize the 
loop and factorial call away altogether. If you don't believe me, have a 
look at the attached sample program and the asm generated for main() 
(some recent Clang version, clang -O3).


David


---
#include stdio.h

int factorial(int a) {
  int p = 1;
  for (int i = 2; i = a; ++i) {
p *= i;
  }
  return p;
}

int main (int argc, char const *argv[]) {
  int t;
  for (long i = 0; i  1000; ++i) {
t = factorial(9);
  }
  printf(%d\n, t);
  return 0;
}


_main:
00010ee0pushq   %rbp
00010ee1movq%rsp,%rbp
00010ee4leaq0x0041(%rip),%rdi
00010eebmovl$0x00058980,%esi ; 9! in hex
00010ef0xorb%al,%al
00010ef2callq   0x10f02; symbol stub for: _printf
00010ef7xorl%eax,%eax
00010ef9popq%rbp
00010efaret


Re: un-requested compiler optimisations

2011-04-14 Thread spir

On 04/14/2011 08:33 PM, Steven Schveighoffer wrote:

If it does optimise, then it is definitely a compiler bug. Since you
*explicitely* ask for a double reverse, it *must* just do it. Suppressing
them is here just breaking the language's semantics!


I feel like people aren't looking at my post :)


Sorry, I wrote this reply before reading yours.

denis
--
_
vita es estrany
spir.wikidot.com



Re: un-requested compiler optimisations

2011-04-14 Thread Timon Gehr
 Should the compiler optimise by computing n only once? (even possibly at
compile-time)
 Then, what if I'm doing that in purpose? (be it stupid or not)

 Denis

What is the difference? The only thing that is affected by this optimization is
execution speed. Optimizing is about implementing the requested semantics in an
efficient matter. Why would you actually want to suppress completely safe
optimizations? Also, when benchmarking, you don't usually want to benchmark
unoptimized code. Or am I misunderstanding something?

If absolutely necessary, you can always use the volatile keyword to disable all
optimizations on a given variable, or call the function explicitly using inline 
ASM.


Re: un-requested compiler optimisations

2011-04-14 Thread bearophile
Timon Gehr:

 If absolutely necessary, you can always use the volatile keyword to disable 
 all
 optimizations on a given variable, or call the function explicitly using 
 inline ASM.

volatile is present only in D1...

Bye,
bearophile