Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize square root instruction #221

Open
edubart opened this issue Apr 2, 2024 · 0 comments
Open

Optimize square root instruction #221

edubart opened this issue Apr 2, 2024 · 0 comments
Labels
enhancement New feature or request optimization Optimization

Comments

@edubart
Copy link
Contributor

edubart commented Apr 2, 2024

Context

Currently fsqrt.d is one of the most slow instructions that can be called from userspace, for instance it is roughly 3x slower than other floating point instructions, we could improve it. For dapps running untrusted RISC-V code, this function could be abused to make the dapp validation intentionally slower.

Measurements:

fsqrt.d                                        60.309 MIPS    42852 ucycles
fmul.d                                        152.402 MIPS     6964 ucycles
mul                                           494.966 MIPS     6434 ucycles

I added other instructions speed as reference. Also sqrt seems to be taking a large number of microarchitecture cycles.

EDIT: Seems like fdiv.d causes a iterations of 128 loops in uarch due to our 128bit implementation, we could also optimize that.

Possible solutions

Our current implementation is using Newton's method to find the square root, with many iterations. Seems like "Berkeley Softfloat" gets away without for loops, using fast invert square root, possible inspired by the famous Quake's fast invert square root. We could investigate how this is done, removing for loops would be the ideal case for running in microarchitecture.

@edubart edubart added enhancement New feature or request optimization Optimization labels Apr 2, 2024
@edubart edubart self-assigned this Apr 2, 2024
@edubart edubart removed their assignment Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request optimization Optimization
Projects
Status: Todo
Development

No branches or pull requests

1 participant