From 28b496687c0eb36a035a8686794fa45434957e28 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 7 Apr 2025 00:26:55 +0900 Subject: feat: do not use FFI to cast integers to f32 --- src/WebAssembly/Execution/NumericOps.php | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) (limited to 'src/WebAssembly') diff --git a/src/WebAssembly/Execution/NumericOps.php b/src/WebAssembly/Execution/NumericOps.php index f7a1e89..7d5e57c 100644 --- a/src/WebAssembly/Execution/NumericOps.php +++ b/src/WebAssembly/Execution/NumericOps.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Nsfisis\Waddiwasi\WebAssembly\Execution; -use FFI; use Nsfisis\Waddiwasi\BitOps\BinaryConversion; use Nsfisis\Waddiwasi\BitOps\FloatOps; use RoundingMode; @@ -70,13 +69,13 @@ final readonly class NumericOps public static function f32ConvertI64S(int $x): float { - return self::castBigIntToF32((string)$x); + return BinaryConversion::convertBigIntToF32((string)$x); } public static function f32ConvertI64U(int $x): float { $x = self::convertS64ToBigUInt($x); - return self::castBigIntToF32($x); + return BinaryConversion::convertBigIntToF32($x); } public static function f32CopySign(float $x, float $y): float @@ -1165,6 +1164,9 @@ final readonly class NumericOps } } + /** + * @return numeric-string + */ public static function convertS64ToBigUInt(int $x): string { if ($x < 0) { @@ -1190,22 +1192,4 @@ final readonly class NumericOps } return (int)$result; } - - - private static function castBigIntToF32(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; - } } -- cgit v1.2.3-70-g09d2