zig icon indicating copy to clipboard operation
zig copied to clipboard

Implement `@depositBits` and `@extractBits`

Open ominitay opened this issue 1 year ago • 5 comments

Implements @depositBits and @extractBits, corresponding to pdep and pext from BMI2. The builtins are supported in the LLVM backend for any integer width, are supported in the x86 backend for integer widths below and including 64 bits, and are evaluated at compile-time when possible (also at any integer width). The functionality remains unimplemented in all other backends, and is out of the scope of this PR.

Apologies for how long it has taken me to find the time to get this merge-ready, but I'm happy for it to be merged now, and any further enhancements to be split out into further issues.

Continuation of #15285 Closes #14995

ominitay avatar Jan 25 '24 13:01 ominitay

All checks passed, happy for this to be merged if approved :)

ominitay avatar Jan 25 '24 23:01 ominitay

Rebased onto master.

ominitay avatar Feb 14 '24 12:02 ominitay

Converted to draft as there are a couple of changes I'm part-way through making to this PR. Should be ready for review in a week or two.

ominitay avatar Mar 22 '24 03:03 ominitay

Emulation on unsupported targets and large int types has been moved to compiler-rt. Yet to implement calls to this from other backends, but x86 backend should now pass behaviour tests. Before ready for review, I'll look at implementing testing of the various implementations of this against each other to each other with random integers, as we have 4 of them: the two compiler-rt methods (generic and bigint), the comptime bigint, and the native instruction call, so any latent bugs I could have missed should be detected. This is kind of building off of @matu3ba's suggestions: https://github.com/ziglang/zig/pull/15285#discussion_r1167245563

ominitay avatar Mar 29 '24 19:03 ominitay

Disabled the failing behaviour tests caused by the bug in the x86 backend. My rationale here is that it's not within the scope of this PR to work around, as a user needing to use this functionality could simply just not use the x86 backend.

ominitay avatar Apr 17 '24 00:04 ominitay