>
>
C++ Tail Recursion Using 64-bit variabl…

Giovanni Campo
Articles: 2

C++ Tail Recursion Using 64-bit variables

I want to share with you a problem I run into comparing iterative and recursive functions in C++. There are several differences between recursion and iteration, this article explains the topic nicely if you want to know more. In general languages like Java, C, and Python, recursion is fairly expensive compared to iteration because it requires the allocation of a new stack frame. It is possible to eliminate this overhead in C/C++ enabling compiler optimization to perform tail recursion, which transforms certain types of recursion (actually, certain types of tail calls) into jumps instead of function calls. To let the compiler performs this optimization it is necessary that the last thing a function does before it returns is call another function (in this case itself). In this scenario it should be safe to jump to the start of the second routine. Main disadvantage of Recursion in imperative languages is the fact that not always is possible to have tail calls, which means an allocation of the function address (and relative variables, like structs for instance) onto the stack at each call. For deep recursive function this can cause a stack-overflow exception because of a limit to the maximum size of the stack, which is typically less than the size of RAM by quite a few orders of magnitude.

I have written a simple Fibonacci function as an exercise in C++ using Visual Studio to test Tail Recursion and to see how it works:

int fib_tail(int n, int res, int next)
{
  if (n == 0)
  {
    return res;
  }
  return fib_tail(n - 1, next, res + next);
}

The last thing fib_tail does is calling itself so it should have a tail call at the end. Let's look at the generated assembly to check. For this purpose I compiled the program in Release mode, which uses the optimization option /O2. The assembly looks like this:

Bingo. If you look close at the last line you'll see a JMP instruction instead of a CALL. In this case tail recursion is enabled, our Fibonacci function doesn't suffer from any stack overflow exception because at assembly level it is simply translated into an iterative function.

Not happy of this result, I wanted to do some performance tests by increasing the input variable n in my Fibonacci function. I then opted to change the variable type, used in the function, from int to unsigned long long. I ran the program again and surprisingly I got a stack overflow exception coming through!!! This is the new Fibonacci function:

typedef unsigned long long ULONG64;
ULONG64 fib_tail(ULONG64 n, ULONG64 res, ULONG64 next)
{
  if (n == 0)
  {
    return res;
  }
  return fib_tail(n - 1, next, res + next);
}

Let's have a look at the generated assembly again:

As I expected there is no tail recursion anymore! Now instead of the expected JMP there is a CALL. The only differences between those two functions is using a 64bit variable instead of a 32bit. Question is: "Why the compiler cannot enable tail recursion for 64 bit variable?"

I then decided to compile my program in 64bit to see if the same behavior would occur. This is the generated assembly:

And now surprisingly we have tail recursion enabled again!! With the aid of 64 bit registers (rax, r8, rcx, rdx) the caller and the callee share the same stack frame and the callee returns directly to the caller's caller.

I posted a question too on Stack Overflow and it seems it could be a problem with the Microsoft C++ compiler. One of the answer on Stack Overflow states that other C++ compilers like clang don't suffer from this problem but I have to test it.

I put the solution example on GitHub, you can clone it and try yourself. I was told on Reddit and Stack Overflow as well that VS2013 Community Edition does not have this issue. I tried with VS2013 Ultimate but I noticed the same exact problem. I will try in the next few days to test GCC and compare the result.

See the Example project on GitHub.

I hope this can save you time when and if you need to see why tail recursion might have not been correctly handled by the compiler.

See you soon!

This article was originally published at Coding Adventures Blog. Republished by the author permission.