I just could not leave this to rest. There might be more optimizations possible, but at least it offers you 5 native perl methods to do it and the speed diff:

$ cat test.pl use strict; use warnings; use Data::Peek; use Benchmark qw( cmpthese ); my $src = pack "C*" => map { int rand 256 } 0 .. 3071; print STDERR "First 48 bytes of the compressed data ...\n"; DHexDump substr $src, 0, 48; for (unpack "(a3)*", substr $src, 0, 12) { DHexDump $_; my $bits = unpack "B*", $_; (my $b8 = substr $bits, 0, 48) =~ s/(.{8})/$1 /g; (my $b6 = substr $bits, 0, 48) =~ s/(.{6})/$1 /g; print STDERR " $b8\n $b6\n"; } # unpack pack unpack unpack reverse sub upuu { map { unpack "C", pack "b6", scalar reverse $_ } unpack "(a6)*" => unpack "B*" => $src; } # opuu # unpack with 00 sub upuu0 { map { unpack "C", pack "B8", "00$_" } unpack "(a6)*" => unpack "B*" => $src; } # opuu0 # using uuencode sub uu { map { map { ($_ - 32) & 63 } unpack "xC*" => $_ } split m/ *\n/ => pack "u" => $src; } # uu { my @lut; for (0 .. 0b111111) { $lut[$_ << 18] = $_; $lut[$_ << 12] = $_; $lut[$_ << 6] = $_; $lut[$_ ] = $_; } sub mlut { map { my $v = unpack "N", "\00$_\x00\x00"; ($lut[$v & 0b111111_000000_000000_000000], $lut[$v & 0b000000_111111_000000_000000], $lut[$v & 0b000000_000000_111111_000000], $v & 0b000000_000000_000000_111111); } unpack "(a3)*" => $src; } # mlut } # And shift unpack { my $m0 = 0b111111_000000_000000_000000; my $m1 = 0b000000_111111_000000_000000; my $m2 = 0b000000_000000_111111_000000; my $m3 = 0b000000_000000_000000_111111; sub asu { map {my $b = unpack "N", "\x00$_\x00\x00"; ( ($b & $m0) >> 18, ($b & $m1) >> 12, ($b & $m2) >> 6, ($b & $m3) ) } unpack "(a3)*" => $src; } # asu } my @dst; @dst = uu (); print STDERR "$#dst E\n"; print STDERR "uu: (@dst[0..22] ...\n @dst[4073..4095])\n"; @dst = upuu (); print STDERR "upuu: (@dst[0..22] ...\n @dst[4073..4095])\n"; @dst = upuu0 (); print STDERR "upuu0: (@dst[0..22] ...\n @dst[4073..4095])\n"; @dst = asu (); print STDERR "asu: (@dst[0..22] ...\n @dst[4073..4095])\n"; @dst = mlut (); print STDERR "mlut: (@dst[0..22] ...\n @dst[4073..4095])\n"; cmpthese (-2, { uu => \&uu, upuu => \&upuu, upuu0 => \&upuu0, asu => \ +&asu, mlut => \&mlut }); $ $ perl test.pl First 48 bytes of the compressed data ... 0000 1c 66 02 ac 50 1f 41 b2 e4 10 b9 03 38 b3 7e 2d .f..P.A.....8. +~- 0010 5e 95 02 c6 c7 71 39 08 c7 82 37 40 0d 40 c1 e1 ^....q9...7@.@ +.. 0020 1f 9a d9 6d 51 f3 7e 0a 8d 9b 75 85 0a a5 82 65 ...mQ.~...u... +.e 0000 1c 66 02 .f. 00011100 01100110 00000010 000111 000110 011000 000010 0000 ac 50 1f .P. 10101100 01010000 00011111 101011 000101 000000 011111 0000 41 b2 e4 A.. 01000001 10110010 11100100 010000 011011 001011 100100 0000 10 b9 03 ... 00010000 10111001 00000011 000100 001011 100100 000011 4095 E uu: (7 6 24 2 43 5 0 31 16 27 11 36 4 11 36 3 14 11 13 62 11 21 58 +... 23 32 30 37 37 26 19 30 42 19 29 32 54 2 9 3 23 32 62 52 5 31 +46) upuu: (7 6 24 2 43 5 0 31 16 27 11 36 4 11 36 3 14 11 13 62 11 21 58 +... 23 32 30 37 37 26 19 30 42 19 29 32 54 2 9 3 23 32 62 52 5 31 +46) upuu0: (7 6 24 2 43 5 0 31 16 27 11 36 4 11 36 3 14 11 13 62 11 21 58 +... 23 32 30 37 37 26 19 30 42 19 29 32 54 2 9 3 23 32 62 52 5 31 +46) asu: (7 6 24 2 43 5 0 31 16 27 11 36 4 11 36 3 14 11 13 62 11 21 58 +... 23 32 30 37 37 26 19 30 42 19 29 32 54 2 9 3 23 32 62 52 5 31 +46) mlut: (7 6 24 2 43 5 0 31 16 27 11 36 4 11 36 3 14 11 13 62 11 21 58 +... 23 32 30 37 37 26 19 30 42 19 29 32 54 2 9 3 23 32 62 52 5 31 +46) Rate upuu0 upuu mlut asu uu upuu0 354/s -- -1% -56% -64% -68% upuu 357/s 1% -- -56% -64% -67% mlut 813/s 130% 128% -- -17% -26% asu 985/s 178% 176% 21% -- -10% uu 1093/s 209% 206% 34% 11% -- $

Enjoy!


Enjoy, Have FUN! H.Merijn

In reply to Re: unpacking 6-bit values by Tux
in thread unpacking 6-bit values by BrowserUk

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.