Blame xf86drmRandom.c

Packit Service 103f6b
/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
Packit Service 103f6b
 * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
Packit Service 103f6b
 *
Packit Service 103f6b
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
Packit Service 103f6b
 * All Rights Reserved.
Packit Service 103f6b
 *
Packit Service 103f6b
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 103f6b
 * copy of this software and associated documentation files (the "Software"),
Packit Service 103f6b
 * to deal in the Software without restriction, including without limitation
Packit Service 103f6b
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 103f6b
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 103f6b
 * Software is furnished to do so, subject to the following conditions:
Packit Service 103f6b
 * 
Packit Service 103f6b
 * The above copyright notice and this permission notice (including the next
Packit Service 103f6b
 * paragraph) shall be included in all copies or substantial portions of the
Packit Service 103f6b
 * Software.
Packit Service 103f6b
 * 
Packit Service 103f6b
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 103f6b
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 103f6b
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit Service 103f6b
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit Service 103f6b
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit Service 103f6b
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit Service 103f6b
 * DEALINGS IN THE SOFTWARE.
Packit Service 103f6b
 * 
Packit Service 103f6b
 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
Packit Service 103f6b
 *
Packit Service 103f6b
 * DESCRIPTION
Packit Service 103f6b
 *
Packit Service 103f6b
 * This file contains a simple, straightforward implementation of the Park
Packit Service 103f6b
 * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
Packit Service 103f6b
 * multiplicative linear congruential generator (MLCG) with a period of
Packit Service 103f6b
 * 2^31-1.
Packit Service 103f6b
 *
Packit Service 103f6b
 * This implementation is intended to provide a reliable, portable PRNG
Packit Service 103f6b
 * that is suitable for testing a hash table implementation and for
Packit Service 103f6b
 * implementing skip lists.
Packit Service 103f6b
 *
Packit Service 103f6b
 * FUTURE ENHANCEMENTS
Packit Service 103f6b
 *
Packit Service 103f6b
 * If initial seeds are not selected randomly, two instances of the PRNG
Packit Service 103f6b
 * can be correlated.  [Knuth81, pp. 32-33] describes a shuffling technique
Packit Service 103f6b
 * that can eliminate this problem.
Packit Service 103f6b
 *
Packit Service 103f6b
 * If PRNGs are used for simulation, the period of the current
Packit Service 103f6b
 * implementation may be too short.  [LE88] discusses methods of combining
Packit Service 103f6b
 * MLCGs to produce much longer periods, and suggests some alternative
Packit Service 103f6b
 * values for A and M.  [LE90 and Sch92] also provide information on
Packit Service 103f6b
 * long-period PRNGs.
Packit Service 103f6b
 *
Packit Service 103f6b
 * REFERENCES
Packit Service 103f6b
 *
Packit Service 103f6b
 * [Knuth81] Donald E. Knuth. The Art of Computer Programming.  Volume 2:
Packit Service 103f6b
 * Seminumerical Algorithms.  Reading, Massachusetts: Addison-Wesley, 1981.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
Packit Service 103f6b
 * Generators".  CACM 31(6), June 1988, pp. 742-774.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
Packit Service 103f6b
 * October 1990, pp. 85-97.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
Packit Service 103f6b
 * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
Packit Service 103f6b
 * CPUs".  Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
Packit Service 103f6b
 *
Packit Service 103f6b
 * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer.  In
Packit Service 103f6b
 * "Technical Correspondence: Remarks on Choosing and Implementing Random
Packit Service 103f6b
 * Number Generators". CACM 36(7), July 1993, pp. 105-110.
Packit Service 103f6b
 *
Packit Service 103f6b
 */
Packit Service 103f6b
Packit Service 103f6b
#include <stdio.h>
Packit Service 103f6b
#include <stdlib.h>
Packit Service 103f6b
Packit Service 103f6b
#include "libdrm_macros.h"
Packit Service 103f6b
#include "xf86drm.h"
Packit Service 103f6b
#include "xf86drmRandom.h"
Packit Service 103f6b
Packit Service 103f6b
#define RANDOM_MAGIC 0xfeedbeef
Packit Service 103f6b
Packit Service 103f6b
drm_public void *drmRandomCreate(unsigned long seed)
Packit Service 103f6b
{
Packit Service 103f6b
    RandomState  *state;
Packit Service 103f6b
Packit Service 103f6b
    state           = drmMalloc(sizeof(*state));
Packit Service 103f6b
    if (!state) return NULL;
Packit Service 103f6b
    state->magic    = RANDOM_MAGIC;
Packit Service 103f6b
#if 0
Packit Service 103f6b
				/* Park & Miller, October 1988 */
Packit Service 103f6b
    state->a        = 16807;
Packit Service 103f6b
    state->m        = 2147483647;
Packit Service 103f6b
    state->check    = 1043618065; /* After 10000 iterations */
Packit Service 103f6b
#else
Packit Service 103f6b
				/* Park, Miller, and Stockmeyer, July 1993 */
Packit Service 103f6b
    state->a        = 48271;
Packit Service 103f6b
    state->m        = 2147483647;
Packit Service 103f6b
    state->check    = 399268537; /* After 10000 iterations */
Packit Service 103f6b
#endif
Packit Service 103f6b
    state->q        = state->m / state->a;
Packit Service 103f6b
    state->r        = state->m % state->a;
Packit Service 103f6b
Packit Service 103f6b
    state->seed     = seed;
Packit Service 103f6b
				/* Check for illegal boundary conditions,
Packit Service 103f6b
                                   and choose closest legal value. */
Packit Service 103f6b
    if (state->seed <= 0)        state->seed = 1;
Packit Service 103f6b
    if (state->seed >= state->m) state->seed = state->m - 1;
Packit Service 103f6b
Packit Service 103f6b
    return state;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public int drmRandomDestroy(void *state)
Packit Service 103f6b
{
Packit Service 103f6b
    drmFree(state);
Packit Service 103f6b
    return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public unsigned long drmRandom(void *state)
Packit Service 103f6b
{
Packit Service 103f6b
    RandomState   *s = (RandomState *)state;
Packit Service 103f6b
    unsigned long hi;
Packit Service 103f6b
    unsigned long lo;
Packit Service 103f6b
Packit Service 103f6b
    hi      = s->seed / s->q;
Packit Service 103f6b
    lo      = s->seed % s->q;
Packit Service 103f6b
    s->seed = s->a * lo - s->r * hi;
Packit Service 103f6b
    if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
Packit Service 103f6b
Packit Service 103f6b
    return s->seed;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_public double drmRandomDouble(void *state)
Packit Service 103f6b
{
Packit Service 103f6b
    RandomState *s = (RandomState *)state;
Packit Service 103f6b
    
Packit Service 103f6b
    return (double)drmRandom(state)/(double)s->m;
Packit Service 103f6b
}