[metapost] recursion in MP/ MF [was: all intersections between two paths]

Laurence Finston lfinsto1 at gwdg.de
Mon Jan 17 23:09:27 CET 2005


Hello,

I made the following experiment to test whether GCC 
(the GNU Compiler Collection) would optimize recursive 
function calls such that tail recursion would be performed.
The results of the experiment indicate that it does not.  
I have tried to make the program as good a candidate as 
possible for this, but perhaps I'm missing something.
If so, I'd appreciate it if someone would put me straight.

Thanks,

Laurence

********* The test program.

// testrcrs.c++
// Created by Laurence Finston Mo Jan 17 22:18:16 CET 2005

#include <iomanip>
#include <iostream>
#include <stdio.h>   
#include <stdlib.h>

using namespace std;

void
a(int ctr)
{
   cerr << "In `a':  `ctr' == " << ctr << endl;
   if (ctr > 0)
      a(ctr - 1);
   return;
}


int
main (int argc, char** argv)
{

   a(3);

   return 0;
 
} 

***** Excerpts from the GCC man page:

This indicates that using the `-g' option to `g++' should not
cause tail recursion to be disabled. 

***************************

COPYRIGHT
       Copyright (c) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
       1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.

           Unlike most other C compilers, GCC allows you to use -g with -O.
           The shortcuts taken by optimized code may occasionally produce sur-
           prising results: some variables you declared may not exist at all;
           flow of control may briefly move where you did not expect it; some
           statements may not be executed because they compute constant
           results or their values were already at hand; some statements may
           execute in different places because they were moved out of loops.

       -foptimize-sibling-calls
           Optimize sibling and tail recursive calls.

           Enabled at levels -O2, -O3, -Os.

********** The compilation command:

g++ -g -O3 testrcrs.c++

*********** Excerpts from a gdb (The GNU Debugger) session: 

run
Starting program: /ramdisk/home/knoppix/tmp/a.out 

Breakpoint 1, a (ctr=1076064912) at testrcrs.c++:16

Breakpoint 1, a (ctr=3) at testrcrs.c++:16
(gdb) bt
#0  a (ctr=3) at testrcrs.c++:16
#1  0x08048853 in a (ctr=3) at testrcrs.c++:19
#2  0x08048885 in main (argc=1, argv=0xbffffc74) at testrcrs.c++:30

Breakpoint 1, a (ctr=2) at testrcrs.c++:16
(gdb) bt
#0  a (ctr=2) at testrcrs.c++:16
#1  0x08048853 in a (ctr=2) at testrcrs.c++:19
#2  0x08048853 in a (ctr=3) at testrcrs.c++:19
#3  0x08048885 in main (argc=1, argv=0xbffffc74) at testrcrs.c++:30

Breakpoint 1, a (ctr=1) at testrcrs.c++:16
(gdb) bt
#0  a (ctr=1) at testrcrs.c++:16
#1  0x08048853 in a (ctr=1) at testrcrs.c++:19
#2  0x08048853 in a (ctr=2) at testrcrs.c++:19
#3  0x08048853 in a (ctr=3) at testrcrs.c++:19
#4  0x08048885 in main (argc=1, argv=0xbffffc74) at testrcrs.c++:30



More information about the metapost mailing list