Compiler optimizations

Craig Black cblack at ara.com
Sun Apr 30 09:53:24 PDT 2006


"dennis luehring" <dl.soluz at gmx.net> wrote in message
news:e32dfl$1f53$1 at digitaldaemon.com...
> > Please anyone, correct me if I'm wrong, but I think IDIV is a
> > pseudo-instruction.  80x86 is notorious for those (as opposed to RISC).
I
> > read on the Intel website that intel processors do not have a hardware
> > integer division instruction.  Apparently it is somehow better to use
the
> > floating point instruction instead.
>
> i don't know the truth - but why should all the compiler vendors around
> us (microsoft, intel, ...) produce the slower code if there is an faster
> solution? what is the reason (maybe it produce lager code - it this will
> produce more cache problems...)

I'm not saying that there is a faster solution, but there might be.  Mainly,
I'm just trying to learn more so that I will be better at optimizing my
code.

> > The benchmarks that I've done are constistent with this.  I found it was
> > faster to perform a floating-point multiplication on an integer and then
> > convert it back to an integer than just a single integer division.
>
> can you post your benchmark source?
>
> > Have you run benchmarks?
>
> no - but i can run your benchmark...

These two functions show the difference that I'm talking about.  When
benchmarking, pass in the same values for the divisor (pick a value between
2 and 30), and pass in a large enough value for total so that it will take
some time for the CPU to finish.  Suprisingly, the second one is faster.

-Craig

int divTest1(int divisor, int total)
{
  int sum = 0;
  for(int i = 0; i < total; i++)
  {
    int quotient = i / divisor;
    sum += quotient;
  }
}

int divTest2(int divisor, int total)
{
  int sum = 0;
  double idiv = 1.0 / divisor;
  for(int i = 0; i < total; i++)
  {
    int quotient = i * idiv;
    sum += quotient;
  }
}





More information about the Digitalmars-d mailing list