|
Posted by Peter Billam on January 6, 2005, 11:17 am
Please log in for more thread options
Greetings. I'm an xs newbie, just got my first xs working :-) and so,
dizzy with success, I'd now like to convert to C a perl sub which
takes a list of FP numbers as its parameters, e.g.
@numbers = (1.234, 2.345, 3.456, 4,567);
@answers = &subr ( @numbers );
I think I've got the Returning-A-List bit worked out, using
PPCODE:
and then
EXTEND(SP, 2);
PUSHs(sv_2mortal(newSViv(v0)));
PUSHs(sv_2mortal(newSViv(v1)));
I vaguely remember that perl passes its FP args as doubles, and so AFAIK
I'd like to write a C routine which receives an array of doubles as its
parameters.
I looked in "perldoc perlxs" at "Variable-length Parameter Lists" and
"The Typemap", and I looked in /usr/lib/perl5/5.8.3/ExtUtils/typemap
for something looking like an array of doubles, but I didn't see
anything ready-made.
I'm sure there'll be an enormous difference in Easiness between doing
this the Right Way or some Other Way... Advice gratefully received...
--
Regards, Peter
Peter Billam, DPIWE/ILS/CIT/Servers, hbt/lnd/l8, 6233 3061
|
|
Posted by Tassilo v. Parseval on January 6, 2005, 8:52 am
Please log in for more thread options
Also sprach Peter Billam:
> Greetings. I'm an xs newbie, just got my first xs working :-) and so,
> dizzy with success, I'd now like to convert to C a perl sub which
> takes a list of FP numbers as its parameters, e.g.
> @numbers = (1.234, 2.345, 3.456, 4,567);
> @answers = &subr ( @numbers );
>
> I think I've got the Returning-A-List bit worked out, using
> PPCODE:
> and then
> EXTEND(SP, 2);
> PUSHs(sv_2mortal(newSViv(v0)));
> PUSHs(sv_2mortal(newSViv(v1)));
>
> I vaguely remember that perl passes its FP args as doubles, and so AFAIK
> I'd like to write a C routine which receives an array of doubles as its
> parameters.
>
> I looked in "perldoc perlxs" at "Variable-length Parameter Lists" and
> "The Typemap", and I looked in /usr/lib/perl5/5.8.3/ExtUtils/typemap
> for something looking like an array of doubles, but I didn't see
> anything ready-made.
>
> I'm sure there'll be an enormous difference in Easiness between doing
> this the Right Way or some Other Way... Advice gratefully received...
Variable length argument lists don't work particularly well with
typemaps. You could probably define your own typemap for 'double*' or
'NV*', but passing in just a list of values is more easily done without:
void
subr (...)
CODE:
{
register int i;
double *ary;
/* New() is perlapi's equivalent
* to malloc(). See perlclib.pod */
New(0, ary, items, double);
for (i = 0; i < items; i++)
ary[i] = (double) SvNV(ST(i));
...
for (i = 0; i < items; i++)
ST(i) = sv_2mortal(newSVnv(ary[i] / 2.0));
Safefree(ary);
XSRETURN(items);
}
'items' is a specially pre-defined variable holding the number of
arguments your XSUB received. That means that there are 'items' SV*
variables to be found on the argument stack.
You can access the argument stack by using 'ST(i)'. As each of these
'ST(i)'s is an ordinary SV, you can use the macros and functions from
perlapi to convert them to plain C types: SvNV for turning it into a
double, SvIV for an int, SvPV for a char* and so on.
The above uses 'CODE' and assigning to ST() directly for building the
return list. You need 'CODE' when using the variadic argument list
(...). Also, it's often more convenient than PPCODE.
Note that there is a mailinglist designated to XS that might be of use
to you: perl-xs@perl.org.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam)(rekcah)(lreP)!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.t$&."'!#+sexisexiixesixeseg;y~n~~dddd;eval
|
| Similar Threads | Posted | | strings and numbers | July 30, 2005, 2:28 am |
| strings and numbers | July 30, 2005, 2:27 am |
| converting phone numbers to alphabets | July 7, 2004, 6:49 pm |
| need translators for Lingua::Slavic::Numbers | July 29, 2008, 10:52 am |
| Modules/code for comparing version numbers? | September 15, 2004, 1:55 pm |
| Print Subject without characters Just Numbers using POP3Client. Please Help | April 10, 2007, 11:27 pm |
| [ANNOUNCE] Test::Float -- compare numbers to specified precision | December 26, 2004, 4:20 pm |
| How to print just numbers includes in Subject using POP3Client Module. | April 9, 2007, 9:18 pm |
|