Last active
March 26, 2020 05:58
-
-
Save aqjune/c6ff3f3ff618d30f8fc5acecd1873b8b to your computer and use it in GitHub Desktop.
Practice session 2 - fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned fib(unsigned n) { | |
unsigned answ; | |
if (n <= 1) | |
answ = n; | |
else | |
answ = fib(n - 1) + fib(n - 2); | |
return answ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define i32 @fib(i32 %n) nounwind { | |
entry: | |
%cmp = icmp ult i32 %n, 2 | |
br i1 %cmp, label %if.end, label %if.else | |
if.else: | |
%sub = add i32 %n, -1 | |
%call = call i32 @fib(i32 %sub) | |
%sub1 = add i32 %n, -2 | |
%call2 = call i32 @fib(i32 %sub1) | |
%add = add i32 %call2, %call | |
br label %if.end | |
if.end: | |
%answ.0 = phi i32 [ %add, %if.else ], [ %n, %entry ] | |
ret i32 %answ.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment