64-bit integers in Ruby/DL

J

Jamis Buck

Okay, you C gurus out there. Here's a stumper.

Is there any way, without modifying Ruby/DL itself, to specify
a function that returns a 64-bit integer using Ruby/DL? And to pass
a 64-bit integer to a function?

Consider:

require 'dl/import'
module Test64
dlload "foo.so"
extern "long long int get_64bit_value()"
extern "void set_64bit_value(long long int)"
end

The above results in an error, currently, because Ruby/DL does not
support 64-bit integers as immediate values.

I thought of using a double (which is 64 bits, on my platform anyway):

require 'dl/import'
module Test64
dlload "foo.so"
extern "double get_64bit_value()"
extern "void set_64bit_value(double)"
end

result = Test64.get_64bit_value
p [result].pack("D").unpack("LL")

But the value that comes back is nothing like what the function itself
is really returning...

Any clever tricks I can try? Anyone? Please?

- Jamis
 
F

Florian Gross

Jamis said:
Is there any way, without modifying Ruby/DL itself, to specify
a function that returns a 64-bit integer using Ruby/DL? And to pass
a 64-bit integer to a function?

As far as I know Ruby/DL does not yet work properly on 64 bit platforms.
The author plans to fix that, but didn't have the time to do it when I
last contacted him.

I think this would involve changes in lots of places because Ruby/DL
seems to be using one-byte type tokens and .pack() internally. However
if you want to use the native bit-width of the system you will need to
use "L_" instead of "L" and so on in the pack arguments. So I think this
would involve changing the semantics in quite a few code locations...
 
J

Jamis Buck

As far as I know Ruby/DL does not yet work properly on 64 bit platforms.
The author plans to fix that, but didn't have the time to do it when I
last contacted him.

I think this would involve changes in lots of places because Ruby/DL
seems to be using one-byte type tokens and .pack() internally. However
if you want to use the native bit-width of the system you will need to
use "L_" instead of "L" and so on in the pack arguments. So I think this
would involve changing the semantics in quite a few code locations...

Well, I'm actually on a 32-bit system. But gcc supports a 64-bit
integer (long long), as does MSVC++.

It's not critical, but it would certainly be nice. For now, I'm just
using unsigned longs, which will lose the most significant 32 bits of
each return value. :(

- Jamis
 
D

DaZoner

I don't know very much about Ruby/DL but can't you just define a struct that
has two 32-bit numbers in it, and manipulate and pass those?
 
J

Jamis Buck

I don't know very much about Ruby/DL but can't you just define a struct that
has two 32-bit numbers in it, and manipulate and pass those?

Well, structs in Ruby/DL are implemented as pointers to structs. You
can't create a "bare" structure in Ruby/DL (to my knowledge).

- Jamis
Jamis Buck said:
Okay, you C gurus out there. Here's a stumper.

Is there any way, without modifying Ruby/DL itself, to specify
a function that returns a 64-bit integer using Ruby/DL? And to pass
a 64-bit integer to a function?

Consider:

require 'dl/import'
module Test64
dlload "foo.so"
extern "long long int get_64bit_value()"
extern "void set_64bit_value(long long int)"
end

The above results in an error, currently, because Ruby/DL does not
support 64-bit integers as immediate values.

I thought of using a double (which is 64 bits, on my platform anyway):

require 'dl/import'
module Test64
dlload "foo.so"
extern "double get_64bit_value()"
extern "void set_64bit_value(double)"
end

result = Test64.get_64bit_value
p [result].pack("D").unpack("LL")

But the value that comes back is nothing like what the function itself
is really returning...

Any clever tricks I can try? Anyone? Please?

- Jamis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top