From e5ddbd619b1f85a3f67fe8e09c48b4761d9fe177 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 29 Jun 2024 17:25:26 +0900 Subject: fix: some float ops --- src/Execution/Runtime.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/Execution') 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)); } -- cgit v1.2.3-70-g09d2