From: Jan Vesely Subject: Re: [PATCH libdrm 6/9] tests/random: extract test out of xf86drmRandom.c Date: Tue, 24 Mar 2015 18:10:48 -0400 Message-ID: <1427235048.3547.94.camel@rutgers.edu> References: <1427061825-27470-1-git-send-email-emil.l.velikov@gmail.com> <1427061825-27470-7-git-send-email-emil.l.velikov@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0584095778==" Return-path: Received: from mail-qg0-f46.google.com (mail-qg0-f46.google.com [209.85.192.46]) by gabe.freedesktop.org (Postfix) with ESMTP id 4613B6E79B for ; Tue, 24 Mar 2015 15:10:50 -0700 (PDT) Received: by qgep97 with SMTP id p97so13672595qge.1 for ; Tue, 24 Mar 2015 15:10:49 -0700 (PDT) In-Reply-To: <1427061825-27470-7-git-send-email-emil.l.velikov@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: Emil Velikov Cc: dri-devel@lists.freedesktop.org List-Id: dri-devel@lists.freedesktop.org --===============0584095778== Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-/QYf4f2Zl0nWAvk3qeFr" --=-/QYf4f2Zl0nWAvk3qeFr Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, 2015-03-22 at 22:03 +0000, Emil Velikov wrote: > With follow up commits we can clear it up and wire to > make check >=20 > Signed-off-by: Emil Velikov > --- > tests/Makefile.am | 3 +- > tests/random.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++++ > xf86drmRandom.c | 67 ++-------------------------- > 3 files changed, 132 insertions(+), 65 deletions(-) > create mode 100644 tests/random.c >=20 > diff --git a/tests/Makefile.am b/tests/Makefile.am > index 392abf5..9b13b2e 100644 > --- a/tests/Makefile.am > +++ b/tests/Makefile.am > @@ -29,7 +29,8 @@ LDADD =3D $(top_builddir)/libdrm.la > =20 > check_PROGRAMS =3D \ > dristat \ > - drmstat > + drmstat \ > + random > =20 > if HAVE_NOUVEAU > SUBDIRS +=3D nouveau > diff --git a/tests/random.c b/tests/random.c > new file mode 100644 > index 0000000..6dc8386 > --- /dev/null > +++ b/tests/random.c > @@ -0,0 +1,127 @@ > +/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation > + * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com > + * > + * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. > + * All Rights Reserved. > + * > + * Permission is hereby granted, free of charge, to any person obtaining= a > + * copy of this software and associated documentation files (the "Softwa= re"), > + * to deal in the Software without restriction, including without limita= tion > + * the rights to use, copy, modify, merge, publish, distribute, sublicen= se, > + * and/or sell copies of the Software, and to permit persons to whom the > + * Software is furnished to do so, subject to the following conditions: > + *=20 > + * The above copyright notice and this permission notice (including the = next > + * paragraph) shall be included in all copies or substantial portions of= the > + * Software. > + *=20 > + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRE= SS OR > + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILI= TY, > + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SH= ALL > + * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAG= ES OR > + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, > + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR= OTHER > + * DEALINGS IN THE SOFTWARE. > + *=20 > + * Authors: Rickard E. (Rik) Faith > + * > + * DESCRIPTION > + * > + * This file contains a simple, straightforward implementation of the Pa= rk > + * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer > + * multiplicative linear congruential generator (MLCG) with a period of > + * 2^31-1. > + * > + * This implementation is intended to provide a reliable, portable PRNG > + * that is suitable for testing a hash table implementation and for > + * implementing skip lists. > + * > + * FUTURE ENHANCEMENTS > + * > + * If initial seeds are not selected randomly, two instances of the PRNG > + * can be correlated. [Knuth81, pp. 32-33] describes a shuffling techni= que > + * that can eliminate this problem. > + * > + * If PRNGs are used for simulation, the period of the current > + * implementation may be too short. [LE88] discusses methods of combini= ng > + * MLCGs to produce much longer periods, and suggests some alternative > + * values for A and M. [LE90 and Sch92] also provide information on > + * long-period PRNGs. > + * > + * REFERENCES > + * > + * [Knuth81] Donald E. Knuth. The Art of Computer Programming. Volume 2= : > + * Seminumerical Algorithms. Reading, Massachusetts: Addison-Wesley, 19= 81. > + * > + * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Numbe= r > + * Generators". CACM 31(6), June 1988, pp. 742-774. > + * > + * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10, > + * October 1990, pp. 85-97. > + * > + * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators= : > + * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201= . > + * > + * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit > + * CPUs". Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40. > + * > + * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer. In > + * "Technical Correspondence: Remarks on Choosing and Implementing Rando= m > + * Number Generators". CACM 36(7), July 1993, pp. 105-110. > + * > + */ > + > +#include > +#include > + > +#include "xf86drm.h" > + > +typedef struct RandomState { > + unsigned long magic; > + unsigned long a; > + unsigned long m; > + unsigned long q; /* m div a */ > + unsigned long r; /* m mod a */ > + unsigned long check; > + unsigned long seed; > +} RandomState; I think this should also go to an internal header. Otherwise lgtm > + > +static void check_period(unsigned long seed) > +{ > + unsigned long count =3D 0; > + unsigned long initial; > + void *state; > + =20 > + state =3D drmRandomCreate(seed); > + initial =3D drmRandom(state); > + ++count; > + while (initial !=3D drmRandom(state)) { > + if (!++count) break; > + } > + printf("With seed of %10lu, period =3D %10lu (0x%08lx)\n", > + seed, count, count); > + drmRandomDestroy(state); > +} > + > +int main(void) > +{ > + RandomState *state; > + int i; > + unsigned long rand; > + > + state =3D drmRandomCreate(1); > + for (i =3D 0; i < 10000; i++) { > + rand =3D drmRandom(state); > + } > + printf("After 10000 iterations: %lu (%lu expected): %s\n", > + rand, state->check, > + rand - state->check ? "*INCORRECT*" : "CORRECT"); > + drmRandomDestroy(state); > + > + printf("Checking periods...\n"); > + check_period(1); > + check_period(2); > + check_period(31415926); > + =20 > + return 0; > +} > diff --git a/xf86drmRandom.c b/xf86drmRandom.c > index 94922ad..39f3c52 100644 > --- a/xf86drmRandom.c > +++ b/xf86drmRandom.c > @@ -74,23 +74,11 @@ > #include > #include > =20 > -#define RANDOM_MAIN 0 > - > -#if !RANDOM_MAIN > -# include "xf86drm.h" > -#endif > +#include "xf86drm.h" > =20 > #define RANDOM_MAGIC 0xfeedbeef > #define RANDOM_DEBUG 0 > =20 > -#if RANDOM_MAIN > -#define RANDOM_ALLOC malloc > -#define RANDOM_FREE free > -#else > -#define RANDOM_ALLOC drmMalloc > -#define RANDOM_FREE drmFree > -#endif > - > typedef struct RandomState { > unsigned long magic; > unsigned long a; > @@ -101,18 +89,11 @@ typedef struct RandomState { > unsigned long seed; > } RandomState; > =20 > -#if RANDOM_MAIN > -extern void *drmRandomCreate(unsigned long seed); > -extern int drmRandomDestroy(void *state); > -extern unsigned long drmRandom(void *state); > -extern double drmRandomDouble(void *state); > -#endif > - > void *drmRandomCreate(unsigned long seed) > { > RandomState *state; > =20 > - state =3D RANDOM_ALLOC(sizeof(*state)); > + state =3D drmMalloc(sizeof(*state)); > if (!state) return NULL; > state->magic =3D RANDOM_MAGIC; > #if 0 > @@ -140,7 +121,7 @@ void *drmRandomCreate(unsigned long seed) > =20 > int drmRandomDestroy(void *state) > { > - RANDOM_FREE(state); > + drmFree(state); > return 0; > } > =20 > @@ -164,45 +145,3 @@ double drmRandomDouble(void *state) > =20 > return (double)drmRandom(state)/(double)s->m; > } > - > -#if RANDOM_MAIN > -static void check_period(unsigned long seed) > -{ > - unsigned long count =3D 0; > - unsigned long initial; > - void *state; > - =20 > - state =3D drmRandomCreate(seed); > - initial =3D drmRandom(state); > - ++count; > - while (initial !=3D drmRandom(state)) { > - if (!++count) break; > - } > - printf("With seed of %10lu, period =3D %10lu (0x%08lx)\n", > - seed, count, count); > - drmRandomDestroy(state); > -} > - > -int main(void) > -{ > - RandomState *state; > - int i; > - unsigned long rand; > - > - state =3D drmRandomCreate(1); > - for (i =3D 0; i < 10000; i++) { > - rand =3D drmRandom(state); > - } > - printf("After 10000 iterations: %lu (%lu expected): %s\n", > - rand, state->check, > - rand - state->check ? "*INCORRECT*" : "CORRECT"); > - drmRandomDestroy(state); > - > - printf("Checking periods...\n"); > - check_period(1); > - check_period(2); > - check_period(31415926); > - =20 > - return 0; > -} > -#endif --=20 Jan Vesely --=-/QYf4f2Zl0nWAvk3qeFr 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 iQIcBAABCgAGBQJVEeDoAAoJEDhUsXb6ZiH/bskQAI8hmbHSVAeb3WBauU5wQtIS n09G282WRbb87zJV/+oOiI98Vh/5kJrCjbZwMGVzatwKZ+ZuVtS3tQB4OzsEMqp0 Y7vNm0Hm0Md4AFlSQRnWfuUPGUP2ZI35oNuaZ0thVcq35u9Pp5pWl+rWNnFNnKzy FkkvmvYFT/F4QReHP/TFO0U2AMaYuLSAEFkynzr3NVZRrEmMsLhvGii5O1O7jKPJ bksrqiuNuFHocR5SXJx7mrTLnbfXY5bVFiGAgYG+F5pWKi4akbYP91Ur5Hv1XOjQ +wmfv5yHTzFGSIPR6TEMLgHaY5WXX6spu6gbdk321ijvFGTkUEFryYEKnG9LGO7V HB6srCe9KLd0/Rk21TGB9ShBQEmEtlMI06hQnj1KfSOdv6y1TuP0gvb5ENshDGnB cWSAaDcZBAEkNlPep5CdruNIHcgxUhQ/PZ8ME6uH8u2nU/cjnUxHQqRdfDG2Hh5c v3Z/V3EcDTl+8LMjoJoMhedJhqLUtzEpT652UnZOeJkx1p8wXLe+2hqqLIax7EOQ hQXfo4uykLmc6+h3YhurIFiV7lyClIf/PFX5s9ap8RzJDfWBzIisehtMZEf0DijM KXwrHlNo/h/Mhqjl39ljQoIqk5syORrjP2vixo+BiSaLQnZqcTgW8PdIGnglllGV Yz3wyJGEIfDkj/VkEDeQ =Do6a -----END PGP SIGNATURE----- --=-/QYf4f2Zl0nWAvk3qeFr-- --===============0584095778== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHA6Ly9saXN0 cy5mcmVlZGVza3RvcC5vcmcvbWFpbG1hbi9saXN0aW5mby9kcmktZGV2ZWwK --===============0584095778==--