php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Allow CData to be passed as valid argument to primitive ffi functions

Open the-eater opened this issue 4 years ago • 2 comments

Description

Currently one can only pass a float (C) from the PHP's float, which is a double.

When using a lot of floats with C libraries, this means that float's are constantly converted from float's to double' and double's to floats.

I hereby would like to request the option to send the CData class to FFI bound functions, to bypass this type juggling

I guess this would also mean that there should be an option to get the return as the "raw" CData value, which I have no idea how to fully implement from a user standpoint ($ffi->atan->raw(0.5) or something?)

A simple code example (that has no bearing on reality) that displays the "issue" would be the following:

<?php

$c = FFI::cdef('float atanf (float x);', 'libm.so.6');
$x = $c->atanf(0.4);
$z = FFI::new('float');
$z->cdata = $x;
$y = $c->atanf($z);
echo $y.$x."\n";

php will now simply throw a warning "PHP Warning: Object of class FFI\CData could not be converted to float", and use 1.0 instead

the-eater avatar Dec 19 '21 17:12 the-eater

I hereby would like to request the option to send the CData class to FFI bound functions, to bypass this type juggling

But why? Regarding the performance, that float <-> double casting is likely way more efficient than constructing new CData objects. And as far as I know, a float to double conversion is lossless (assuming IEEE 754 binary32/binary64).

In my opinion, that would only make sense, if PHP had a native float type.

cmb69 avatar Dec 20 '21 09:12 cmb69

No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

github-actions[bot] avatar Jan 04 '22 00:01 github-actions[bot]