[FFmpeg-devel] [PATCHv2 2/2] lavu/lfg: switch to Ziggurat algorithm for normal random number generation (WIP)

Reimar Döffinger Reimar.Doeffinger at gmx.de
Sat Mar 12 17:42:01 CET 2016


On Sat, Mar 12, 2016 at 11:11:32AM -0500, Ganesh Ajjanagadde wrote:
> On Sat, Mar 12, 2016 at 11:02 AM, Michael Niedermayer
> <michael at niedermayer.cc> wrote:
> >> +static inline double ziggurat(AVLFG *lfg)
> >> +{
> >> +    while (1) {
> >
> >> +        uint64_t r = (((uint64_t)av_lfg_get(lfg) << 32) + av_lfg_get(lfg)) & 0x000fffffffffffff;
> >
> > the order of Function evaluations is undefined here i think
> 
> And why would that matter? Aren't these just approximations to truly
> uniform 32 bits? rand()<<32 + rand() is common:

Because you generally don't want the randomness to include the
compiler/compiler options used.
You can end up with a case where a bug only happens when you get
a specific value.
To debug you compile with -O1. Now the compiler changes the order
here and suddenly the bug is gone!
So I think the more appropriate question would be: why is having
them in a single line so important that you would risk doing
this to yourself or a fellow developer?
Personally I am fan of splitting things into lines anyway when
the cost is minimal, like
uint64_t r = (uint64_t)av_lfg_get(lfg) << 32;
r += av_lfg_get(lfg);
r &= 0x000fffffffffffff;


More information about the ffmpeg-devel mailing list