X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FScalar%2FVec%2FUtil.pm;h=dd8b1cbcea3b3cdb30da08a3098a114c9701b838;hb=856caa5792565f48b11a5dc86856519b649eaffb;hp=12d049c26364708fa28e59b337745040da53e5e4;hpb=01ef742d705c0bd6577139d1ee8a2808101336da;p=perl%2Fmodules%2FScalar-Vec-Util.git diff --git a/lib/Scalar/Vec/Util.pm b/lib/Scalar/Vec/Util.pm index 12d049c..dd8b1cb 100644 --- a/lib/Scalar/Vec/Util.pm +++ b/lib/Scalar/Vec/Util.pm @@ -132,14 +132,48 @@ sub vshift { $left = 0; $bits = -$bits; } - $bits = $length if $bits > $length; + if ($bits < $length) { + $length -= $bits; + if ($left) { + vcopy($_[0], $start, $_[0], $start + $bits, $length); + vfill($_[0], $start, $bits, $insert) if defined $insert; + } else { + vcopy($_[0], $start + $bits, $_[0], $start, $length); + vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + } + } else { + vfill($_[0], $start, $length, $insert) if defined $insert; + } +} + +=head2 C<< vrot $v, $start, $length, $bits >> + +In the area starting at C<$start> and of length C<$length> in C<$v>, rotates bits C positions left if C<< $bits > 0 >> and right otherwise. +Bits outside of the specified area are left untouched. +Currently allocates an extra buffer of size C. + +=cut + +sub vrot { + my ($start, $length, $bits) = @_[1 .. 3]; + return unless $length and $bits; + my $left = 1; + if ($bits < 0) { + $left = 0; + $bits = -$bits; + } + $bits %= $length; + return unless $bits; $length -= $bits; + my $buf = ''; if ($left) { - vcopy($_[0], $start, $_[0], $start + $bits, $length); - vfill($_[0], $start, $bits, $insert) if defined $insert; + vcopy($_[0], $start + $length, $buf, 0, $bits); + vcopy($_[0], $start, $_[0], $start + $bits, $length); + vcopy($buf, 0, $_[0], $start, $bits); } else { - vcopy($_[0], $start + $bits, $_[0], $start, $length); - vfill($_[0], $start + $length, $bits, $insert) if defined $insert; + vcopy($_[0], $start, $buf, 0, $bits); + vcopy($_[0], $start + $bits, $_[0], $start, $length); + vcopy($buf, 0, $_[0], $start + $length, $bits); } } @@ -163,7 +197,7 @@ sub veq_pp { =head1 EXPORT -The functions L, L, L and L are only exported on request. +The functions L, L, L, L and L are only exported on request. All of them are exported by the tags C<':funcs'> and C<':all'>. The constants L and L are also only exported on request. @@ -175,7 +209,7 @@ use base qw/Exporter/; our @EXPORT = (); our %EXPORT_TAGS = ( - 'funcs' => [ qw/vfill vcopy vshift veq/ ], + 'funcs' => [ qw/vfill vcopy vshift vrot veq/ ], 'consts' => [ qw/SVU_PP SVU_SIZE/ ] ); our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;