Skip to content

Commit

Permalink
Merge pull request #513 from wasmx/simple_ops
Browse files Browse the repository at this point in the history
Use simpler ops for binary instructions (fixes GCC issue)
  • Loading branch information
chfast authored Sep 2, 2020
2 parents f42d4dc + 0e905cb commit eacb0ed
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,36 @@ inline void comparison_op(OperandStack& stack, Op<T> op) noexcept
stack.top() = uint32_t{op(val1, val2)};
}

template <typename T>
inline constexpr T add(T a, T b) noexcept
{
return a + b;
}

template <typename T>
inline constexpr T sub(T a, T b) noexcept
{
return a - b;
}

template <typename T>
inline constexpr T mul(T a, T b) noexcept
{
return a * b;
}

template <typename T>
inline constexpr T div(T a, T b) noexcept
{
return a / b;
}

template <typename T>
inline constexpr T rem(T a, T b) noexcept
{
return a % b;
}

template <typename T>
inline T shift_left(T lhs, T rhs) noexcept
{
Expand Down Expand Up @@ -1448,17 +1478,17 @@ ExecutionResult execute(
}
case Instr::i32_add:
{
binary_op(stack, std::plus<uint32_t>());
binary_op(stack, add<uint32_t>);
break;
}
case Instr::i32_sub:
{
binary_op(stack, std::minus<uint32_t>());
binary_op(stack, sub<uint32_t>);
break;
}
case Instr::i32_mul:
{
binary_op(stack, std::multiplies<uint32_t>());
binary_op(stack, mul<uint32_t>);
break;
}
case Instr::i32_div_s:
Expand All @@ -1470,7 +1500,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::divides<int32_t>());
binary_op(stack, div<int32_t>);
break;
}
case Instr::i32_div_u:
Expand All @@ -1481,7 +1511,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::divides<uint32_t>());
binary_op(stack, div<uint32_t>);
break;
}
case Instr::i32_rem_s:
Expand All @@ -1499,7 +1529,7 @@ ExecutionResult execute(
stack.top() = 0;
}
else
binary_op(stack, std::modulus<int32_t>());
binary_op(stack, rem<int32_t>);
break;
}
case Instr::i32_rem_u:
Expand All @@ -1510,7 +1540,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::modulus<uint32_t>());
binary_op(stack, rem<uint32_t>);
break;
}
case Instr::i32_and:
Expand Down Expand Up @@ -1571,17 +1601,17 @@ ExecutionResult execute(
}
case Instr::i64_add:
{
binary_op(stack, std::plus<uint64_t>());
binary_op(stack, add<uint64_t>);
break;
}
case Instr::i64_sub:
{
binary_op(stack, std::minus<uint64_t>());
binary_op(stack, sub<uint64_t>);
break;
}
case Instr::i64_mul:
{
binary_op(stack, std::multiplies<uint64_t>());
binary_op(stack, mul<uint64_t>);
break;
}
case Instr::i64_div_s:
Expand All @@ -1593,7 +1623,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::divides<int64_t>());
binary_op(stack, div<int64_t>);
break;
}
case Instr::i64_div_u:
Expand All @@ -1604,7 +1634,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::divides<uint64_t>());
binary_op(stack, div<uint64_t>);
break;
}
case Instr::i64_rem_s:
Expand All @@ -1622,7 +1652,7 @@ ExecutionResult execute(
stack.top() = 0;
}
else
binary_op(stack, std::modulus<int64_t>());
binary_op(stack, rem<int64_t>);
break;
}
case Instr::i64_rem_u:
Expand All @@ -1633,7 +1663,7 @@ ExecutionResult execute(
trap = true;
goto end;
}
binary_op(stack, std::modulus<uint64_t>());
binary_op(stack, rem<uint64_t>);
break;
}
case Instr::i64_and:
Expand Down Expand Up @@ -1716,17 +1746,17 @@ ExecutionResult execute(

case Instr::f32_add:
{
binary_op(stack, std::plus<float>{});
binary_op(stack, add<float>);
break;
}
case Instr::f32_sub:
{
binary_op(stack, std::minus<float>{});
binary_op(stack, sub<float>);
break;
}
case Instr::f32_mul:
{
binary_op(stack, std::multiplies<float>{});
binary_op(stack, mul<float>);
break;
}
case Instr::f32_div:
Expand Down Expand Up @@ -1794,17 +1824,17 @@ ExecutionResult execute(

case Instr::f64_add:
{
binary_op(stack, std::plus<double>{});
binary_op(stack, add<double>);
break;
}
case Instr::f64_sub:
{
binary_op(stack, std::minus<double>{});
binary_op(stack, sub<double>);
break;
}
case Instr::f64_mul:
{
binary_op(stack, std::multiplies<double>{});
binary_op(stack, mul<double>);
break;
}
case Instr::f64_div:
Expand Down

0 comments on commit eacb0ed

Please sign in to comment.