diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-04-05 01:32:42 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-04-05 01:32:42 +0900 |
| commit | e8d6416b05d5dfd120dc6a1699f167a2d9d70884 (patch) | |
| tree | 09acf77e0f164ca705669499dbf295a0422e9e02 /src/WebAssembly/Execution/NumericOps.php | |
| parent | 5fd76d0bf2cf3128cc8e9f55364641f16e1aa82e (diff) | |
| download | php-waddiwasi-e8d6416b05d5dfd120dc6a1699f167a2d9d70884.tar.gz php-waddiwasi-e8d6416b05d5dfd120dc6a1699f167a2d9d70884.tar.zst php-waddiwasi-e8d6416b05d5dfd120dc6a1699f167a2d9d70884.zip | |
fix: conversion to f32
Diffstat (limited to 'src/WebAssembly/Execution/NumericOps.php')
| -rw-r--r-- | src/WebAssembly/Execution/NumericOps.php | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/WebAssembly/Execution/NumericOps.php b/src/WebAssembly/Execution/NumericOps.php index ee13aad..a24791f 100644 --- a/src/WebAssembly/Execution/NumericOps.php +++ b/src/WebAssembly/Execution/NumericOps.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Execution; +use FFI; use InvalidArgumentException; use RoundingMode; use function abs; @@ -70,13 +71,13 @@ final readonly class NumericOps public static function f32ConvertI64S(int $x): float { - return self::truncateF64ToF32((float) $x); + return self::castBitIntToCFloat((string)$x); } public static function f32ConvertI64U(int $x): float { $x = self::convertS64ToBigUInt($x); - return self::truncateF64ToF32((float) $x); + return self::castBitIntToCFloat($x); } public static function f32CopySign(float $x, float $y): float @@ -88,7 +89,7 @@ final readonly class NumericOps public static function f32DemoteF64(float $x): float { - return $x; + return self::truncateF64ToF32($x); } public static function f32Div(float $x, float $y): float @@ -1335,4 +1336,21 @@ final readonly class NumericOps $i & 0b00000000_00001111_11111111_11111111_11111111_11111111_11111111_11111111, ]; } + + private static function castBitIntToCFloat(string $x): float + { + // @phpstan-ignore-next-line + return self::ffi()->strtof($x, null); + } + + private static function ffi(): FFI + { + static $ffi; + if (!$ffi) { + $ffi = FFI::cdef( + 'float strtof(const char *restrict nptr, char **restrict endptr);', + ); + } + return $ffi; + } } |
