News for hackers. Stuff that matters.
Re:Unsigned comparisons in AVX2/SSE: a quick note by sylware on Friday August 26, 2022 @08:36AM (#32606589)
Yep, some constants are better to be generated than loaded, see the docs from Mr. Agner.

On a more global scale, all assembly "optimisations"/tricks are hidden deep into compilers, which are reasonably "transparent" to their devs only, that due to their abysmal complexity and size.

We would need some sort of online library for those assembly (boolean/branchless calculus...) tricks.

A job for wikipedia? Maybe linked to the maths/boolean calculus?

Re:Unsigned comparisons in AVX2/SSE: a quick note by pjungwir on Friday August 26, 2022 @12:10PM (#32609254)
If you're interested in this, Hacker's Delight has several hundreds of pages of these tricks. The book picks up from a 1972 MIT memo called HAKMEM. For a database/web guy like me, reading it is just recreational, but that's probably true for most people here. I'm currently about halfway through one hundred pages on how to divide. :-)
Re:Unsigned comparisons in AVX2/SSE: a quick note by Const-me on Friday August 26, 2022 @12:58PM (#32609998)
I sometimes write optimized low-level code, but many of these books, and internet articles, are rather old. CPU architectures have converted to just two, AMD64 and ARM64. They have tons of useful instructions like sign extension and integer division.

They also have less trivial instructions equivalent to some of these tricks, only faster. Couple examples.

To turn off the rightmost 1-bit in a word, on AMD64 there’s BLSR instruction from BMI1 set.

ARM CPUs have a fast instruction to reverse bits. AMD64 do not, but they have a fast instruction to flip order of bytes allowing to reverse these bits faster than what’s in that book.

These tricks are only useful very rarely. For instance, SIMD instructions can’t divide vectors of integers. Some older GPUs can’t divide FP64 numbers but most of them can multiply FP64, and all of them can divide FP32. For these exotic use cases, these tricks are still relevant.

Re:Unsigned comparisons in AVX2/SSE: a quick note by muziq on Friday August 26, 2022 @03:42PM (#32611878)
If you’re not dividing greater than 2^23 numbers then can’t you just use the FP divide ? I’m sure I’ve done this in CUDA many years back.. Worked a treat :) If you are then you have my sympathies..
Re:Unsigned comparisons in AVX2/SSE: a quick note by Const-me on Friday August 26, 2022 @04:25PM (#32612350)
That’s often a good way, but not necessarily the best one. On my computer with Zen3 CPU, cvtdq2ps and cvttps2dq have 3 cycles of latency each, divps up to 10 cycles, results in 16 CPU cycles in total.

When the divisor’s the same for all lanes and known at compile time, there’re tricks to reduce division to a single integer multiplication, and a few extra cheap instructions like shifts and additions. Usually faster than 16 cycles. Here’s an example, automagically made by clang 13: https://godbolt.org/z/T4TKb7oov

Re:Unsigned comparisons in AVX2/SSE: a quick note by Const-me on Friday August 26, 2022 @09:12AM (#32607018)
I think the compiler tricks are reasonably transparent to any developer who knows both C and assembly, thanks to godbolt.org.

Couple times I even back-ported these tricks from clang-generated assembly back into C++ intrinsics.

Re:Unsigned comparisons in AVX2/SSE: a quick note by sylware on Saturday August 27, 2022 @08:35AM (#32618277)
godbolt is not really noscript/basic (x)html friendly... or I missed such www portal.
Re:Unsigned comparisons in AVX2/SSE: a quick note by janwas on Friday August 26, 2022 @03:36PM (#32611806)
github.com/google/highway is arguably such a collection of tricks, the x86_256-inl.h header does emulate some missing AVX-512 instructions such as this one or compressstoreu. (In this case we currently use a constant.)