Mail Archives: djgpp/1996/11/18/04:51:29
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 -