Wow! Interesting thread!

Not sure if a C version of the jmcnamara idea got benchmarked. I think such a thing would look like below. I didn't look at the ASM code, but even at a low optimize level, this probably results in pretty good code.

#include <stdio.h> #define MASK05_00 (0x3f) /*6 bits at a time*/ #define MASK11_06 (MASK05_00 << 6) #define MASK17_12 (MASK11_06 << 6) #define MASK23_18 (MASK17_12 << 6) int main (int argc, char **argv) { unsigned char in [24] = { 00, 16, 131, 16, 81, 135, 32, 146, 139, 48, 211, 143, 65, 20, 147, 81, 85, 151, 97, 150, 155, 113, 215, 159}; unsigned char out [32]; void expand6 (unsigned char *in, unsigned char* out); void hexdump (unsigned char *p, int n); expand6(in, out); hexdump(in, 24); hexdump(out, 32); return(0); } void expand6 (unsigned char *in, unsigned char* out) { unsigned int temp; int i; for (i=0; i<8; i++) /*8 24 bit sequences */ { temp = *in++ << 16; temp |= *in++ << 8; temp |= *in++; *out++ = (temp & MASK23_18)>>18; *out++ = (temp & MASK17_12)>>12; *out++ = (temp & MASK11_06)>>6; *out++ = (temp & MASK05_00); } } void hexdump(unsigned char *p, int n) { int i; for (i=0; i<n; i++) printf("%02x ", *p++); puts("\n"); } /* ouput 00 10 83 10 51 87 20 92 **in buf 8b 30 d3 8f 41 14 93 51 55 97 61 96 9b 71 d7 9f 00 01 02 03 04 05 06 07 **out buf 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f */

In reply to Re: unpacking 6-bit values by Marshall
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.