From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTP id 7EDC948B42
	for <ffmpegdev@gitmailbox.com>; Wed,  3 Jan 2024 19:27:52 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 31EAB68CC5B;
	Wed,  3 Jan 2024 21:27:49 +0200 (EET)
Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net
 [217.70.183.196])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C656968C8C4
 for <ffmpeg-devel@ffmpeg.org>; Wed,  3 Jan 2024 21:27:42 +0200 (EET)
Received: by mail.gandi.net (Postfix) with ESMTPSA id A018FE0003
 for <ffmpeg-devel@ffmpeg.org>; Wed,  3 Jan 2024 19:27:41 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=niedermayer.cc;
 s=gm1; t=1704310061;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 in-reply-to:in-reply-to:references:references;
 bh=eMcMsI8Px0qMZLB41bRSk+RVxuiGzYUw3bEdu1JHSy4=;
 b=oSFUQdJQHpsCYeCazBobCpCCQP+q77nyinc1QFQE5YirBEovW8GYNsFRtBjxi9mmDsImHy
 ZF36xzgJyG243dzqErgFVxQWGmgDK1f+PKpQ9RsKQqfwoqlEUT8BjTfrcMnCU3/PBDCn+H
 t1x0B5jv9ysOdS7dA75NynwCEM+VIPPEB8NJHo2xSGhkgBZUFGCKDLqbRQHgY4CGOJ1StL
 sln/il0FxxrFi0rIzM6VTAn+kb6+369Hvo8hfKb5gLFxrfnvcq4zZn8egKKL3ahazG2FfO
 jVn8RhHbF4VVX+CtJIY+x0T/oP5P5A3iQM3sgQdCp5ZmMjx+NSq22iMf2UXt9w==
Date: Wed, 3 Jan 2024 20:27:40 +0100
From: Michael Niedermayer <michael@niedermayer.cc>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Message-ID: <20240103192740.GZ6420@pb2>
References: <20240102022845.9916-1-michael@niedermayer.cc>
 <ZZPuNSP4EnmaOB8Q@mariano>
MIME-Version: 1.0
In-Reply-To: <ZZPuNSP4EnmaOB8Q@mariano>
X-GND-Sasl: michael@niedermayer.cc
Subject: Re: [FFmpeg-devel] [PATCH] avutil/eval: Use integer for random()
 state
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: multipart/mixed; boundary="===============4277003547366189375=="
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20240103192740.GZ6420@pb2/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>


--===============4277003547366189375==
Content-Type: multipart/signed; micalg=pgp-sha256;
	protocol="application/pgp-signature"; boundary="dXJXs/M7/ajzpbcK"
Content-Disposition: inline


--dXJXs/M7/ajzpbcK
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jan 02, 2024 at 12:06:29PM +0100, Stefano Sabatini wrote:
> On date Tuesday 2024-01-02 03:28:45 +0100, Michael Niedermayer wrote:
> > rounding the 64bit integer state to double between each iteration
> > causes a reduction in quality of the random number generator.
> > For example its period drops from 2^64 to around 200 million
> >=20
> > Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
> > ---
> >  libavutil/eval.c | 16 +++++++++++++---
> >  1 file changed, 13 insertions(+), 3 deletions(-)
> >=20
> > diff --git a/libavutil/eval.c b/libavutil/eval.c
> > index bad9e4ecb8d..89c61ba4bf5 100644
> > --- a/libavutil/eval.c
> > +++ b/libavutil/eval.c
> > @@ -55,6 +55,7 @@ typedef struct Parser {
> >      void *log_ctx;
> >  #define VARS 10
> >      double *var;
> > +    uint64_t *var_uint64;
> >  } Parser;
> > =20
> >  static const AVClass eval_class =3D {
> > @@ -173,6 +174,7 @@ struct AVExpr {
> >      } a;
> >      struct AVExpr *param[3];
> >      double *var;
> > +    uint64_t *var_uint64;
> >  };
> > =20
> >  static double etime(double v)
> > @@ -230,9 +232,10 @@ static double eval_expr(Parser *p, AVExpr *e)
> >          }
> >          case e_random:{
> >              int idx=3D av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
> > -            uint64_t r=3D isnan(p->var[idx]) ? 0 : p->var[idx];
> > +            uint64_t r=3D p->var_uint64[idx] ? p->var_uint64[idx] : (i=
snan(p->var[idx]) ? 0 : p->var[idx]);
> >              r=3D r*1664525+1013904223;
> >              p->var[idx]=3D r;
> > +            p->var_uint64[idx]=3D r;
> >              return e->value * (r * (1.0/UINT64_MAX));
> >          }
> >          case e_while: {
> > @@ -319,7 +322,11 @@ static double eval_expr(Parser *p, AVExpr *e)
> >                  case e_div: return e->value * (d2 ? (d / d2) : d * INF=
INITY);
> >                  case e_add: return e->value * (d + d2);
> >                  case e_last:return e->value * d2;
> > -                case e_st : return e->value * (p->var[av_clip(d, 0, VA=
RS-1)]=3D d2);
> > +                case e_st :  {
> > +                    int index =3D av_clip(d, 0, VARS-1);
>=20
> > +                    p->var_uint64[index] =3D 0;
>=20
> can't you set this to d2 and then in case e_random:
>=20
> uint64_t r =3D isnan(p->var[idx]) ? 0 : p->var_uint64[idx];
>=20
> ?

setting var_uint64[] to d2 would require us to check for non representable
cases like outside 0..UINT64_MAX and NAN.
We would have to do this for every st() even though very few actually get u=
sed
as seeds. So it would slow it down.

if we set it to 0, then we only need to do these checks in the first call t=
o random
(or just require the user not to use random with such values)
subsequent calls use integers and need not check the double value

thx

[...]
--=20
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The day soldiers stop bringing you their problems is the day you have stopp=
ed=20
leading them. They have either lost confidence that you can help or conclud=
ed=20
you do not care. Either case is a failure of leadership. - Colin Powell

--dXJXs/M7/ajzpbcK
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iF0EABEIAB0WIQSf8hKLFH72cwut8TNhHseHBAsPqwUCZZW1JgAKCRBhHseHBAsP
q+3OAJwK8GcGe5EjlHJh18IMOB7CC8B+zgCglvYBamsQiQ4txcP7cCBlfHyuWZQ=
=qH7U
-----END PGP SIGNATURE-----

--dXJXs/M7/ajzpbcK--

--===============4277003547366189375==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

--===============4277003547366189375==--