[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: [Tinycc-devel] fib.c

From: Laszlo Hars
Subject: Re: Re: [Tinycc-devel] fib.c
Date: Fri, 16 Mar 2007 12:46:58 -0700
User-agent: Web-Based Email 4.9.21


Your code is a nice demonstration of a general technique to convert
loops to (tail)recursive calls, by passing the loop variable and the
list of changed variables as parameters. It is a good test for the
optimization capabilities of the compiler: it should convert the code
back to iterative (loop) form. Otherwise, one should use something like

typedef unsigned long long UI64;

UI64 fib(int const n)
    int i; UI64 t, a = 0, b = 1;
    for( i = 1; i < n && a <= b; ++i) {
        t = b; b += a, a = t; }
    return b < a ? 0 : b;

Your code and this one relies on the "wrap-around at overflow" behavior
of the compiler. I did not find it documented. Have I missed it? There
are compilers, which return the largest possible number at an overflow,
others return some special value. Is it safe to rely on this

Laszlo Hars

reply via email to

[Prev in Thread] Current Thread [Next in Thread]