-
Notifications
You must be signed in to change notification settings - Fork 432
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
Add RDRAND feature #109
Add RDRAND feature #109
Conversation
impl Rng for OsRng { | ||
fn next_u32(&mut self) -> u32 { | ||
let ret; | ||
unsafe{asm!("rdrand $0":"=r"(ret):::"volatile")}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't check the carry bit to ensure that RDRAND succeeded. RDRAND can and does fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, good catch, thanks. I need to update some code elsewhere too... Fixed in 602551c
* Add dependency on core_io * Change mentions of std to core * Cfg-out everything that doesn't work in core
First and foremost, the design of the rand crate does not preclude having whatever random generation algorithms outside of rand crate itself. The availability of things like Second, this crate must compile with stable rustc, and Finally, a shameless plug, the rdrand crate provides such implementation of |
Many parts of Rust features a lack of dependency injection though. There are other things that rely on
And I do argue that. Another solution that does work on stable is to use the gcc crate with external assembly but that will probably be slower and IMO adds a lot of unnecessary dependencies. |
You can swap out the whole crates, though (via --extern), so if you really rdrand to be used for your generation purposes, you can just replace
I have measurements:
As you can see overhead of a call-per-number is less than the variance (i.e. is invisible). The only fair point is that you have dependencies on more crates/external tools/etc. |
I agree with @nagisa that this is best done as an external crate, and as to whether or not it replaces |
The @jethrogb Part of your motivation here is to have |
I'm fine if it's not implemented directly but instead depends on @nagisa's crate.
I'm not sure what you mean by this. There are many different reasons why you'd be using If the architecture you're running on doesn't have RDRAND, this PR is not going to help you, no. If on the other hand you're running |
I'm not very knowledgeable about this, and I don't believe pitdicker is either. Would you care to explain? WASM would be another possible More generally, it would be useful to be able to configure |
True 😄, and also not very knowledgeable about many things.
This sounds like the same problem custom allocators have. Would a similar solution help?
Am I missing something big? Would this also help with embedded and WASM? |
I think we agree that this PR is not the way we want to go, but that there is a real problem with |
I expect this to be somewhat controversial.
This PR adds the
rdrand
feature to this crate. With this feature enabled,OsRng
andThreadRng
are replaced by an implementation that just calls the x86 RDRAND instruction. This also makes those interfaces available again inno_std
mode (see #108).The benefits of using
RDRAND
are two-fold:Only works on nightly because of the need for inline assembly