Uh oh!
There was an error while loading. Please reload this page.
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.
You didn't check the carry bit to ensure that RDRAND succeeded. RDRAND can and does fail.
There was a problem hiding this comment.
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
nagisa
commented
Jul 10, 2016
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 |
jethrogb
commented
Jul 10, 2016
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. |
nagisa
commented
Jul 11, 2016
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. |
alexcrichton
commented
Jul 11, 2016
I agree with @nagisa that this is best done as an external crate, and as to whether or not it replaces |
pitdicker
commented
Jan 5, 2018
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 |
dhardy
commented
Jan 5, 2018
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 |
pitdicker
commented
Jan 5, 2018
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? |
pitdicker
commented
Mar 18, 2018
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
rdrandfeature to this crate. With this feature enabled,OsRngandThreadRngare replaced by an implementation that just calls the x86 RDRAND instruction. This also makes those interfaces available again inno_stdmode (see #108).The benefits of using
RDRANDare two-fold:Only works on nightly because of the need for inline assembly