Home | History | Annotate | Line # | Download | only in i386
random.S revision 1.3.144.2
      1  1.3.144.1      mjf /*	$NetBSD: random.S,v 1.3.144.2 2009/01/17 13:29:21 mjf Exp $	*/
      2        1.3  mycroft 
      3        1.3  mycroft /*-
      4        1.3  mycroft  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5        1.3  mycroft  * All rights reserved.
      6        1.3  mycroft  *
      7        1.3  mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8        1.3  mycroft  * by Charles M. Hannum.
      9        1.3  mycroft  *
     10        1.3  mycroft  * Redistribution and use in source and binary forms, with or without
     11        1.3  mycroft  * modification, are permitted provided that the following conditions
     12        1.3  mycroft  * are met:
     13        1.3  mycroft  * 1. Redistributions of source code must retain the above copyright
     14        1.3  mycroft  *    notice, this list of conditions and the following disclaimer.
     15        1.3  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.3  mycroft  *    notice, this list of conditions and the following disclaimer in the
     17        1.3  mycroft  *    documentation and/or other materials provided with the distribution.
     18        1.3  mycroft  *
     19        1.3  mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.3  mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.3  mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.3  mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.3  mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.3  mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.3  mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.3  mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.3  mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.3  mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.3  mycroft  * POSSIBILITY OF SUCH DAMAGE.
     30        1.3  mycroft  */
     31        1.1  mycroft 
     32        1.1  mycroft /*
     33        1.1  mycroft  * Copyright (c) 1990,1993 The Regents of the University of California.
     34        1.1  mycroft  * All rights reserved.
     35        1.1  mycroft  *
     36        1.1  mycroft  * Redistribution and use in source and binary forms, with or without
     37        1.1  mycroft  * modification, are permitted provided that: (1) source code distributions
     38        1.1  mycroft  * retain the above copyright notice and this paragraph in its entirety, (2)
     39        1.1  mycroft  * distributions including binary code include the above copyright notice and
     40        1.1  mycroft  * this paragraph in its entirety in the documentation or other materials
     41        1.1  mycroft  * provided with the distribution, and (3) all advertising materials mentioning
     42        1.1  mycroft  * features or use of this software display the following acknowledgement:
     43        1.1  mycroft  * ``This product includes software developed by the University of California,
     44        1.1  mycroft  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     45        1.1  mycroft  * the University nor the names of its contributors may be used to endorse
     46        1.1  mycroft  * or promote products derived from this software without specific prior
     47        1.1  mycroft  * written permission.
     48        1.1  mycroft  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     49        1.1  mycroft  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     50        1.1  mycroft  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     51        1.1  mycroft  *
     52        1.1  mycroft  * Here is a very good random number generator.  This implementation is
     53        1.1  mycroft  * based on ``Two Fast Implementations of the "Minimal Standard" Random
     54        1.1  mycroft  * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
     55        1.1  mycroft  * Vol 33 No 1.  Do NOT modify this code unless you have a very thorough
     56        1.1  mycroft  * understanding of the algorithm.  It's trickier than you think.  If
     57        1.1  mycroft  * you do change it, make sure that its 10,000'th invocation returns
     58        1.1  mycroft  * 1043618065.
     59        1.1  mycroft  *
     60        1.1  mycroft  * Here is easier-to-decipher pseudocode:
     61        1.1  mycroft  *
     62        1.1  mycroft  * p = (16807*seed)<30:0>	# e.g., the low 31 bits of the product
     63        1.1  mycroft  * q = (16807*seed)<62:31>	# e.g., the high 31 bits starting at bit 32
     64        1.1  mycroft  * if (p + q < 2^31)
     65        1.1  mycroft  * 	seed = p + q
     66        1.1  mycroft  * else
     67        1.1  mycroft  *	seed = ((p + q) & (2^31 - 1)) + 1
     68        1.1  mycroft  * return (seed);
     69        1.1  mycroft  *
     70        1.1  mycroft  * The result is in (0,2^31), e.g., it's always positive.
     71        1.1  mycroft  */
     72        1.1  mycroft #include <machine/asm.h>
     73        1.1  mycroft 
     74        1.1  mycroft 	.data
     75        1.1  mycroft randseed:
     76        1.1  mycroft 	.long	1
     77        1.1  mycroft 	.text
     78        1.1  mycroft ENTRY(random)
     79        1.1  mycroft 	movl	$16807,%eax
     80  1.3.144.2      mjf 	PIC_PROLOGUE
     81  1.3.144.2      mjf 	imull	PIC_GOTOFF(randseed)
     82  1.3.144.2      mjf 	PIC_EPILOGUE
     83        1.1  mycroft 	shld	$1,%eax,%edx
     84        1.1  mycroft 	andl	$0x7fffffff,%eax
     85        1.1  mycroft 	addl	%edx,%eax
     86        1.1  mycroft 	js	1f
     87  1.3.144.2      mjf 	PIC_PROLOGUE
     88  1.3.144.2      mjf 	movl	%eax,PIC_GOTOFF(randseed)
     89  1.3.144.2      mjf 	PIC_EPILOGUE
     90        1.1  mycroft 	ret
     91        1.1  mycroft 1:
     92        1.1  mycroft 	subl	$0x7fffffff,%eax
     93  1.3.144.2      mjf 	PIC_PROLOGUE
     94  1.3.144.2      mjf 	movl	%eax,PIC_GOTOFF(randseed)
     95  1.3.144.2      mjf 	PIC_EPILOGUE
     96        1.1  mycroft 	ret
     97