Decoding UTF8 with parallel extract
nrk.neocities.org
nrk.neocities.org
Here is e.g. the NEON code: https://github.com/simdutf/simdutf/blob/1b8ca3d1072a8e2e1026...
Upthread was posted, "I want to iterate over UTF-8 codepoints directly. The initial test for a 1-byte sequence is just too simple with a branch and will be hit most of the time."
Compared to this, your proposals require iterating twice, in the context as I understand it: either (1) to convert to UCS-4 and (2) to process; or (1) to establish pointers to the starting UTF-8 code unit of each code point and (2) to process.
Additionally, if you convert to UCS-4/UTF-32, and most of the text is in ASCII, you will roughly quadruple the space required for this conversion. If you establish 32-bit pointers into the UTF-8 string for the starting code unit of each code point, you will also approximately quadruple the space required. Obviously, double that if you're using 64-bit pointers.
By contrast, iterating through a UTF-8 string directly has no such problems, at the expense of slightly more CPU time for each iteration. Such iteration can take the form of:
// Extremely hot codepath. Handling well-formed UTF-8 by macro --> ~4% speedup
#define next(R) ((R[1]>>6!=2) ? (R+1) : (R[2]>>6!=2) ? (R+2) : \
(R[3]>>6!=2) ? (R+3) : (R[4]>>6!=2) ? (R+4) : next_func(R+4))
static const unsigned char *next_func(const unsigned char *rex) {
do { rex++; } while (*rex && rex[0]>>6 == 2); return rex;
}
Are you talking about something else? Perhaps I have misunderstood something somewhere.The article is using pext, which is different from a regular table lookup. While it has horrible latency and overhead on older chips, on Zen3 it appears to decode to a single uop with a latency of 3, and on Haswell it also decodes to one uop with a latency of 3. So it's not as bad as a regular table lookup as long as you're on a new enough processor.
It's also possible to do vectorized lookup table loads if your lookup table is small enough and you have a modern CPU, see http://0x80.pl/notesen/2016-03-13-simd-lookup-pshufb.html and http://0x80.pl/notesen/2019-02-03-simd-switch-implementation... (there's a more generalized version of this I can't remember the URL of...)
A lot of tools that create JSON, XML, and so forth will escape any non ASCII characters by default. Python's json package, for example.
#define cdpt(s) ((s[0] < 128) ? s[0] : cdpt_func(s))
0x10000U >> ((0x1531U >> (i*5)) & 31);
On most current x86 chips this has a latency of 3 cycles -- LEA+SHR+SHR -- which is better than an L1 cache hit almost everywhere. 0x10880 & (24 << (i*4))It's part of simdutf.