-
Notifications
You must be signed in to change notification settings - Fork 2
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
Replace convert_slice_{32,64}
with read_u{32,64}_into
#77
Conversation
And a little cleanup around the init functions
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.
Sorry, my comments are either feature creep or something we should have picked up before. I know from_rng
is often used with fresh entropy, but it can be used to seed from a deterministic master PRNG, so I think it should be portable. Agree?
/// Reads unsigned 32 bit integers from `src` into `dst`. | ||
/// Borrowed from the `byteorder` crate. | ||
#[inline] | ||
pub fn read_u32_into(src: &[u8], dst: &mut [u32]) { |
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.
Code looks fine. Oh, you copied the name from byteorder
? I guess it'll do.
src/prng/chacha.rs
Outdated
unsafe { | ||
let ptr = seed.as_mut_ptr() as *mut u8; | ||
let slice = slice::from_raw_parts_mut(ptr, SEED_WORDS * 4); | ||
other.try_fill(slice)?; |
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're converting from bytes to u32
here. I think this will produce different results on BE CPUs? We went to all that trouble to make sure fill_bytes
is portable, so I think this should be too. Maybe the best option is to use from_seed
here :/
src/prng/hc128.rs
Outdated
unsafe { | ||
let ptr = seed.as_mut_ptr() as *mut u8; | ||
let slice = slice::from_raw_parts_mut(ptr, 8 * 4); | ||
let slice = slice::from_raw_parts_mut(ptr, SEED_WORDS * 4); |
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.
Same as ChaCha: watch Endianness
src/prng/isaac.rs
Outdated
unsafe { | ||
let ptr = key.as_mut_ptr() as *mut u8; | ||
let ptr = seed.as_mut_ptr() as *mut u8; |
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.
I guess here too
src/prng/isaac64.rs
Outdated
unsafe { | ||
let ptr = key.as_mut_ptr() as *mut u8; | ||
let ptr = seed.as_mut_ptr() as *mut u8; |
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.
and here
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.
All four are now share the same code 😄
I am not sure what you mean with feature creep.
I would argue that we should be able to rely on But making I played with |
I suppose we can just document that Of course both |
Thinking some more about it, a function that just works without pitfalls is worth more than a bit simpler implementation, or optimization on uncommon architectures (which are probably unmeasurable small for I will update the What did you mean with feature creep? |
My comments about requiring If we make Currently PRNGs implement |
But I agree this are minor advantages. I leave the call to you. Shall I leave |
No, your arguments make sense. Aside from one thing: surely the seed buffer passed to By portable I mean produces the same results everywhere, including in future versions. |
But is LLVM able to figure that out? If the functions are not inlined it has no choice but use slower unaligned copy functions. But that is something to try and play with, probably we can figure something out. Or just measure the impact, which may be tiny. |
I have added a commit that folds With some extra bounds on In Xorshift In |
Hang on, weren't you just arguing that |
I though you were thinking in the opposite direction, and I saw these advantages only as 'minor' 😄. How do you think it turned out? We do not seem to lose much, because it is possible to override the default implementation. |
The most significant change is that With specialisation we should be able to do a default implementation of |
Yes, true. I don't think it will cause big problems though. There is good documentation, requiring deterministic numbers is (I think) not that common, and the fix is to just import the right RNG. And now we have one less trait for users of |
In that case, is there even much point in having This brings up another point, the rationale for Note: almost all of this still needs merging into upstream, and needs some rationale. |
And I think we can say I am not sure if this is the right way to think about it, but to me the available methods in an API tell me how it is supposed to be used.
Not checked yet, but |
Good point about the error handling. All the distributions and Adding Ok, so we do want |
And a little cleanup around the init functions