News for hackers. Stuff that matters.
Re:“csinc”, the AArch64 instruction you didn’t know you wanted by jart on Wednesday June 07, 2023 @03:25AM (#36223911)
I discovered a really cool ARM64 trick today. One thing about x86 that I've found useful on so many occasions is the PCMPEQB + PMOVMSKB + BSF trick that lets me scan the bytes of a string 10x faster. I couldn't find any information on Google for doing PMOVMSKB with ARM, so I've been studying ARM's "Optimized Routines" codebase where I stumbled upon the answer in their strnlen() implementation. It turns out the trick is to use `shrn dst.8b, src.8h, 4` which turns a 128-bit mask into a 64-bit mask. You can then get the string offset index with fmov, rbit, clz and finally shift by 2.
Re:“csinc”, the AArch64 instruction you didn’t know you wanted by danlark on Wednesday June 07, 2023 @03:37AM (#36224001)
I am the author of this trick as well

You can read about it in https://community.arm.com/arm-community-blogs/b/infrastructu...

Re:“csinc”, the AArch64 instruction you didn’t know you wanted by jart on Wednesday June 07, 2023 @04:14AM (#36224267)
Wow. I love your work. Thank you for coming here and talking about it. You could write Hacker's Delight 2nd edition for a new generation.
Re:“csinc”, the AArch64 instruction you didn’t know you wanted by tr33house on Wednesday June 07, 2023 @05:38AM (#36224774)
would totally love to read a modern `Hacker's Delight`. My mind was so blown away the first time I learned about low-level optimizations. I wish I did more of that on a day to day
Re:“csinc”, the AArch64 instruction you didn’t know you wanted by spicymaki on Wednesday June 07, 2023 @11:18AM (#36227920)
Hacker's Delight already has a 2nd edition.

https://www.oreilly.com/library/view/hackers-delight-second/...

Re:“csinc”, the AArch64 instruction you didn’t know you wanted by zX41ZdbW on Wednesday June 07, 2023 @04:19AM (#36224293)
Let's add it to ClickHouse: https://github.com/ClickHouse/ClickHouse/blob/master/base/ba...

It should significantly improve the performance on ARM.

Re:“csinc”, the AArch64 instruction you didn’t know you wanted by jeffreygoesto on Wednesday June 07, 2023 @05:45AM (#36224805)
> However, developers often encounter problems with Arm NEON instructions being expensive to move to scalar code and back.

I remember talking to an ARM engineer easily 10 years ago and he told us in that nice british accent: "You know, NEON is like 'back in the yard'" :-D. This has changed a lot, but not enough from what you wrote... Bit sad that these SIMD optimizations are still hand written...

Re:“csinc”, the AArch64 instruction you didn’t know you wanted by Sesse__ on Wednesday June 07, 2023 @08:45AM (#36226090)
The VSHRN trick is nice (I used it only two hours ago!), but it really does feel like a crutch; I don't understand why they couldn't simply implement a PMOVMSKB-like instruction to begin with (it cannot possibly be very expensive in silicon, at least not if it moved into a vector register). One-bit-per-byte is really the sweet spot for almost any kind of text manipulation, and often requires less setup/post-fixup on either side of the POVMSKB/VSHRN.
Re:“csinc”, the AArch64 instruction you didn’t know you wanted by camel-cdr on Wednesday June 07, 2023 @04:37AM (#36224394)
I found the following article about the topic really good: https://branchfree.org/2019/04/01/fitting-my-head-through-th...

In my experience using a 512 wide movemask (to uint64_t) is the fastest on both x86 and arm64.

With rvv you don't really what to go from a vector mask to a general purpose non vector register, because the vector length may vary. But I found it really useful that vector masks are always packed into v0. So even with LMUL=8, you can just to a vmseq, switch to LMUL=1 and use vfirst & vmsbf to iterate through all indices.