Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by aeruder on Wednesday May 16, 2018 @11:58AM
(#17083419)
The trick here is that each byte is treated as a signed 8-bit number. When the top bit is set, the number is negative.
[ Parent
| on HN ]
Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by zbjornson on Wednesday May 16, 2018 @12:06PM
(#17083483)
Oh duh, thanks. (Checking less than zero, read it wrong.)
I think it would be faster to OR the entire string with itself, then finally check the 8th bit though. On Skylake that would cut it to 0.33 cycles per 16 bytes (HSW 1 per 16).
Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by Sharlin on Wednesday May 16, 2018 @01:42PM
(#17084541)
Depends on your input. If non-ASCII strings are frequent and likely to contain a non-ASCII character fairly close to the start of the string, then it makes sense to short circuit.
Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by loeg on Wednesday May 16, 2018 @05:57PM
(#17086825)
The previous >0 algorithm didn't short-circuit. There is no change to short-circuit behavior here.
Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by Sharlin on Thursday May 17, 2018 @06:23AM
(#17090118)
Ah, I was thinking of the naive implementation in the previous post [1].
[1] https://lemire.me/blog/2018/05/09/how-quickly-can-you-check-...
Re:Validating UTF-8 strings using as little as 0.7 cycles pe...
by vardump on Wednesday May 16, 2018 @12:35PM
(#17083789)
> I think it would be faster to OR the entire string with itself, then finally check the 8th bit though.
The string could have NUL (zero) bytes in between.