From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Vesely Subject: Re: [libdrm][PATCH 1/2] random: Use unsigned long for seed Date: Mon, 09 Feb 2015 18:26:09 -0500 Message-ID: <1423524369.10898.56.camel@rutgers.edu> References: <1423517995-28251-1-git-send-email-jan.vesely@rutgers.edu> <54D93E99.4010107@freedesktop.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1381247180==" Return-path: Received: from mail-qg0-f54.google.com (mail-qg0-f54.google.com [209.85.192.54]) by gabe.freedesktop.org (Postfix) with ESMTP id 51C5C6E0E8 for ; Mon, 9 Feb 2015 15:26:12 -0800 (PST) Received: by mail-qg0-f54.google.com with SMTP id z60so23622081qgd.13 for ; Mon, 09 Feb 2015 15:26:11 -0800 (PST) In-Reply-To: <54D93E99.4010107@freedesktop.org> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: Ian Romanick Cc: dri-devel@lists.freedesktop.org List-Id: dri-devel@lists.freedesktop.org --===============1381247180== Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-TqUH3cXwyOGSoHrKm+cN" --=-TqUH3cXwyOGSoHrKm+cN Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, 2015-02-09 at 15:11 -0800, Ian Romanick wrote: > On 02/09/2015 01:39 PM, Jan Vesely wrote: > > This is more consistent with the rest, and avoids potential undefined > > behavior (signed overflow) in drmRandom() > >=20 > > Signed-off-by: Jan Vesely > > --- > > xf86drmRandom.c | 14 +++++++------- > > 1 file changed, 7 insertions(+), 7 deletions(-) > >=20 > > diff --git a/xf86drmRandom.c b/xf86drmRandom.c > > index ecab9e2..a084b86 100644 > > --- a/xf86drmRandom.c > > +++ b/xf86drmRandom.c > > @@ -98,7 +98,7 @@ typedef struct RandomState { > > unsigned long q; /* m div a */ > > unsigned long r; /* m mod a */ > > unsigned long check; > > - long seed; > > + unsigned long seed; > > } RandomState; > > =20 > > #if RANDOM_MAIN > > @@ -147,13 +147,13 @@ int drmRandomDestroy(void *state) > > unsigned long drmRandom(void *state) > > { > > RandomState *s =3D (RandomState *)state; > > - long hi; > > - long lo; > > + unsigned long hi; > > + unsigned long lo; > > =20 > > hi =3D s->seed / s->q; > > lo =3D s->seed % s->q; > > s->seed =3D s->a * lo - s->r * hi; > > - if (s->seed <=3D 0) s->seed +=3D s->m; > > + if ((s->a * lo) <=3D (s->r * hi)) s->seed +=3D s->m; >=20 > This seems like an unrelated change. This change is necessary. since s->seed is unsigned now it cannot be < 0. also the result of s->seed op s->q is unsigned. >=20 > > =20 > > return s->seed; > > } > > @@ -166,7 +166,7 @@ double drmRandomDouble(void *state) > > } > > =20 > > #if RANDOM_MAIN > > -static void check_period(long seed) > > +static void check_period(unsigned long seed) > > { > > unsigned long count =3D 0; > > unsigned long initial; > > @@ -178,7 +178,7 @@ static void check_period(long seed) > > while (initial !=3D drmRandom(state)) { > > if (!++count) break; > > } > > - printf("With seed of %10ld, period =3D %10lu (0x%08lx)\n", > > + printf("With seed of %10lu, period =3D %10lu (0x%08lx)\n", > > seed, count, count); > > drmRandomDestroy(state); > > } > > @@ -195,7 +195,7 @@ int main(void) > > } > > printf("After 10000 iterations: %lu (%lu expected): %s\n", > > rand, state->check, > > - rand - state->check ? "*INCORRECT*" : "CORRECT"); > > + rand !=3D state->check ? "*INCORRECT*" : "CORRECT"); >=20 > So does this. I'll drop this one in v2, it shouldn't have been included >=20 > > drmRandomDestroy(state); > > =20 > > printf("Checking periods...\n"); > >=20 >=20 --=20 Jan Vesely --=-TqUH3cXwyOGSoHrKm+cN Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAABCgAGBQJU2UIRAAoJEDhUsXb6ZiH/bDgP/1/aF+lKWgsevIwRzCQIAeVj fRSi2xfaCSXddihCOBSxosOwuenDb7BWZdamdER4YyReUIfddUrMTAoU+8xAN23V 6QI0fkuMzZNPzThMN4v1x8/hFAxvCkFZe8ZawPj+bMSu/MkVPnSuzOsr/kdjyqMO VMFUSXyH94z0kwh6mMAnd0Sz675FnMgTlZ6xA9CkjVVQMnKcfur/3mPfglAHwNPS jsYl1vlEiM0af/wbcOwKEzuVWFO/OIKTVK66MF/bVKOvx6Cxp0clcyxmsjzwaUMO yZ+dliiT6SqrNxfRqylXvQpm2lF7jZ5eD7Pew3CJL72G98JjfAJxW7VsBpE/JjlI Fp33A2h5zPT8oYubrYH38d29+AS9GK9PgIB++PcFGAuru68/rHxWWIiR119BvbYy 12zPDIT139RBomz/cXTUOA3FW9dcZZp868ibBBtkFdILyYZBCGBeiW/izrDdSCV2 WJGTfPyDYhK7YKGkbjMsD6s9JQ4b3jl6dlZyAnIi0/FvBasIajriNqBWiyzgLwPy SMVGZWpatyKq+IkQIy1hrHEU2mXC4JOH3WM7Tgk+HDqPW9yR31JPSzHZSXmlKnTN JHxweo48dm8EQXNQJ/IcgF8fdZSCvX/qcaSvYfne2ZmWr60S8xMxIsYHDb49jzNO m/CNeyOBlZIk84qqHZu8 =exKX -----END PGP SIGNATURE----- --=-TqUH3cXwyOGSoHrKm+cN-- --===============1381247180== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHA6Ly9saXN0 cy5mcmVlZGVza3RvcC5vcmcvbWFpbG1hbi9saXN0aW5mby9kcmktZGV2ZWwK --===============1381247180==--