Skip to content

Commit

Permalink
add IO::Misc::bswap32
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Nov 12, 2024
1 parent a6458c0 commit a718cdb
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 264 deletions.
9 changes: 5 additions & 4 deletions Basic/IO-Misc/misc.pd
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use List::Util;

sub defswap {
my ($bytes, $doc) = @_;
my $sized_type = $bytes == 16 ? "PDL_CDouble" : "uint".($bytes*8)."_t";
my $sized_type = "struct {char n[$bytes];}";
pp_def(
"bswap$bytes",
Pars => '[io] x()',
Expand All @@ -68,10 +68,10 @@ if (sizeof(\$GENERIC(x)) < $bytes)
EOF
Code => <<EOF,
int i;
int n = sizeof(\$x()) / sizeof($sized_type);
$sized_type *aa = ($sized_type*) &\$x();
int n = sizeof(\$x()) / $bytes;
$sized_type *aa = (void *) &\$x();
for (i=0;i<n; i++) {
$sized_type bb = aa[i];
$sized_type bb; memcpy(&bb, aa+i, $bytes);
PDL_Byte *a = (PDL_Byte*) (void*) (aa+i);
PDL_Byte *b = (PDL_Byte*) &bb;
@{[map "a[$_] = b[".($bytes-$_-1)."];", 0..$bytes-1]}
Expand All @@ -84,6 +84,7 @@ defswap(2, "Swaps pairs of bytes in argument x()");
defswap(4, "Swaps quads of bytes in argument x()");
defswap(8, "Swaps octets of bytes in argument x()");
defswap(16, "Swaps 16s of bytes in argument x()");
defswap(32, "Swaps 32s of bytes in argument x()");

pp_addpm(<<'!NO!SUBS!');
Expand Down
4 changes: 4 additions & 0 deletions Basic/IO-Misc/t/misc.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use Test::PDL;
my $tempd = tempdir( CLEANUP => 1 ) or die "Couldn't get tempdir\n";
my ($fileh,$file) = tempfile( DIR => $tempd );

for my $type (PDL::Types::types()) {
ok $type->bswap, "$type has bswap";
}

############# Test rcols with colsep and missing fields ###################

print $fileh <<EOD;
Expand Down
Loading

0 comments on commit a718cdb

Please sign in to comment.