simplify _loop so that the "prologe" is shorter and the jump is back into

the first < 5 bytes
This commit is contained in:
2018-01-03 21:35:00 +01:00
parent 8989abafd2
commit 2048bdb6e1
10 changed files with 25 additions and 29 deletions

View File

@@ -6,16 +6,13 @@ use64
public _loop
_loop:
xor eax, eax
inc eax
mov rbx, rdx ; RDX is overwritten by mul
@again:
cmp rbx, 0
je @loop_end
mov rax, rcx
@loop_loop:
mul rcx
dec rbx
jmp @again
@loop_end:
nop
nop
nop
loop @loop_loop ; lol
ret
public _tail_recursion

View File

@@ -1,12 +1,11 @@
#pragma once
extern "C" {
/**
* Raises @num @cnt times
* $$ x*x * (x-1)! $$
*
* @param num
* @param cnt
*/
uint32_t _loop(uint32_t num, uint32_t cnt);
uint32_t _loop(uint32_t x);
/**
* Computes factorial

View File

@@ -32,10 +32,10 @@ TEST_CASE("Advanced instruction functions work as expected, unhokked") {
}
TEST_CASE("Loops & tail recursion work as expected, unhook") {
REQUIRE(_loop(2, 3) == 8);
REQUIRE(_loop(5, 3) == 125);
REQUIRE(_loop(5, 0) == 1);
REQUIRE(_loop(5, 1) == 5);
REQUIRE(_loop(1) == 1);
REQUIRE(_loop(2) == 4);
REQUIRE(_loop(3) == 18);
REQUIRE(_loop(5) == 25*24);
REQUIRE(_tail_recursion(0) == 1);
REQUIRE(_tail_recursion(1) == 1);