Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

supporting SSE 128 bit data in Perl

by bulk88 (Priest)
on May 31, 2012 at 21:45 UTC ( [id://973618]=perlquestion: print w/replies, xml ) Need Help??

bulk88 has asked for the wisdom of the Perl Monks concerning the following question:

This is related to supporting quads on 32 bit Perl. I believe that in all perls, the highest numeric data unit is a 8 byte double. I have a C library that takes and returns 128 bit SSE2 data. The library looks at the 128 bit value as a union of various things, sometimes its an array of 4 floats, an array of 16 chars, an array of 2 doubles, array of 2 64 bit integers or 1 128bit integer or 1 128 bit floating point, depending on a bitfield passed to the function or depending on the function's name. All the values are little endian. I use perl on windows. What should my XS wrapper around this C library expose these 16 byte SSE2 variables as? There seems to be alot negativity over Math::BigInt's bloatedness. Also how to do you load packed little endian numbers into Math::BigInt, it seems bigint only knows what big endian ASCII hex is for importing numbers. The solution should be CPAN grade. The SSE2 variables are very rarely used by this C library but they need to be exposed somehow for the rare case someone wants to call that function. The easiest thing is just have the user supply a 16 byte string and they get a 16 byte string in return. They have to pack and unpack the union array themselves. Is this the best solution? I think the most difficult solution to make but easiest to use is a tied hash that takes and returns BigInts. I couldn't find anything on CPAN that already does SSE or SSE2.

edit: I have SSE2 not SSE data

edit: Instead of a 16 byte string (primative), or a tied hash (complicated), I thought of the following,
#load it as 4 floats, a little bit of precision is lost $obj = Local::SSE->new_fp(1.0, 2.0, 3.0, 4.0); #load it as 2 doubles $obj = Local::SSE->new_fp(1.0, 2.0); #load it as 4 32 bit ints $obj = Local::SSE->new_int(1, 2, 3, 4); #load it as 2 64 bit ints, quad support perl required $obj = Local::SSE->new_int(1, 2); #load it raw $obj = Local::SSE->new_raw("\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x +00\x00\x00\x00\x00\x01"); #read as 4 shorts @array = $obj->get_fp(4); #read as 2 doubles @array = $obj->get_fp(2); #read as 4 32 bit ints @array = $obj->get_int(4); #read raw packed value $packedSSEVal = $obj->get_raw();
Is this a better choice for an api?

Replies are listed 'Best First'.
Re: supporting SSE 128 bit data in Perl
by spx2 (Deacon) on Jun 01, 2012 at 01:48 UTC

    I'd go for one of these two, and use them to write all the code that has to deal with SSE2 using them, I'm more inclined to think you want the first option since it gives you full control over everything:

    These guys here seem to be using double * for the data types, maybe you want to use that too.. but regardless, that article is a good read.

    As far as packing goes, I would suggest you bother with that only when your SSE2 calculations are done and you need the result visible in Perl. Then you can easily convert it to some C data type and then to an XS data type or whatever other data structure you need.

      Thank you for asm tips, but I have a C API that sometimes wants SSE2 data as arguments in a C function prototype. I am not trying to use SSE2 cpu instructions or intrinsics directly. If I were, I would know exactly what part of the SSE2 union the data should be in.

      Maybe I shouldn't be reinventing the wheel and instead looking for a CPAN module that already understands what a C union is and somehow integrate it into my code and just say "supply a Foo::CAggregate object ref for parameter 1" :-/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://973618]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found