]> git.vpit.fr Git - perl/modules/Scalar-Vec-Util.git/blob - lib/Scalar/Vec/Util.pm
Silence some redefine warnings for SVU_PP and SVU_SIZE
[perl/modules/Scalar-Vec-Util.git] / lib / Scalar / Vec / Util.pm
1 package Scalar::Vec::Util;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7
8 =head1 NAME
9
10 Scalar::Vec::Util - Utility routines for vec strings.
11
12 =head1 VERSION
13
14 Version 0.05
15
16 =cut
17
18 our $VERSION;
19 BEGIN {
20  $VERSION = '0.05';
21  eval {
22   require XSLoader;
23   XSLoader::load(__PACKAGE__, $VERSION);
24   1;
25  } or do {
26   *SVU_PP   = sub () { 1 };
27   *SVU_SIZE = sub () { 1 };
28   *vfill    = *vfill_pp;
29   *vcopy    = *vcopy_pp;
30   *veq      = *veq_pp;
31  }
32 }
33
34 =head1 SYNOPSIS
35
36     use Scalar::Vec::Util qw/vfill vcopy veq/;
37
38     my $s;
39     vfill $s, 0, 100, 1; # Fill with 100 bits 1 starting at 0.
40     my $t;
41     vcopy $s, 20, $t, 10, 30; # Copy 30 bits from $s, starting at 20,
42                               #                to $t, starting at 10.
43     vcopy $t, 10, $t, 20, 30; # Overalapping areas DWIM.
44     if (veq $t, 10, $t, 20, 30) { ... } # Yes, they are equal now.
45
46 =head1 DESCRIPTION
47
48 A set of utilities to manipulate bits in vec strings. Highly optimized XS routines are used when available, but straightforward pure perl replacements are also provided for platforms without a C compiler.
49
50 This module doesn't reimplement bit vectors. It can be used on the very same scalars that C<vec> builds, or actually on any Perl string (C<SVt_PV>).
51
52 =head1 CONSTANTS
53
54 =head2 C<SVU_PP>
55
56 True when pure perl fallbacks are used instead of XS functions.
57
58 =head2 C<SVU_SIZE>
59
60 Size in bits of the unit used for moves. The higher this value is, the faster the XS functions are. It's usually C<CHAR_BIT * $Config{alignbytes}>, except on non-little-endian architectures where it currently falls back to C<CHAR_BIT> (e.g. SPARC).
61
62 =head1 FUNCTIONS
63
64 =head2 C<vfill $vec, $start, $length, $bit>
65
66 Starting at C<$start> in C<$vec>, fills C<$length> bits with C<$bit>. Grows C<$vec> if necessary.
67
68 =cut
69
70 sub _alldef {
71  for (@_) { return 0 unless defined }
72  return 1;
73 }
74
75 sub vfill_pp {
76  (undef, my $s, my $l, my $x) = @_;
77  croak "Invalid argument" unless _alldef @_;
78  return unless $l;
79  $x = 1 if $x;
80  vec($_[0], $_, 1) = $x for $s .. $s + $l - 1;
81 }
82
83 =head2 C<< vcopy $from => $from_start, $to => $to_start, $length >>
84
85 Copies C<$length> bits starting at C<$from_start> in C<$from> to C<$to_start> in C<$to>. If C<$from_start + $length> is too long for C<$from>, zeros are copied past C<$length>. Grows C<$to> if necessary.
86
87 =cut
88
89 sub vcopy_pp {
90  my ($fs, $ts, $l) = @_[1, 3, 4];
91  croak "Invalid argument" unless _alldef @_;
92  return unless $l;
93  my $step = $ts - $fs;
94  if ($step <= 0) { 
95   vec($_[2], $_ + $step, 1) = vec($_[0], $_, 1) for $fs .. $fs + $l - 1;
96  } else { # There's a risk of overwriting if $_[0] and $_[2] are the same SV.
97   vec($_[2], $_ + $step, 1) = vec($_[0], $_, 1) for reverse $fs .. $fs + $l - 1;
98  }
99 }
100
101 =head2 C<< veq $v1 => $v1_start, $v2 => $v2_start, $length >>
102
103 Returns true if the C<$length> bits starting at C<$v1_start> in C<$v1> and C<$v2_start> in C<$v2> are equal, and false otherwise. If needed, C<$length> is decreased to fit inside C<$v1> and C<$v2> boundaries.
104
105 =cut
106
107 sub veq_pp {
108  my ($s1, $s2, $l) = @_[1, 3, 4];
109  croak "Invalid argument" unless _alldef @_;
110  my $i = 0;
111  while ($i < $l) {
112   return 0 if vec($_[0], $s1 + $i, 1) != vec($_[2], $s2 + $i, 1);
113   ++$i;
114  }
115  return 1;
116 }
117
118 =head1 EXPORT
119
120 The functions L</vfill>, L</vcopy> and L</veq> are only exported on request. All of them are exported by the tags C<':funcs'> and C<':all'>.
121
122 The constants L</SVU_PP> and L</SVU_SIZE> are also only exported on request. They are all exported by the tags C<':consts'> and C<':all'>.
123
124 =cut
125
126 use base qw/Exporter/;
127
128 our @EXPORT         = ();
129 our %EXPORT_TAGS    = (
130  'funcs'  => [ qw/vfill vcopy veq/ ],
131  'consts' => [ qw/SVU_PP SVU_SIZE/ ]
132 );
133 our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
134 $EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
135
136 =head1 BENCHMARKS
137
138 The following timings were obtained by running the C<samples/bench.pl> script. The C<_pp> entries are the pure Perl versions, while C<_bv> are L<Bit::Vector> versions.
139
140 =over 4
141
142 =item This is for perl 5.8.8 on a Core 2 Duo 2.66GHz machine (unit is 64 bits).
143
144     Filling bits at a given position :
145                   Rate vfill_pp vfill_bv    vfill
146     vfill_pp    80.3/s       --    -100%    -100%
147     vfill_bv 1053399/s 1312401%       --     -11%
148     vfill    1180792/s 1471129%      12%       --
149
150     Copying bits from a bit vector to a different one :
151                  Rate vcopy_pp vcopy_bv    vcopy
152     vcopy_pp    112/s       --    -100%    -100%
153     vcopy_bv  62599/s   55622%       --     -89%
154     vcopy    558491/s  497036%     792%       --
155
156     Moving bits in the same bit vector from a given position to a different one :
157                  Rate vmove_pp vmove_bv    vmove
158     vmove_pp   64.8/s       --    -100%    -100%
159     vmove_bv  64742/s   99751%       --     -88%
160     vmove    547980/s  845043%     746%       --
161
162     Testing bit equality from different positions of different bit vectors :
163                Rate  veq_pp  veq_bv     veq
164     veq_pp   92.7/s      --   -100%   -100%
165     veq_bv  32777/s  35241%      --    -94%
166     veq    505828/s 545300%   1443%      --
167
168 =item This is for perl 5.10.0 on a Pentium 4 3.0GHz (unit is 32 bits).
169
170                  Rate vfill_pp vfill_bv    vfill
171     vfill_pp    185/s       --    -100%    -100%
172     vfill_bv 407979/s  220068%       --     -16%
173     vfill    486022/s  262184%      19%       --
174
175                  Rate vcopy_pp vcopy_bv    vcopy
176     vcopy_pp   61.5/s       --    -100%    -100%
177     vcopy_bv  32548/s   52853%       --     -83%
178     vcopy    187360/s  304724%     476%       --
179
180                  Rate vmove_pp vmove_bv    vmove
181     vmove_pp   63.1/s       --    -100%    -100%
182     vmove_bv  32829/s   51933%       --     -83%
183     vmove    188572/s  298787%     474%       --
184
185                Rate  veq_pp  veq_bv     veq
186     veq_pp   34.2/s      --   -100%   -100%
187     veq_bv  17518/s  51190%      --    -91%
188     veq    192181/s 562591%    997%      --
189
190 =item This is for perl 5.10.0 on an UltraSPARC-IIi (unit is 8 bits).
191
192                 Rate vfill_pp    vfill vfill_bv
193     vfill_pp  4.23/s       --    -100%    -100%
194     vfill    30039/s  709283%       --     -17%
195     vfill_bv 36022/s  850568%      20%       --
196
197                 Rate vcopy_pp vcopy_bv    vcopy
198     vcopy_pp  2.74/s       --    -100%    -100%
199     vcopy_bv  8146/s  297694%       --     -60%
200     vcopy    20266/s  740740%     149%       --
201
202                 Rate vmove_pp vmove_bv    vmove
203     vmove_pp  2.66/s       --    -100%    -100%
204     vmove_bv  8274/s  311196%       --     -59%
205     vmove    20287/s  763190%     145%       --
206
207               Rate  veq_pp  veq_bv     veq
208     veq_pp  7.33/s      --   -100%   -100%
209     veq_bv  2499/s  33978%      --    -87%
210     veq    19675/s 268193%    687%      --
211
212 =back
213
214 =head1 CAVEATS
215
216 Please report architectures where we can't use the alignment as the move unit. I'll add exceptions for them.
217
218 =head1 DEPENDENCIES
219
220 L<Carp>, L<Exporter> (core modules since perl 5), L<XSLoader> (since perl 5.006).
221
222 =head1 SEE ALSO
223
224 L<Bit::Vector> gives a complete reimplementation of bit vectors.
225
226 =head1 AUTHOR
227
228 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
229
230 You can contact me by mail or on C<irc.perl.org> (vincent).
231
232 =head1 BUGS
233
234 Please report any bugs or feature requests to C<bug-scalar-vec-util at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Scalar-Vec-Util>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
235
236 =head1 SUPPORT
237
238 You can find documentation for this module with the perldoc command.
239
240     perldoc Scalar::Vec::Util
241
242 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Scalar-Vec-Util>.
243
244 =head1 COPYRIGHT & LICENSE
245
246 Copyright 2008 Vincent Pit, all rights reserved.
247
248 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
249
250 =cut
251
252 1; # End of Scalar::Vec::Util