All of lore.kernel.org
 help / color / mirror / Atom feed
From: Emil Velikov <emil.l.velikov@gmail.com>
To: dri-devel@lists.freedesktop.org
Cc: emil.l.velikov@gmail.com
Subject: [PATCH libdrm 6/9] tests/random: extract test out of xf86drmRandom.c
Date: Sun, 22 Mar 2015 22:03:42 +0000	[thread overview]
Message-ID: <1427061825-27470-7-git-send-email-emil.l.velikov@gmail.com> (raw)
In-Reply-To: <1427061825-27470-1-git-send-email-emil.l.velikov@gmail.com>

With follow up commits we can clear it up and wire to
make check

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 tests/Makefile.am |   3 +-
 tests/random.c    | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drmRandom.c   |  67 ++--------------------------
 3 files changed, 132 insertions(+), 65 deletions(-)
 create mode 100644 tests/random.c

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 = $(top_builddir)/libdrm.la
 
 check_PROGRAMS = \
 	dristat \
-	drmstat
+	drmstat \
+	random
 
 if HAVE_NOUVEAU
 SUBDIRS += 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 "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * 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:
+ * 
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES 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.
+ * 
+ * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ * DESCRIPTION
+ *
+ * This file contains a simple, straightforward implementation of the Park
+ * & 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 technique
+ * 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 combining
+ * 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, 1981.
+ *
+ * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
+ * 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 Random
+ * Number Generators". CACM 36(7), July 1993, pp. 105-110.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#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;
+
+static void check_period(unsigned long seed)
+{
+    unsigned long count = 0;
+    unsigned long initial;
+    void          *state;
+    
+    state = drmRandomCreate(seed);
+    initial = drmRandom(state);
+    ++count;
+    while (initial != drmRandom(state)) {
+	if (!++count) break;
+    }
+    printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
+	   seed, count, count);
+    drmRandomDestroy(state);
+}
+
+int main(void)
+{
+    RandomState   *state;
+    int           i;
+    unsigned long rand;
+
+    state = drmRandomCreate(1);
+    for (i = 0; i < 10000; i++) {
+	rand = 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);
+    
+    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 <stdio.h>
 #include <stdlib.h>
 
-#define RANDOM_MAIN 0
-
-#if !RANDOM_MAIN
-# include "xf86drm.h"
-#endif
+#include "xf86drm.h"
 
 #define RANDOM_MAGIC 0xfeedbeef
 #define RANDOM_DEBUG 0
 
-#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;
 
-#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;
 
-    state           = RANDOM_ALLOC(sizeof(*state));
+    state           = drmMalloc(sizeof(*state));
     if (!state) return NULL;
     state->magic    = RANDOM_MAGIC;
 #if 0
@@ -140,7 +121,7 @@ void *drmRandomCreate(unsigned long seed)
 
 int drmRandomDestroy(void *state)
 {
-    RANDOM_FREE(state);
+    drmFree(state);
     return 0;
 }
 
@@ -164,45 +145,3 @@ double drmRandomDouble(void *state)
     
     return (double)drmRandom(state)/(double)s->m;
 }
-
-#if RANDOM_MAIN
-static void check_period(unsigned long seed)
-{
-    unsigned long count = 0;
-    unsigned long initial;
-    void          *state;
-    
-    state = drmRandomCreate(seed);
-    initial = drmRandom(state);
-    ++count;
-    while (initial != drmRandom(state)) {
-	if (!++count) break;
-    }
-    printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
-	   seed, count, count);
-    drmRandomDestroy(state);
-}
-
-int main(void)
-{
-    RandomState   *state;
-    int           i;
-    unsigned long rand;
-
-    state = drmRandomCreate(1);
-    for (i = 0; i < 10000; i++) {
-	rand = 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);
-    
-    return 0;
-}
-#endif
-- 
2.3.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  parent reply	other threads:[~2015-03-22 22:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-22 22:03 [PATCH libdrm 0/9] Rework remaining tests Emil Velikov
2015-03-22 22:03 ` [PATCH libdrm 1/9] tests/dristat: don't include C files Emil Velikov
2015-03-23 22:10   ` Jan Vesely
2015-03-26 14:59     ` Emil Velikov
2015-03-22 22:03 ` [PATCH libdrm 2/9] tests/hash: extract test out of xf86drmHash.c Emil Velikov
2015-03-23 22:27   ` Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 3/9] tests/hash: misc compilation fixes Emil Velikov
2015-03-23 22:42   ` Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 4/9] tests/hash: style fixes Emil Velikov
2015-03-23 22:46   ` Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 5/9] tests/hash: return non-zero on failure Emil Velikov
2015-03-23 23:08   ` Jan Vesely
2015-03-22 22:03 ` Emil Velikov [this message]
2015-03-24 22:10   ` [PATCH libdrm 6/9] tests/random: extract test out of xf86drmRandom.c Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 7/9] tests/random: return non-zero on test failure Emil Velikov
2015-03-24 22:12   ` Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 8/9] drm: replace HASH_DEBUG with DEBUG Emil Velikov
2015-03-24  0:01   ` Jan Vesely
2015-03-22 22:03 ` [PATCH libdrm 9/9] drm: use correct printf modifiers Emil Velikov
2015-03-24  0:04   ` Jan Vesely
2015-03-24 23:20 ` [PATCH libdrm 0/9] Rework remaining tests Jan Vesely
2015-03-26 15:14   ` Emil Velikov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1427061825-27470-7-git-send-email-emil.l.velikov@gmail.com \
    --to=emil.l.velikov@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.