From patchwork Thu Mar 26 23:57:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 6102771 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 916A3BF90F for ; Thu, 26 Mar 2015 23:54:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 45D8420443 for ; Thu, 26 Mar 2015 23:54:09 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 2B96F20451 for ; Thu, 26 Mar 2015 23:54:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 76BBE6EAD1; Thu, 26 Mar 2015 16:54:06 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wi0-f178.google.com (mail-wi0-f178.google.com [209.85.212.178]) by gabe.freedesktop.org (Postfix) with ESMTP id ED79D6EAD0 for ; Thu, 26 Mar 2015 16:54:04 -0700 (PDT) Received: by wibg7 with SMTP id g7so7839486wib.1 for ; Thu, 26 Mar 2015 16:54:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=A/rm98t6cu4uw3XweInxweHnVXBGBidEakjWaSlDWB8=; b=etd+5o7Txl9+C4ce5jj2Nu79F7+kgtjR3CHnMCPbHeQL4U7jYrPkMex+2y9vKgvL9q hzoezAmuEZG7xzEnqOM7zNmEsQgLypFBiIKCqmExXzFDI9N8LHBzS/sXPONNkz7A3KHm e7sivnQV6ZusQMblVfD35pgjAsfz4EEkr3qMLGwQ6krS0veDrIfRw/bFTpzf50TjAYn2 ONmyV0UfELW5p+S4gX5jT4H0nypbpryTCrvEzWkUhP5Pls2FvWvEJd33NLycg+DsG5hL i5qMzjnAKz8BRjHkz4Zo2KcGqY58BEvwgLErWgD3QHTab3kpzNNvIxgcBAvfo/toRTA7 pgTg== X-Received: by 10.194.80.193 with SMTP id t1mr33509659wjx.8.1427414044423; Thu, 26 Mar 2015 16:54:04 -0700 (PDT) Received: from arch-x220.localdomain (cpc12-croy20-2-0-cust52.croy.cable.virginm.net. [82.44.54.53]) by mx.google.com with ESMTPSA id ey10sm1060338wib.2.2015.03.26.16.54.03 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 26 Mar 2015 16:54:03 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm v2 5/8] tests/random: extract test out of xf86drmRandom.c Date: Thu, 26 Mar 2015 23:57:08 +0000 Message-Id: <1427414231-11385-5-git-send-email-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.3.1 In-Reply-To: <1427414231-11385-1-git-send-email-emil.l.velikov@gmail.com> References: <1427414231-11385-1-git-send-email-emil.l.velikov@gmail.com> Cc: emil.l.velikov@gmail.com X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With follow up commits we can clear it up and wire to make check v2: - Use xf86drmRandom.h for common struct.(Jan) - Add test to .gitignore. Signed-off-by: Emil Velikov --- .gitignore | 1 + Makefile.sources | 1 + tests/Makefile.am | 3 +- tests/random.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ xf86drmRandom.c | 78 ++---------------------------------- xf86drmRandom.h | 35 ++++++++++++++++ 6 files changed, 161 insertions(+), 75 deletions(-) create mode 100644 tests/random.c create mode 100644 xf86drmRandom.h diff --git a/.gitignore b/.gitignore index 0faa825..cc31f8f 100644 --- a/.gitignore +++ b/.gitignore @@ -81,6 +81,7 @@ tests/getversion tests/hash tests/lock tests/openclose +tests/random tests/setversion tests/updatedraw tests/modeprint/modeprint diff --git a/Makefile.sources b/Makefile.sources index ff4fe5e..f215747 100644 --- a/Makefile.sources +++ b/Makefile.sources @@ -3,6 +3,7 @@ LIBDRM_FILES := \ xf86drmHash.c \ xf86drmHash.h \ xf86drmRandom.c \ + xf86drmRandom.h \ xf86drmSL.c \ xf86drmMode.c \ xf86atomic.h \ diff --git a/tests/Makefile.am b/tests/Makefile.am index 79a13c4..e3443df 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..db341f9 --- /dev/null +++ b/tests/random.c @@ -0,0 +1,118 @@ +/* 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 + * + * 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 +#include + +#include "xf86drm.h" +#include "xf86drmRandom.h" + +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..b3a4be9 100644 --- a/xf86drmRandom.c +++ b/xf86drmRandom.c @@ -74,45 +74,17 @@ #include #include -#define RANDOM_MAIN 0 - -#if !RANDOM_MAIN -# include "xf86drm.h" -#endif +#include "xf86drm.h" +#include "xf86drmRandom.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; - unsigned long m; - unsigned long q; /* m div a */ - unsigned long r; /* m mod a */ - unsigned long check; - 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 +112,7 @@ void *drmRandomCreate(unsigned long seed) int drmRandomDestroy(void *state) { - RANDOM_FREE(state); + drmFree(state); return 0; } @@ -164,45 +136,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 diff --git a/xf86drmRandom.h b/xf86drmRandom.h new file mode 100644 index 0000000..43b730c --- /dev/null +++ b/xf86drmRandom.h @@ -0,0 +1,35 @@ +/* + * 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 + */ + +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;