optimize udivmod - #15265
Conversation
| @setRuntimeSafety(is_test); | ||
| if (T == u64 and builtin.target.cpu.arch == .x86_64) { | ||
| var rem: T = undefined; | ||
| const quo = asm ( |
There was a problem hiding this comment.
This nets 10% perf improvement over the generic for x86. There are likely similar instructions for other architectures but don't have available for testing.
There was a problem hiding this comment.
Additionally this could be used for udivmoddi and the 64-bit by 32-bit divisions but have left these to take the generic path since I have not tested these.
| const quo = asm ( | ||
| \\divq %[v] | ||
| : [_] "={rax}" (-> T), | ||
| [_] "={rdx}" (rem), |
There was a problem hiding this comment.
This ideally would be [_] "={rdx}" (*r) and we wouldn't need the temporary, however the assembly syntax seems subtly different so this valid construct in C inline asm is not valid in zig.
|
CI failure unrelated. Will rerun when the c main function definition is fixed. |
6cb2ffd to
4c06ffb
Compare
matu3ba
left a comment
There was a problem hiding this comment.
As I understand it, divwide_generic assumes register size of 64 bit, which should be added as assumption and might create suboptimal code for those platforms.
See https://electronics.stackexchange.com/questions/499329/physical-size-of-the-registers-for-x86 for context.
|
Windows fails due to inline asm blocks not being supported for bootstrapping with msvc: Not sure, what the best workaround is. |
This should still work for 32-bit and the Lines 103 to 105 in e2fe190 You are right in that it probably isn't optimal but it shouldn't be worse than the current implementation.
Not sure on this either but if no suggestions on an easy fix I'll disable when the target is windows temporarily. |
4c06ffb to
a950c04
Compare
See https://reviews.llvm.org/D81809 for upstream description. In summary this is ~10x improvement for small divisors and similar performance for equal divisors. Closes ziglang#13523.
a950c04 to
59a6f48
Compare
|
Great work! |
See https://reviews.llvm.org/D81809 for upstream description.
In summary this is ~10x improvement for small divisors and similar performance for equal divisors.
Closes #13523.
As a sanity, check, the below benchmark reinforces the visibly slow latency for small divisors:
Some additional reading for those interested: https://danlark.org/2020/06/14/128-bit-division/