dl: cannot pass double value correctly on all x86_64 systems
ext/dl/test/test_dl2.rb doesn't fail on x86_64 linux. But it is by chance.
It fails just by changing as follows.
--- ext/dl/test/test_dl2.rb (revision 21182)
+++ ext/dl/test/test_dl2.rb (working copy)
@@ -34,8 +34,8 @@
def test_sin() cfunc = CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin')
- x = cfunc.call([3.14/2].pack("d").unpack("l!*"))
- assert_equal(x, Math.sin(3.14/2))
- x = cfunc.call([1.57].pack("d").unpack("l!*"))
cfunc = CFunc.new(@libm['sin'], TYPE_DOUBLE, 'sin')
x = cfunc.call([-3.14/2].pack("d").unpack("l!*"))
dl passes function arguments as long types. But on x86_64 linux, the
first double value is passed by XMM0 register and the first long
value is by RDI. sin() expects that the argument is passed by XMM0.
But dl passes it by RDI.
The test had passed because calculating 3.14/2 set XMM0 register and the
value had not been changed until cfunc.call was called. sin() got the
argument by XMM0 which happened to be 3.14/2.
I have tested this on x86_64 linux. But I guess it will fail on all x86_64 systems.
"Microsoft x64 calling convention" and "AMD64 ABI convention"
I guess it is extremely hard to fix this.
Updated by tenderlovemaking (Aaron Patterson) almost 10 years ago
- Status changed from Assigned to Closed
- ruby -v set to ruby 1.9.2dev (2010-02-03 trunk 26544) [x86_64-darwin10.2.0]
DL now uses libffi for function calling. Steer clear of using CFunc#call. Use Function#call instead, and you will use libffi which will deal with x86_64 systems.