home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!inmos!titan.inmos.co.uk!news
- From: conor@lion.inmos.co.uk (Conor O'Neill)
- Subject: Re: To Recurse or NOT to recurse?
- Message-ID: <1992Nov12.155509.16761@titan.inmos.co.uk>
- Sender: news@titan.inmos.co.uk
- Reply-To: conor@inmos.co.uk (Conor O'Neill)
- Organization: INMOS Limited, Bristol, UK.
- References: <1992Nov6.185136.8055@netcom.com> <4223@litchi.bbn.com> <10920@baird.cs.strath.ac.uk> <BxKAJ2.7H@netnews.jhuapl.edu>
- Date: Thu, 12 Nov 1992 15:55:09 GMT
- Lines: 49
-
- In article <BxKAJ2.7H@netnews.jhuapl.edu> bandy@netnews.jhuapl.edu (Mike Bandy) writes:
- >I've used C on a few platforms and have never seen an optimization that
- >converts recursion to anything but an optimized recursion. What you're
- >saying is that the optimizer will replace user-defined function calls
- >with in-line code. Can someone give an example of a compiler (and perhaps
- >some code) that verifies this?
-
- The INMOS optimising ANSI C compiler produces essentially identical code
- for the following two examples; it happily optimises the tail recursion.
-
- #include <stdlib.h>
- typedef int data;
- typedef struct tree_s
- {
- data *d;
- struct tree_s *left;
- struct tree_s *right;
- } tree_t;
-
- /* recursive function */
-
- tree_t *find(tree_t *t, data *d) {
- if (t == NULL) /* sentinel */
- return NULL;
- else if (t->d == d) /* node found? */
- return t;
- else if (t->d > d) /* search left */
- return find(t->left, d);
- else /* search right */
- return find(t->right, d);
- }
-
- /* non-recursive function */
-
- tree_t *find(tree_t *t, data *d) {
- while (t != NULL)
- if (t->d == d) /* node found? */
- return t;
- else if (t->d > d) /* search left */
- t= t->left;
- else /* search right */
- t= t->right;
- return NULL; /* sentinel */
- }
-
- ---
- Conor O'Neill, Software Group, INMOS Ltd., UK.
- UK: conor@inmos.co.uk US: conor@inmos.com
- "It's state-of-the-art" "But it doesn't work!" "That is the state-of-the-art".
-