Passing array of float from Squeak to C

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing array of float from Squeak to C

Ang BeePeng
I am writing a plugin. How do I pass
starray := {1.0.  4.5555555. 3.0001.}.
to
double * carray.

Thanks.

Ang Beepeng
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Passing array of float from Squeak to C

Joshua Gargus-2
You would pass in a ByteArray.  You can populate the ByteArray like this:

ba := ByteArray new: 8 * 3.  "3 is number of elements"
useBigEndian := true.  "or false, depending on what kind of machine you
are on"
ba doubleAt: 1 put: 1.0 bigEndian: useBigEndian.
ba doubleAt: 9 put: 4.55555555 bigEndian: useBigEndian.
ba doubleAt: 17 put: 3.0001 bigEndian: useBigEndian.
someObj primitiveDoSomethingWithDoubles: ba


Your primitive would need to get a pointer to the actual data in the
ByteArray (see senders of #firstIndexableField:), and cast it to (double*).

Hope this helps,
Josh



Ang Beepeng wrote:
> I am writing a plugin. How do I pass
> starray := {1.0.  4.5555555. 3.0001.}.
> to
> double * carray.
>
> Thanks.
>
> Ang Beepeng
>  


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Passing array of float from Squeak to C

Ang BeePeng
Thanks for the help. The plugin works fine now.

Here's another problem I'm facing. How can I get value in double from ByteArray for the interpreter simulation? ByteArray is referred by CArrayAccessor.

Here's one thing I learned. We can use
useBigEndian := SmalltalkImage current isBigEndian.
instead of setting it manually.

Thank you so much.

Ang Beepeng


Joshua Gargus-2 wrote
You would pass in a ByteArray.  You can populate the ByteArray like this:

ba := ByteArray new: 8 * 3.  "3 is number of elements"
useBigEndian := true.  "or false, depending on what kind of machine you
are on"
ba doubleAt: 1 put: 1.0 bigEndian: useBigEndian.
ba doubleAt: 9 put: 4.55555555 bigEndian: useBigEndian.
ba doubleAt: 17 put: 3.0001 bigEndian: useBigEndian.
someObj primitiveDoSomethingWithDoubles: ba


Your primitive would need to get a pointer to the actual data in the
ByteArray (see senders of #firstIndexableField:), and cast it to (double*).

Hope this helps,
Josh