-
Notifications
You must be signed in to change notification settings - Fork 51
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
minor speedup in pcg32_random_r() possible? #3
Comments
No. Not when rot == 0. Your code would shift by 32, and that’s undefined behavior. |
FWIW, when the compiler's idiom-detection is working properly, it should look at this code and say “Oh, that's an idiomatic C/C++ rotate, I'll compile that to a single rotate instruction”. If the optimizer doesn't do that, it's not really doing its job and should be reported as a bug. |
ah -- those darn standards writers. Thanks! (editorial comment: Seems like the C standard library should have contained a rotate intrinsic, rather than forcing programmers to jump through tricky hoops to do what they really want.) |
Agreed! |
@jason-s - in this case I think the standard writers chose a reasonable tack: ISA do actually differ in what they do when do a shift by 32. Some make the result zero, x86 leaves the result unchanged (effectively, the shift amount is taken mod 32), ARM is a mix of both. |
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
Wouldn't this be equivalent (and hence be slightly faster)?
return (xorshifted >> rot) | (xorshifted << (32-rot));
The text was updated successfully, but these errors were encountered: