Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: 3-byte representation (Simplified!)

by AnomalousMonk (Abbot)
on Oct 12, 2011 at 23:22 UTC ( #931106=note: print w/ replies, xml ) Need Help??


in reply to Re: 3-byte representation (Simplified!)
in thread 3-byte representation

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


Comment on Re^2: 3-byte representation (Simplified!)
Select or Download Code
Re^3: 3-byte representation (Simplified!)
by BrowserUk (Pope) on Oct 13, 2011 at 00:49 UTC
    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

        I'm pretty sure that the only 6-bit byte machines still running -- if any at all -- will be in museums.

        These days, the most likely candidates are DACS, sound-cards, maybe video, and GPS type hardware.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2014-09-22 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls