www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/18/04:51:29

From: Calvin French <french AT freenet DOT calgary DOT ab DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Will djgpp optimize recursive function calls?
Date: Sun, 17 Nov 1996 16:15:22 -0700
Organization: Calgary Free-Net
Lines: 83
Message-ID: <Pine.A32.3.93.961117160639.56302C-100000@srv1.freenet.calgary.ab.ca>
References: <Pine DOT A32 DOT 3 DOT 93 DOT 961115194737 DOT 37954A-100000 AT srv1 DOT freenet DOT calgary DOT ab DOT ca> <328E1F11 DOT 5F69 AT cs DOT com> <328F7903 DOT 5B8E AT spy DOT isp DOT nsc DOT ru>
NNTP-Posting-Host: french AT srv1 DOT freenet DOT calgary DOT ab DOT ca
Mime-Version: 1.0
In-Reply-To: <328F7903.5B8E@spy.isp.nsc.ru>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Sun, 17 Nov 1996, Ilya P. Ryzhenkov wrote:

> Hmmmm.... It's not true perfectly ... Consider :
> inline unsigned long fact(unsigned long i) /* I don't know if it will be
> inlined without inline keyword, but i hope */
> { 
>  return (i<2)?i:fract(i-1)*i;
> }
> void main()
> {
>  fract(0); fract(1); fract(2);
> }
> and compile this with gcc -O3 -o test.s test.c -S
> and look into resulting test.s file
> You'll see, that GCC DO optimizes (inline), but only for the FIRST call.
> so you will actually get
>  fract(x-1)*x for the call like fract(x) and a constant if you use
> frac(0) or fract(1)

I'm caving in <grin>. I normally don't post just general procedural
questions, but I'm afraid that I can't work through this hand-recursion. I
mean, I understand what it does, and I'm HALF there, but there is
something I'm missing about the stack, etc. So if somebody could help me
out it would be great. This is as far as I can get:

(original recursive function)

void recurse( int a, int b )
{
  // do stuff.
  if( whatever ) recurse( (a+b) / 2, b );
  if( whatever else ) recurse( a, (a+b) / 2 );
};

(and I've expanded this to:)

void nonrecurse( int a, int b )
{
  int A = a, B = b;
  for( ;; )
  {
    for( ;; )
    {
      // do stuff.
      if( whatever )
      {
	// ** What do I have to do here? gather, "push(a), push(b)" to my
	// little stack, right?
	A = (A+B) / 2;
      } else {
	break;
      }
    }
    if( whatever else )
    {
      // ** And here? I take it I have to pop(&b), pop(&a) but this
      // doesn't work. I have tried many variants on this but nothing :(
      B = (A+B) / 2;
    } else {
      break;
    }
  }
};

Something is obviously amiss. Please help me!!! <uncontrollable sobbing>

Again, I normally don't post general procedural (a.k.a., get off your arse
and figure it out yourself) and I *DO* understand recursion, and I *DO*
program assembler, and I *AM* competant, well, maybe. But please somebody
help!

= Calvin =
--------------------------------------------------------------------
B013CD10B7A08EC3B2C8B940018BC133C2AAE2F9FECA75F2B407CD21B80300CD10C3
--------------------------------------------------------------------
  -- I apologuise, Shaun began, but I would rather spinooze
you one from the grimm gests of Jacko and Esaup, fable one,
feeble too. Let us here consider the casus, my dear little cousis
(husstenhasstencaffincoffintussemtossemdamandamnacosaghcusa-
ghhobixhatouxpeswchbechoscashlcarcarcaract) of the Ondt and
the Gracehoper.
--------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019