Skip to content

Commit

Permalink
Optimize addition and subtraction at comptime too
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Oct 30, 2023
1 parent 418b444 commit 4b1d417
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
1 change: 0 additions & 1 deletion lib/orb/i32/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ defmodule Orb.I32.DSL do

def left * right do
Orb.Numeric.Multiply.optimized(Orb.I32, left, right)
# Orb.I32.mul(left, right)
end

def _left == _right do
Expand Down
3 changes: 3 additions & 0 deletions lib/orb/numeric/add.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ defmodule Orb.Numeric.Add do
[MDN reference](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Addition)
"""

# Perform at comptime.
def optimized(_type, a, b) when is_integer(a) and is_integer(b), do: a + b
# Add anything by zero is that same thing.
def optimized(_type, a, 0), do: a
def optimized(_type, 0, b), do: b
# def optimized(type, a, b), do: {type.wasm_type(), :add, {a, b}}
Expand Down
2 changes: 1 addition & 1 deletion lib/orb/numeric/multiply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ defmodule Orb.Numeric.Multiply do
# Multiply anything by one is that same thing.
def optimized(_type, a, 1), do: a
def optimized(_type, 1, b), do: b
# Finally, spit out the instruction.
# Finally, spit out the runtime instruction.
def optimized(type, a, b), do: Orb.Instruction.new(type.wasm_type(), :mul, [a, b])
end
3 changes: 3 additions & 0 deletions lib/orb/numeric/sub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ defmodule Orb.Numeric.Subtract do
[MDN reference](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Subtraction)
"""

# Perform at comptime.
def optimized(_type, a, b) when is_integer(a) and is_integer(b), do: a - b
# Minus zero from anything is that same thing.
def optimized(_type, a, 0), do: a
def optimized(type, a, b), do: Orb.Instruction.new(type.wasm_type(), :sub, [a, b])
end
8 changes: 8 additions & 0 deletions test/examples/arena.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ defmodule Examples.Arena do

Orb.snippet Orb.S32, new_ptr: I32.UnsafePointer do
new_ptr = Instruction.global_get(Orb.I32, offset_global_name)




if new_ptr + byte_count > end_offset * Orb.Memory.page_byte_size() do
unreachable!()
end





Instruction.global_set(Orb.I32, offset_global_name, new_ptr + byte_count)

new_ptr
Expand Down
26 changes: 20 additions & 6 deletions test/operators_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ defmodule OperatorsTest do
use Orb

wasm do
func add(), I32 do
func add(a: I32) do
1 + 2
:drop

a + 2
:drop
end

func multiply(a: I32, b: I32) do
Expand Down Expand Up @@ -48,8 +52,11 @@ defmodule OperatorsTest do

assert to_wat(D1) == """
(module $D1
(func $add (export "add") (result i32)
(i32.add (i32.const 1) (i32.const 2))
(func $add (export "add") (param $a i32)
(i32.const 3)
drop
(i32.add (local.get $a) (i32.const 2))
drop
)
(func $multiply (export "multiply") (param $a i32) (param $b i32)
(i32.mul (local.get $a) (local.get $b))
Expand Down Expand Up @@ -83,8 +90,12 @@ defmodule OperatorsTest do
use Orb

wasm U32 do
func add(), I32 do
func add(a: I32) do
1 + 2
:drop

a + 2
:drop
end

func multiply(a: I32, b: I32) do
Expand Down Expand Up @@ -118,8 +129,11 @@ defmodule OperatorsTest do

assert to_wat(U1) == """
(module $U1
(func $add (export "add") (result i32)
(i32.add (i32.const 1) (i32.const 2))
(func $add (export "add") (param $a i32)
(i32.const 3)
drop
(i32.add (local.get $a) (i32.const 2))
drop
)
(func $multiply (export "multiply") (param $a i32) (param $b i32)
(i32.mul (local.get $a) (local.get $b))
Expand Down
2 changes: 1 addition & 1 deletion test/orb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ defmodule OrbTest do
(module $SnippetUser
(memory (export "memory") 1)
(func $answer (export "answer") (result i32)
(i32.load (i32.add (i32.const #{0x100}) (i32.const 4)))
(i32.load (i32.const #{0x104}))
)
)
"""
Expand Down

0 comments on commit 4b1d417

Please sign in to comment.