I find that the best XS code (easiest to understand, most robust, nicest interface, etc.) is usually the least XS code. That is, write as much as possible in Perl not in C.

This means write Perl code to get your data into a format that is easy for C to handle. In this case I'd just make a big flat list of values for both input and output.

void _do_matrix( char *in, long rows, long cols, char *out ) CODE: long i, j, a; double *in_matrix= (double *)in; double *out_matrix= (double *)out; double value; for( i= 0; i < rows; i++ ) { for( j= i-1; j >= 0; j-- ) { value= 0; for( a= 0; a <= cols; a++ ) { value += in_matri­x[i*cols+a]*in_matrix[j*cols+a]; } value /= (cols-1); out_matrix[i*rows+j]= val­ue; } }
and then the Perl wrapper:
sub do_matrix(\@) { my( $avMatrix )= @_; my( $rows )= 0+@$avMatrix; my( $cols )= 0+@{$avMatrix->[0]}; my $in= pack "d*", map @$_[0..$cols-1], @$avMatrix; my $out= pack "d".($rows*$rows); _do_matrix( $in, $rows, $cols, $out ); my @out= unpack "d*", $out; my @ret; while( @out ) { push @ret, [ splice( @out, 0, $rows ) ]; } return \@ret; }
or something substantially similar after testing finds a few bugs.

                - tye

In reply to Re: perlxs and gcc error (use more Perl) by tye
in thread perlxs and gcc error by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.