Stdlib: add popcount, nlz, nls, ntz and related bit count functions
#14433
+140
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds classic bit counting functions to the
Int,Int32,Int64andNativeintstandard library modules. (OnlyInt64is implemented at this point; I'll do the other modules when we agree on the API, implementation, and documentation.)The new functions are the classic
popcount: population count / Hamming weightnlz: number of leading zerosntz: number of trailing zerosas well as three derived functions that I find interesting as well:
nls: number of leading sign bitsunsigned_bitsize: number of bits required for an unsigned binary representationsigned_bitsize: number of bits required for a signed, two's complement binary representation.The implementation is pure OCaml, using Hacker's Delight algorithms. I know we can get better performance using C with built-in functions, or inline asm, but speed is not the main goal of this PR. The emphasis here is on providing the basic functionality in the stdlib and on fixing the API.