6 ms·
In case anyone might be mislead given the topic of this article, you shouldn't do base64 encoding with a general purpose routine like getbits(). Since 3 * 8 / 6
by psykotic 9y ago
In case anyone might be mislead given the topic of this article, you shouldn't do base64 encoding with a general purpose routine like getbits(). Since 3 * 8 / 6 = 4 you can process the input bytes in groups of 3 to avoid straddling issues (ideally with an unaligned 32-bit load if they're fast on your platform). Then your inner loop will look like this:
uint32_t triple = *(uint32_t *)in;
*out++ = base64[triple & MASK];
*out++ = base64[(triple >> 6) & MASK];
*out++ = base64[(triple >> 12) & MASK];
*out++ = base64[(triple >> 18) & MASK];
in += 3;
(The 32-bit load can read one byte past the end of the buffer, so allocate extra space or stop the loop early.)