lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Nov 4, 2014, at 11:10 AM, Jay Carlson <nop@nop.com> wrote:

On Nov 4, 2014 1:28 PM, "Tim Hill" <drtimhill@gmail.com> wrote:

> Have you thought of using the new string.unpack() function to get the 3 bytes as a 64-bit integer? No idea if it would be faster, but perhaps doing multiple integer unpacks in one call would do it. Once you have the integer, peeling off 6 bits at a time is easy.

Surely you mean "12 bits at a time"... wait, no, I'm not going to play this game…

er, no .. you take 3 input bytes = 24 bits, then use this to generate 4 output bytes, so 6 input bits per byte. So of reach output byte you mask the lower 6 bits and lookup the output byte/character, then shift right 6 bits and repeat 4 times. Of course you need to be careful with endian-ness, but unpack can take care of that. Note that although you read in a 64-bit integer, you only process three bytes, so the input offset advances three bytes every time. You also must handle the last 3 bytes carefully, as you cannot read the extra byte off the end of the string.

I’m not really sure this would be any faster than the OP code for small runs, as I suspect string.unpack() has a fair amount of overhead (if nothing else, it has to interpret the format string).

—Tim