Packing a string of digits into an integer quickly
lemire.me
lemire.me
6502 had a BCD (binary coded decimal) mode; basically when in BCD mode arithmetic operators carried at 0x9 rather than 0xF, and operations worked nibble-wise rather than byte-wise. So it was common to represent 2 digits in one bye regardless of hex vs decimal.
I actually have been thinking about this lately because of the other half of the problem, ie ASCII <—> native types. On the Apple 2 one really cool speed trick was to store strings directly on the screen. The Apple 2 text screen was memory mapped (0x400 - 0x7FF IIRC). To write strings to the screen in assembly, you wrote the ASCII character codes to the right memory locations. But if you were trying to eke out every single cycle you could, and your strings were fixed length, you didn’t need to store them somewhere and copy them to the screen; you could use the screen as the store for your strings.
I once made a stupid program that counted to one million this way; it ran fast enough to watch it finish.
100/256 seems very tolerable efficiency-wise.
part1 = _pext_u64(part1, 0x0f0f0f0f0f0f0f0f);
part2 = _pext_u64(part2, 0x0f000f0f0f0f0f0f);
You are right that if you were to use a space in an unexpected position, the code wouldn't work.So this solution only works when the digit character places are fixed and known.
It would be possible to generate mask based on the first nibbles so that it would ignore non-digits. Maybe even with some fairly optimized vector instructions. Not sure would it be faster than some naive solution.
Specifically the line: `part2 = _pext_u64(part2, 0x0f000f0f0f0f0f0f);` seems to mask out that character.
Here's my solution, which also uses pdep/pext
The time scale between OP's code and yours is about the same as the timescale between yours and mine :)