Let's say I've got a C function:
unsigned int fact(unsigned int n, unsigned int acc) {
if(n > 0) return fact(n - 1, n * acc);
return acc;
}
Is there a way to tell GCC to tail call the recursive function, something like
unsigned int fact(unsigned int n, unsigned int acc) {
if(n > 0) return __tail_call(fact, n - 1, n * acc);
return acc;
}
I can make this happen with the -O2
flag passed to the compiler but I'd like the tail call behavior at lower optimization levels so I can compile more quickly.
The actual function is much more complicated, and I want to use TCO because this makes it explicit which state in an iteration can affect future iterations, and which state is reinitialized each iteration.
EDIT: This was flagged as a duplicate of this, but that thread a) doesn't have any answers for GCC, and b) isn't about placing something in the code to force TCO.