xref: /netbsd-src/sys/lib/libkern/arch/i386/random.S (revision f3c7386990c30f8e574efe9534ad30369c2ae5cb)
1*f3c73869Spooka/*	$NetBSD: random.S,v 1.6 2010/09/07 20:35:50 pooka Exp $	*/
2cc8a78e7Smycroft
3cc8a78e7Smycroft/*-
4cc8a78e7Smycroft * Copyright (c) 1998 The NetBSD Foundation, Inc.
5cc8a78e7Smycroft * All rights reserved.
6cc8a78e7Smycroft *
7cc8a78e7Smycroft * This code is derived from software contributed to The NetBSD Foundation
8cc8a78e7Smycroft * by Charles M. Hannum.
9cc8a78e7Smycroft *
10cc8a78e7Smycroft * Redistribution and use in source and binary forms, with or without
11cc8a78e7Smycroft * modification, are permitted provided that the following conditions
12cc8a78e7Smycroft * are met:
13cc8a78e7Smycroft * 1. Redistributions of source code must retain the above copyright
14cc8a78e7Smycroft *    notice, this list of conditions and the following disclaimer.
15cc8a78e7Smycroft * 2. Redistributions in binary form must reproduce the above copyright
16cc8a78e7Smycroft *    notice, this list of conditions and the following disclaimer in the
17cc8a78e7Smycroft *    documentation and/or other materials provided with the distribution.
18cc8a78e7Smycroft *
19cc8a78e7Smycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20cc8a78e7Smycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21cc8a78e7Smycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22cc8a78e7Smycroft * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23cc8a78e7Smycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24cc8a78e7Smycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25cc8a78e7Smycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26cc8a78e7Smycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27cc8a78e7Smycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28cc8a78e7Smycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29cc8a78e7Smycroft * POSSIBILITY OF SUCH DAMAGE.
30cc8a78e7Smycroft */
3152541a2eSmycroft
3252541a2eSmycroft/*
3352541a2eSmycroft * Copyright (c) 1990,1993 The Regents of the University of California.
3452541a2eSmycroft * All rights reserved.
3552541a2eSmycroft *
3652541a2eSmycroft * Redistribution and use in source and binary forms, with or without
3752541a2eSmycroft * modification, are permitted provided that: (1) source code distributions
3852541a2eSmycroft * retain the above copyright notice and this paragraph in its entirety, (2)
3952541a2eSmycroft * distributions including binary code include the above copyright notice and
4052541a2eSmycroft * this paragraph in its entirety in the documentation or other materials
4152541a2eSmycroft * provided with the distribution, and (3) all advertising materials mentioning
4252541a2eSmycroft * features or use of this software display the following acknowledgement:
4352541a2eSmycroft * ``This product includes software developed by the University of California,
4452541a2eSmycroft * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
4552541a2eSmycroft * the University nor the names of its contributors may be used to endorse
4652541a2eSmycroft * or promote products derived from this software without specific prior
4752541a2eSmycroft * written permission.
4852541a2eSmycroft * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
4952541a2eSmycroft * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
5052541a2eSmycroft * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5152541a2eSmycroft *
5252541a2eSmycroft * Here is a very good random number generator.  This implementation is
5352541a2eSmycroft * based on ``Two Fast Implementations of the "Minimal Standard" Random
5452541a2eSmycroft * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
5552541a2eSmycroft * Vol 33 No 1.  Do NOT modify this code unless you have a very thorough
5652541a2eSmycroft * understanding of the algorithm.  It's trickier than you think.  If
5752541a2eSmycroft * you do change it, make sure that its 10,000'th invocation returns
5852541a2eSmycroft * 1043618065.
5952541a2eSmycroft *
6052541a2eSmycroft * Here is easier-to-decipher pseudocode:
6152541a2eSmycroft *
6252541a2eSmycroft * p = (16807*seed)<30:0>	# e.g., the low 31 bits of the product
6352541a2eSmycroft * q = (16807*seed)<62:31>	# e.g., the high 31 bits starting at bit 32
6452541a2eSmycroft * if (p + q < 2^31)
6552541a2eSmycroft * 	seed = p + q
6652541a2eSmycroft * else
6752541a2eSmycroft *	seed = ((p + q) & (2^31 - 1)) + 1
6852541a2eSmycroft * return (seed);
6952541a2eSmycroft *
7052541a2eSmycroft * The result is in (0,2^31), e.g., it's always positive.
7152541a2eSmycroft */
7252541a2eSmycroft#include <machine/asm.h>
7352541a2eSmycroft
7452541a2eSmycroft	.data
7552541a2eSmycroftrandseed:
7652541a2eSmycroft	.long	1
7752541a2eSmycroft	.text
7852541a2eSmycroftENTRY(random)
7952541a2eSmycroft	movl	$16807,%eax
80c5889d57Spooka	PIC_PROLOGUE
81c5889d57Spooka	imull	PIC_GOTOFF(randseed)
82c5889d57Spooka	PIC_EPILOGUE
8352541a2eSmycroft	shld	$1,%eax,%edx
8452541a2eSmycroft	andl	$0x7fffffff,%eax
8552541a2eSmycroft	addl	%edx,%eax
86*f3c73869Spooka	js	neg
87c5889d57Spooka	PIC_PROLOGUE
88c5889d57Spooka	movl	%eax,PIC_GOTOFF(randseed)
89c5889d57Spooka	PIC_EPILOGUE
9052541a2eSmycroft	ret
91*f3c73869Spookaneg:
9252541a2eSmycroft	subl	$0x7fffffff,%eax
93c5889d57Spooka	PIC_PROLOGUE
94c5889d57Spooka	movl	%eax,PIC_GOTOFF(randseed)
95c5889d57Spooka	PIC_EPILOGUE
9652541a2eSmycroft	ret
97