by ilyt · 3y ago · 0 comments
I don't think people are making lookup tables to adding or xoring 2 numbers...
Pshufb (4-bit / 16-byte lookup table)
You might be surprised at how many operations can be done without touching the load/store units on CPUs.
In my experience, it's usually worth the effort. Then again, it's a LOT of effort needed.
If you are just doing things simply and easily? Lookups are almost the easiest solution. But I probably wouldn't reach for lookup tables for a performance based problem today.
The function to do the same for a byte is... what? 3 or 4 lines long in C? I guess it takes a couple of temp variables, though, which may dissuade some devs.
auto is_upper= (x > '@') & (x < '[');
return is_upper ? x+32 : x;
Which boils down to add, add, cmp, cmov on x86. It might save some cache traffic, but the cache traffic is less code since it's only a mov. The version that's code instead of lookups probably exacerbates register pressure problems in large programs? I don't have good intuition for which would be more efficient in practice.I dunno, it's ridiculously a benefit to the code by my instinct. While lookup table looks pretty bad.
I mean, I'm not skilled enough in those ISA extensions to stick my neck out. It's not totally obvious to me that there is not some shuffle or permute facility that can load 64 bytes at a time from LUTs.
By using SIMD instructions like AVX2 you can do 32 characters in parallel. With AVX512, 64.
Pretty easy to write something way faster by not using a LUT.
In fact, I'm pretty sure that Unicode cannot be solved with a lookup table due to its variable length, but maybe you can prove me wrong? (Assuming UTF8 here)
TIFFNoBitRevTable[256] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, ... 0xFF }
Maybe PSHUFB/VTBL to reverse each nibble and then put the reversed nibbles together would be wayyy faster?I'm guessing portability matters here, and there is not much commercial pressure to actually optimise this TIFF library?
The most used one is probably sin() on CPUs without hardware math