in reply to unpacking 6-bit values

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 */

Replies are listed 'Best First'.
Re^2: unpacking 6-bit values
by BrowserUk (Patriarch) on Dec 12, 2010 at 14:53 UTC
    Not sure if the C version of the jmcnamara idea got benchmarked.

    Yes. It is laballed as _24to32_2() in my benchmark posts above. The _byteswap_ulong() is a compiler pragma that reduces to a single x64 assembler instruction that effectively does the same thing as your manipulations of the temp variable above. And the masking constants are hardcoded.

    void _24to32_2( SV *packed ) { IS_VARS; char *pp = SvPVX( packed ); int i; IS_RESET; for( i=0; i<24; i+=3 ) { unsigned int n = _byteswap_ulong( *(unsigned long*)&pp[ i -1 ] + ); IS_PUSHUV( ( n & 0xfc0000 ) >> 18 ); IS_PUSHUV( ( n & 0x03f000 ) >> 12 ); IS_PUSHUV( ( n & 0x000fc0 ) >> 6 ); IS_PUSHUV( ( n & 0x00003f ) ); } IS_DONE; return; }

    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.