zig icon indicating copy to clipboard operation
zig copied to clipboard

Implement @depositBits and @extractBits

Open bnuuydev opened this issue 1 year ago • 6 comments

This PR takes #18680, updates the changes to master, and fixes the outstanding issues which prevented it from being merged.

There currently are outstanding issues related to the COFF linker which are blocking progress in the x86 backend:

  • #19498 causes causing calls to the compiler-rt functions for u128 to crash the compiler when using the x86 backend to target Windows.
  • All calls to the compiler-rt functions for pext and pdep appear to invoke a bug in the COFF linker, causing these symbols to be linked from a non-existent DLL called .dll.

I have been told that moving on and just skipping the failing tests is the appropriate course of action here.

The comptime implementation calls into big.int.depositBits/extractBits, which themselves call the corresponding builtins. This allows for significantly better performance at comptime, but obviously requires zig1.wasm to be updated. I understand that a core member will be required to redo this commit for me before this can be merged?

The C backend implementation for integer sizes above u64 is blocked by #19991. I am subscribed to this issue, and once fixed, I have a work-in-progress implementation ready to be completed and included in a follow-up PR. Up-to u64, the C backend implementation works fine, and is sufficent for bootstrapping the compiler.

I've included a number of behaviour tests to ensure that the implementations are correct.

Many thanks to @Rexicon226, @jacobly0 and others for your extensive help :)

Closes #14995

bnuuydev avatar Apr 05 '25 21:04 bnuuydev

There's currently a copy-paste in compiler-rt waiting for the required functions to be moved to std/Target.zig. Once that change is made, I'll fix that commit up.

bnuuydev avatar Apr 05 '25 21:04 bnuuydev

There is scope for a follow-up PR to optimise the codegen for u128 and above to compile down to multiple invocations of pdep and pext. I intend to tackle that at a later date :)

bnuuydev avatar Apr 05 '25 21:04 bnuuydev

I think I've addressed those all now :) I'll take a look at any feedback you've given tomorrow. Thank you for the help so-far! Also it'd probably be a good idea to give the workflows approval in-case any tests aren't passing on platforms I haven't been able to test for :) (Although I imagine you don't want to be running a third-party zig1.wasm on the CI?)

bnuuydev avatar Apr 06 '25 00:04 bnuuydev

Fixed the faulty assumption that the source and mask would have the same number of limbs in the big.int implementation. Also added a couple more tests to confirm we have the correct behaviour.

bnuuydev avatar Apr 06 '25 13:04 bnuuydev

Currently away from my computer, will get these changes addressed in a week or two :)

bnuuydev avatar Apr 11 '25 23:04 bnuuydev

Other reviews have been addressed, will be able to take a proper stab at the reviews of big.int in about 2 weeks once I have some more spare time after exams.

Edit: Got bored and did it ^_^

bnuuydev avatar May 25 '25 19:05 bnuuydev

Just a gentle ping — the 0.15 release is scheduled for August. Is there any chance this PR could make it in time?

lin72h avatar Jul 07 '25 06:07 lin72h

Kind of just waiting for another review of things. Fingers crossed that we could get it merged in time, but focus is probably elsewhere for the core team right now. I'll give things another look this weekend. Not the end of the world if we miss 0.15.

bnuuydev avatar Jul 08 '25 20:07 bnuuydev