This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Recursion Optimization


Zack Weinberg wrote:

> A genius compiler would recognize that
> the values pushed on the stack are predictable, and generate
>
> int sum(int i, int stop)
> {
>     int tmp = 0;
>     while(i <= stop) tmp += i++;
>     return tmp;
> }

And a genius compiler who had also gone to school would recognise
that as a sum of a sequence and generate

     int sum (int i, int stop)
     {
       return (stop * (stop + 1) - i * (i + 1)) / 2;
     }

-- then get it wrong if i or stop are negative.

That would be fixed in the next release, of course ;-)

    Brane

--
Branko Čibej                 <branko.cibej@hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
voice: (+386 61) 186 53 49   fax: (+386 61) 186 52 70



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]