1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-08 10:50:21 +02:00

Implement a correct generation of Fibonacci numbers.

* doc/body.texi: Change documentation to no longer say
	it is a variant of the Fibonacci sequence, and document
	a proper implementation.
	Thanks to Jon Arintok for pointing out that the Fibonacci
	sequence generation was incorrect. It was documented, but
	still confusing.

	* check/fib.tst, check/fib.ok, check/bp.tst, check/bp.ok,
	doc/ifib.c, doc/rbif.c: Implement a proper Fibonacci
	sequence implementation.
This commit is contained in:
Paulo Andrade 2015-11-30 15:32:48 -02:00
parent fd57359498
commit 76876dd7bf
9 changed files with 80 additions and 65 deletions

View file

@ -10,6 +10,7 @@ int main(int argc, char *argv[])
pifi fib;
jit_node_t *in; /* offset of the argument */
jit_node_t *ref; /* to patch the forward reference */
jit_node_t *zero; /* to patch the forward reference */
jit_node_t *jump; /* jump to start of loop */
jit_node_t *loop; /* start of the loop */
@ -18,22 +19,24 @@ int main(int argc, char *argv[])
jit_prolog ();
in = jit_arg ();
jit_getarg (JIT_R2, in); /* R2 = n */
jit_movi (JIT_R1, 1);
ref = jit_blti (JIT_R2, 2);
jit_subi (JIT_R2, JIT_R2, 1);
jit_getarg (JIT_R0, in); /* R0 = n */
zero = jit_beqi (JIT_R0, 0);
jit_movr (JIT_R1, JIT_R0);
jit_movi (JIT_R0, 1);
ref = jit_blei (JIT_R1, 2);
jit_subi (JIT_R2, JIT_R1, 2);
jit_movr (JIT_R1, JIT_R0);
loop= jit_label();
jit_subi (JIT_R2, JIT_R2, 1); /* decr. counter */
jit_addr (JIT_V0, JIT_R0, JIT_R1); /* V0 = R0 + R1 */
jit_movr (JIT_R0, JIT_R1); /* R0 = R1 */
jit_addi (JIT_R1, JIT_V0, 1); /* R1 = V0 + 1 */
jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */
jit_addr (JIT_R0, JIT_R0, JIT_R1); /* R0 = R0 + R1 */
jit_movr (JIT_R1, JIT_V0); /* R1 = V0 */
jump= jit_bnei (JIT_R2, 0); /* if (R2) goto loop; */
jit_patch_at(jump, loop);
jit_patch(ref); /* patch forward jump */
jit_movr (JIT_R0, JIT_R1); /* R0 = R1 */
jit_patch(zero); /* patch forward jump */
jit_retr (JIT_R0);
/* call the generated code, passing 36 as an argument */