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

minor speedup in pcg32_random_r() possible? #3

Open
jason-s opened this issue Apr 17, 2015 · 5 comments
Open

minor speedup in pcg32_random_r() possible? #3

jason-s opened this issue Apr 17, 2015 · 5 comments

Comments

@jason-s
Copy link

jason-s commented Apr 17, 2015

return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));

Wouldn't this be equivalent (and hence be slightly faster)?

return (xorshifted >> rot) | (xorshifted << (32-rot));

@imneme
Copy link
Owner

imneme commented Apr 17, 2015

No. Not when rot == 0. Your code would shift by 32, and that’s undefined behavior.

@imneme
Copy link
Owner

imneme commented Apr 17, 2015

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.

@jason-s
Copy link
Author

jason-s commented Apr 17, 2015

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.)

@imneme
Copy link
Owner

imneme commented Apr 17, 2015

Agreed!

@travisdowns
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants