erts: add erlang:ctz/1 and erlang:popcount/1 BIFs - #11371
Conversation
CT Test Results 3 files 136 suites 56m 16s ⏱️ For more details on these failures, see this check. Results for commit 7b4efd8. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
35013c5 to
62cab4c
Compare
|
Thanks for the PR! We're going to review this once most of us are back from summer vacation. One thing that I'm sure will be brought up is that this is limited to 64 bits, whereas Erlang uses arbitrary precision arithmetic. A fast implementation would be relatively trivial as all three operations can be done in a data-parallel manner. |
cd6e095 to
4f1295c
Compare
|
@jhogberg thanks for the update. One CI action I cannot get to pass is this, which requires maintainers to modify the preloaded I added support for bignums in the commit below. |
Yes, we'll need to do that for you later on.
I don't like that it still assumes 64 bits, I also think the implementation is a bit too ambitious, I'd prefer just a dead-simple C implementation that ignores SIMD/ |
In the last commit, there's no more 64-bit implicit limitation, it just handles 64bit integers differently (built-in GCC) from bignums.
You are right, I didn't think about JIT. I will remove SIMD optimization, leaving only bignum support from the last commit. Or should I leave it for reference (I assume in JIT the same implementation would be needed), and remove once implemented in JIT? |
It assumes a bit width of 64, which doesn't really make sense for bignums (cf.
You can probably do that part yourself. Look at how (You can ignore the |
I see what you meant, and I agree, in presence of bignums the
I followed your hints, and added JIT implementation accompanied by test cases. |
5a8415d to
18ee827
Compare
|
@jhogberg, please review my latest changes, and if you could, please check for the two failing CI checks. I am not sure if the emulator failing test is related to my changes. |
fc3f642 to
6127578
Compare
…and BIF cases erts_ctz() and erts_popcount() now handle BOTH small 64-bit integers AND bignums with optimized fast paths: * Fast path: term_to_Uint64() extracts 64-bit values, uses __builtin_ctzll() and __builtin_popcountll() on ARCH_64, or dual-half processing on ARCH_32 * Slow path: Existing SIMD-optimized bignum logic (AVX-512 or ARM NEON)
"Get position of highest (set) bit" makes sense however, and can be used to implement
That implementation won't run, they're normal BIFs in implementation and Thanks for giving it a shot though, if we accept these, we'll take care of it. :-)
It looks unrelated to me too. |
Ok, so I'll leave it up to you to fill in the gaps if the team decides to merge. |
Adds functionality and tests for efficient bit operations on 64-bit values that implement compiler built-in functions.
ctz/1,popcount/1using compiler builtins with portable fallbackserl_bif_bitops.oto Makefile.in and regenerate build files