News for hackers. Stuff that matters.
Re:Packing a string of digits into an integer quickly by gumby on Saturday July 08, 2023 @06:50PM (#36649559)
I don't understand how this can work: there are 15 (or 7 :-) other characters with the same bottom four bits as each digit. How does it distinguish between, say, '4' and 't'? Space and '0' have the same lower four bits: \0!
Re:Packing a string of digits into an integer quickly by n2d4 on Saturday July 08, 2023 @07:36PM (#36649838)
It doesn't distinguish, the non-digit character index is hardcoded into the bitmask (note the missing f in the second hex literal):

  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.
Re:Packing a string of digits into an integer quickly by jcparkyn on Saturday July 08, 2023 @06:55PM (#36649598)
I think the author is only concerned with decimal digits (0-9).
Re:Packing a string of digits into an integer quickly by patrec on Saturday July 08, 2023 @07:12PM (#36649695)
No, he says that the strings can contain spaces and other characters but that he wants them ordered (only) by the decimal digits in them. I don't see how this can work either.
Re:Packing a string of digits into an integer quickly by xcvb on Saturday July 08, 2023 @07:41PM (#36649868)
Looking at the code, it seems like it expects a string of the form "dddddddd?dddddd" where the '?' is any ascii character, and the code assumes the rest of the characters are ascii digits.

Specifically the line: `part2 = _pext_u64(part2, 0x0f000f0f0f0f0f0f);` seems to mask out that character.

Re:Packing a string of digits into an integer quickly by patrec on Saturday July 08, 2023 @08:13PM (#36650058)
Indeed, I hadn't noticed the 000 (rather than 0f0) in there! Thanks!
Re:Packing a string of digits into an integer quickly by vesinisa on Monday July 10, 2023 @12:25PM (#36668640)
Just noticed this pattern fits a basic ISO 8601 datetime string perfectly. ISO requires using 'T' in place of ?, but using space (0x20) there is pretty common.
Re:Packing a string of digits into an integer quickly by kukkamario on Saturday July 08, 2023 @07:33PM (#36649822)
The bitmasks in the code are adjusted so that it works with that specific pattern of numbers to other letters. Masks only pick bits from digits when the are in those specific positions.

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.