Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: 3-byte representation (Simplified!)

by BrowserUk (Pope)
on Oct 12, 2011 at 16:31 UTC ( #931053=note: print w/ replies, xml ) Need Help??


in reply to 3-byte representation

Update: Simplified the packing by removing a redundant step.

Update2: eliminated the map from the packing.

Non-trivial:

#! perl -slw use strict; use Data::Dump qw[ pp ]; use Math::Random::MT qw[ rand ]; my @sint24s = map{ -2**23 + int( rand 2**24 ) } 1 .. 20; ## removed duplicated my $packed -- Thanks to AnonmalousMonk my $packed = join '', unpack '(a3x)*', pack 'l*', @sint24s; my @unpacked = map { unpack 'l', $_ . chr( vec( $_, 23, 1 ) ? 255 : 0 ); } unpack '(a3)*', $packed; print "$sint24s[ $_ ] ;; $unpacked[ $_ ]" for 0 .. $#sint24s; __END__ C:\test>sint24 4243386 ;; 4243386 4809369 ;; 4809369 -888567 ;; -888567 -7576685 ;; -7576685 1987080 ;; 1987080 -2170022 ;; -2170022 -1135866 ;; -1135866 1924446 ;; 1924446 6348263 ;; 6348263 1911716 ;; 1911716 -1791354 ;; -1791354 -8343943 ;; -8343943 -6224088 ;; -6224088 3919567 ;; 3919567 -1176382 ;; -1176382 6288012 ;; 6288012 -5569609 ;; -5569609 -5363232 ;; -5363232 -1344267 ;; -1344267 3649155 ;; 3649155

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: 3-byte representation (Simplified!)
Download Code
Re^2: 3-byte representation (Simplified!)
by AnomalousMonk (Monsignor) on Oct 12, 2011 at 23:22 UTC
    my $packed = join '', unpack '(a3x)*', pack 'l*', @sint24s;

    Rather than the above,
        my $packed = pack '(l<X)*', @array;
    is more concise, probably faster (but this latter not checked).

    A shame there seems to be no way to get rid of the final  map on the unpacking side.

    >perl -wMstrict -le "use Test::More 'no_plan'; use List::Util qw(shuffle); ;; sub rand_s24 { return map { int(rand 2**24) - 2**23 } 1 .. $_[0]; } ;; use constant MIN_S24 => -(2**23); use constant MAX_S24 => -MIN_S24() - 1; use constant VALUES => (0, 1, -1, MIN_S24, MAX_S24); use constant N_VALUES => scalar(@{[ VALUES ]}); use constant MAX_LEN => 25; ;; for my $pass (1 .. 200) { for my $len (1 .. MAX_LEN) { my @to_pack = (rand_s24($len - N_VALUES), VALUES)[-$len .. -1]; @to_pack = shuffle @to_pack; $len == @to_pack or die qq{bad len: array to pack}; ;; my $packed = pack '(l<X)*', @to_pack; length($packed) == 3 * $len or die qq{bad len: packed string}; ;; my @unpacked = map { unpack('l<', qq{\x00$_})/256 } unpack '(a3)*', $packed; $len == @unpacked or die qq{bad len: unpacked array}; ;; is_deeply \@unpacked, \@to_pack, sprintf('pass %d: len %d', $pass, $len); } } " ok 1 - pass 1: len 1 ok 2 - pass 1: len 2 ... (4996 lines elided) ... ok 4999 - pass 200: len 24 ok 5000 - pass 200: len 25 1..5000
      my $packed = pack '(l<X)*', @array; is more concise, probably faster

      That's clever, but does have the limitation that you cannot produce a big-endian stream.

      Mind, I don't know if there is any hardware that accepts 24-bit BE values.

      A shame there seems to be no way to get rid of the final map on the unpacking side.

      Agreed. This isn't the first time that I've wished that pack would allow the insertion of values from the template.

      I've also wished for a more generic version of vec that allowed arbitrary numbers of bits, rather than just powers of 2.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Mind, I don't know if there is any hardware that accepts 24-bit BE values.

        I don't know if there are still any out in the wild and I don't recall their endian-ness but ICL 1900 series machines used a 6-bit character representation and had 24-bit words.

        Update: Corrected typo, supplied missing word :-(

        Cheers,

        JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://931053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-07-30 09:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls