in reply to unpacking 6-bit values
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 |