Skip to content

Instantly share code, notes, and snippets.

@oscherler
Created February 23, 2017 20:22
Show Gist options
  • Save oscherler/fbe195120ac5a4962146ade32e89b82f to your computer and use it in GitHub Desktop.
Save oscherler/fbe195120ac5a4962146ade32e89b82f to your computer and use it in GitHub Desktop.
Different versions of fib in Erlang
-module( adjacent ).
-export( [ fib/1 ] ).
fibP( 0 ) ->
{ 0, 1 };
fibP( N ) ->
{ P, C } = fibP( N - 1 ),
{ C, P + C }.
fib( N ) ->
{ P, _ } = fibP( N ),
P.
{function, fibP, 1, 2}.
{label,1}.
{line,[{location,"adjacent.erl",4}]}.
{func_info,{atom,adjacent},{atom,fibP},1}.
{label,2}.
{test,is_eq_exact,{f,3},[{x,0},{integer,0}]}.
{move,{literal,{0,1}},{x,0}}.
return.
{label,3}.
{allocate,0,1}.
{line,[{location,"adjacent.erl",7}]}.
{gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,0}}.
{line,[{location,"adjacent.erl",7}]}.
{call,1,{f,2}}.
{test,is_tuple,{f,4},[{x,0}]}.
{test,test_arity,{f,4},[{x,0},2]}.
{get_tuple_element,{x,0},0,{x,1}}.
{get_tuple_element,{x,0},1,{x,2}}.
{line,[{location,"adjacent.erl",8}]}.
{gc_bif,'+',{f,0},3,[{x,1},{x,2}],{x,0}}.
{test_heap,3,3}.
{put_tuple,2,{x,1}}.
{put,{x,2}}.
{put,{x,0}}.
{move,{x,1},{x,0}}.
{deallocate,0}.
return.
{label,4}.
{line,[{location,"adjacent.erl",7}]}.
{badmatch,{x,0}}.
{function, fib, 1, 6}.
{label,5}.
{line,[{location,"adjacent.erl",10}]}.
{func_info,{atom,adjacent},{atom,fib},1}.
{label,6}.
{allocate,0,1}.
{line,[{location,"adjacent.erl",11}]}.
{call,1,{f,2}}.
{test,is_tuple,{f,7},[{x,0}]}.
{test,test_arity,{f,7},[{x,0},2]}.
{get_tuple_element,{x,0},0,{x,0}}.
{deallocate,0}.
return.
{label,7}.
{line,[{location,"adjacent.erl",11}]}.
{badmatch,{x,0}}.
-module( direct ).
-export( [ fib/1 ] ).
fib( 0 ) ->
0;
fib( 1 ) ->
1;
fib( N ) ->
fib( N - 1 ) + fib( N - 2 ).
{function, fib, 1, 2}.
{label,1}.
{line,[{location,"direct.erl",4}]}.
{func_info,{atom,direct},{atom,fib},1}.
{label,2}.
{test,is_integer,{f,4},[{x,0}]}.
{select_val,{x,0},{f,4},{list,[{integer,1},{f,3},{integer,0},{f,3}]}}.
{label,3}.
return.
{label,4}.
{allocate_zero,1,1}.
{line,[{location,"direct.erl",9}]}.
{gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,1}}.
{move,{x,0},{y,0}}.
{move,{x,1},{x,0}}.
{line,[{location,"direct.erl",9}]}.
{call,1,{f,2}}.
{line,[{location,"direct.erl",9}]}.
{gc_bif,'-',{f,0},1,[{y,0},{integer,2}],{x,1}}.
{move,{x,0},{y,0}}.
{move,{x,1},{x,0}}.
{line,[{location,"direct.erl",9}]}.
{call,1,{f,2}}.
{line,[{location,"direct.erl",9}]}.
{gc_bif,'+',{f,0},1,[{y,0},{x,0}],{x,0}}.
{deallocate,1}.
return.
-module( tail ).
-export( [ fib/1 ] ).
fib( 0, P, _C ) ->
P;
fib( N, P, C ) ->
fib( N - 1, C, P + C ).
fib( N ) ->
fib( N, 0, 1 ).
{function, fib, 3, 2}.
{label,1}.
{line,[{location,"tail.erl",4}]}.
{func_info,{atom,tail},{atom,fib},3}.
{label,2}.
{test,is_eq_exact,{f,3},[{x,0},{integer,0}]}.
{move,{x,1},{x,0}}.
return.
{label,3}.
{line,[{location,"tail.erl",7}]}.
{gc_bif,'-',{f,0},3,[{x,0},{integer,1}],{x,0}}.
{line,[{location,"tail.erl",7}]}.
{gc_bif,'+',{f,0},3,[{x,1},{x,2}],{x,1}}.
{move,{x,2},{x,3}}.
{move,{x,1},{x,2}}.
{move,{x,3},{x,1}}.
{call_only,3,{f,2}}.
{function, fib, 1, 5}.
{label,4}.
{line,[{location,"tail.erl",9}]}.
{func_info,{atom,tail},{atom,fib},1}.
{label,5}.
{move,{integer,1},{x,2}}.
{move,{integer,0},{x,1}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment