Skip to content

Instantly share code, notes, and snippets.

@s5bug
Last active July 11, 2020 01:59
Show Gist options
  • Save s5bug/4e13632c45591871b153f4f4679b58bd to your computer and use it in GitHub Desktop.
Save s5bug/4e13632c45591871b153f4f4679b58bd to your computer and use it in GitHub Desktop.
C vs LLVM vs MCFASM
int gcd(int x, int y) {
if(x == y) {
return x;
} else if(x < y) {
return gcd(x, y - x);
} else {
return gcd(x - y, y);
}
}
define i32 @gcd(i32 %x, i32 %y) {
entry:
%tmp3 = icmp eq i32 %x, %y
br i1 %tmp3, label %return, label %cond_false
return:
ret i32 %x
cond_false:
%tmp9 = icmp ult i32 %x, %y
br i1 %tmp9, label %cond_true12, label %cond_false19
cond_true12:
%tmp15 = sub i32 %y, %x
%tmp17 = call i32 @gcd(i32 %x, i32 %tmp15)
ret i32 %tmp17
cond_false19:
%tmp22 = sub i32 %x, %y
%tmp24 = call i32 @gcd(i32 %tmp22, i32 %y)
ret i32 %tmp24
}
# gcd_entry.mcfunction
execute store result score y llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score x llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute unless score x llmcf_scope = y llmcf_scope run scoreboard players set tmp3 llmcf_scope 0
execute if score x llmcf_scope = y llmcf_scope run scoreboard players set tmp3 llmcf_scope 1
execute if score tmp3 llmcf_scope matches 0 run function llmcf:gcd_cond_false
execute if score tmp3 llmcf_scope matches 1 run function llmcf:gcd_return
# gcd_return.mcfunction
execute store result storage llmcf register int 1 run scoreboard players get x llmcf_scope
data modify storage llmcf stack append from storage llmcf register
# gcd_cond_false.mcfunction
execute unless score x llmcf_scope < y llmcf_scope run scoreboard players set tmp9 llmcf_scope 0
execute if score x llmcf_scope < y llmcf_scope run scoreboard players set tmp9 llmcf_scope 1
execute if score tmp9 llmcf_scope matches 0 run function llmcf:gcd_cond_false19
execute if score tmp9 llmcf_scope matches 1 run function llmcf:gcd_cond_true12
# gcd_cond_true12.mcfunction
scoreboard players operation tmp15 llmcf_scope = y llmcf_scope
scoreboard players operation tmp15 llmcf_scope -= x llmcf_scope
execute store result storage llmcf register int 1 run scoreboard players get tmp3 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get tmp9 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get x llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get tmp15 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
function llmcf:gcd_entry
execute store result score tmp17 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score tmp9 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score tmp3 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result storage llmcf register int 1 run scoreboard players get tmp17 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
# gcd_cond_false19.mcfunction
scoreboard players operation tmp22 llmcf_scope = x llmcf_scope
scoreboard players operation tmp22 llmcf_scope -= y llmcf_scope
execute store result storage llmcf register int 1 run scoreboard players get tmp3 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get tmp9 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get tmp22 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
execute store result storage llmcf register int 1 run scoreboard players get y llmcf_scope
data modify storage llmcf stack append from storage llmcf register
function llmcf:gcd_entry
execute store result score tmp24 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score tmp9 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score tmp3 llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result storage llmcf register int 1 run scoreboard players get tmp24 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
int mul_add(int x, int y, int z) {
return x * y + z;
}
define i32 @mul_add(i32 %x, i32 %y, i32 %z) {
entry:
%tmp = mul i32 %x, %y
%tmp2 = add i32 %tmp, %z
ret i32 %tmp2
}
execute store result score z llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score y llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
execute store result score x llmcf_scope run data get storage llmcf stack[-1]
data remove storage llmcf stack[-1]
scoreboard players operation tmp llmcf_scope = x llmcf_scope
scoreboard players operation tmp llmcf_scope *= y llmcf_scope
scoreboard players operation tmp2 llmcf_scope = tmp llmcf_scope
scoreboard players operation tmp2 llmcf_scope += z llmcf_scope
execute store result storage llmcf register int 1 run scoreboard players get tmp2 llmcf_scope
data modify storage llmcf stack append from storage llmcf register
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment