aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-06-29 17:25:26 +0900
committernsfisis <nsfisis@gmail.com>2024-06-29 17:25:26 +0900
commite5ddbd619b1f85a3f67fe8e09c48b4761d9fe177 (patch)
tree420cf1d5050dc3632f1d7f8835af596860a0af6a
parent80b9584de4b9c2b0601c709877b50d589aece57d (diff)
downloadphp-waddiwasi-e5ddbd619b1f85a3f67fe8e09c48b4761d9fe177.tar.gz
php-waddiwasi-e5ddbd619b1f85a3f67fe8e09c48b4761d9fe177.tar.zst
php-waddiwasi-e5ddbd619b1f85a3f67fe8e09c48b4761d9fe177.zip
fix: some float ops
-rw-r--r--src/Execution/Runtime.php24
-rw-r--r--tests/src/SpecTestsuites/SpecTestsuiteBase.php12
2 files changed, 33 insertions, 3 deletions
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php
index 3c8592e..4d74b9a 100644
--- a/src/Execution/Runtime.php
+++ b/src/Execution/Runtime.php
@@ -27,9 +27,11 @@ use function array_reverse;
use function assert;
use function ceil;
use function count;
+use function fdiv;
use function floor;
use function intdiv;
use function is_int;
+use function is_nan;
use function max;
use function min;
use function pack;
@@ -658,6 +660,11 @@ final class Runtime
{
$c2 = $this->stack->popFloat();
$c1 = $this->stack->popFloat();
+ if (is_nan($c1) || is_nan($c2)) {
+ // PHP's standard max() handles NaNs in diffrent way than WebAssembly spec does.
+ $this->stack->pushValue(NAN);
+ return;
+ }
$this->stack->pushValue(max($c1, $c2));
}
@@ -665,6 +672,11 @@ final class Runtime
{
$c2 = $this->stack->popFloat();
$c1 = $this->stack->popFloat();
+ if (is_nan($c1) || is_nan($c2)) {
+ // PHP's standard min() handles NaNs in diffrent way than WebAssembly spec does.
+ $this->stack->pushValue(NAN);
+ return;
+ }
$this->stack->pushValue(min($c1, $c2));
}
@@ -784,7 +796,7 @@ final class Runtime
{
$c2 = $this->stack->popFloat();
$c1 = $this->stack->popFloat();
- $this->stack->pushValue($c1 / $c2);
+ $this->stack->pushValue(fdiv($c1, $c2));
}
private function execInstrNumericF64Eq(Instrs\Numeric\F64Eq $instr): void
@@ -832,6 +844,11 @@ final class Runtime
{
$c2 = $this->stack->popFloat();
$c1 = $this->stack->popFloat();
+ if (is_nan($c1) || is_nan($c2)) {
+ // PHP's standard max() handles NaNs in diffrent way than WebAssembly spec does.
+ $this->stack->pushValue(NAN);
+ return;
+ }
$this->stack->pushValue(max($c1, $c2));
}
@@ -839,6 +856,11 @@ final class Runtime
{
$c2 = $this->stack->popFloat();
$c1 = $this->stack->popFloat();
+ if (is_nan($c1) || is_nan($c2)) {
+ // PHP's standard min() handles NaNs in diffrent way than WebAssembly spec does.
+ $this->stack->pushValue(NAN);
+ return;
+ }
$this->stack->pushValue(min($c1, $c2));
}
diff --git a/tests/src/SpecTestsuites/SpecTestsuiteBase.php b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
index 1e74e13..ee9c65d 100644
--- a/tests/src/SpecTestsuites/SpecTestsuiteBase.php
+++ b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
@@ -159,8 +159,16 @@ abstract class SpecTestsuiteBase extends TestCase
return match ($type) {
'i32' => unpack('l', pack('V', (int)$value))[1],
'i64' => unpack('q', self::convertInt64ToBinary($value))[1],
- 'f32' => unpack('g', pack('V', (int)$value))[1],
- 'f64' => unpack('e', self::convertInt64ToBinary($value))[1],
+ 'f32' => match ($value) {
+ 'nan:canonical' => NAN,
+ 'nan:arithmetic' => NAN,
+ default => unpack('g', pack('V', (int)$value))[1],
+ },
+ 'f64' => match ($value) {
+ 'nan:canonical' => NAN,
+ 'nan:arithmetic' => NAN,
+ default => unpack('e', self::convertInt64ToBinary($value))[1],
+ },
'externref' => $value === 'null' ? Ref::RefNull(RefType::ExternRef) : Ref::RefExtern((int)$value),
'funcref' => $value === 'null' ? Ref::RefNull(RefType::FuncRef) : Ref::RefFunc((int)$value),
default => throw new RuntimeException("unknown type: $type"),