1 /* $NetBSD: random.S,v 1.3 2002/02/24 00:08:23 matt Exp $ */
4 * Copyright (c) 1994 Ludd, University of Lule}, Sweden. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 1990,1993 The Regents of the University of California.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that: (1) source code distributions
35 * retain the above copyright notice and this paragraph in its entirety, (2)
36 * distributions including binary code include the above copyright notice and
37 * this paragraph in its entirety in the documentation or other materials
38 * provided with the distribution, and (3) all advertising materials mentioning
39 * features or use of this software display the following acknowledgement:
40 * ``This product includes software developed by the University of California,
41 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
42 * the University nor the names of its contributors may be used to endorse
43 * or promote products derived from this software without specific prior
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
46 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49 * Here is a very good random number generator. This implementation is
50 * based on ``Two Fast Implementations of the "Minimal Standard" Random
51 * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
52 * Vol 33 No 1. Do NOT modify this code unless you have a very thorough
53 * understanding of the algorithm. It's trickier than you think. If
54 * you do change it, make sure that its 10,000'th invocation returns
57 * Here is easier-to-decipher pseudocode:
59 * p = (16807*seed)<30:0> # e.g., the low 31 bits of the product
60 * q = (16807*seed)<62:31> # e.g., the high 31 bits starting at bit 32
64 * seed = ((p + q) & (2^31 - 1)) + 1
67 * The result is in (0,2^31), e.g., it's always positive.
70 #include <machine/asm.h>
79 movl randseed,%r1 # %r2=16807*loword(randseed)
80 bicl3 $0xffff0000,%r1,%r2
82 ashl $-16,%r1,%r1 # %r1=16807*hiword(randseed)
85 bicl3 $0xffff0000,%r2,%r0
86 ashl $-16,%r2,%r2 # %r1+=(%r2>>16)
89 ashl $16,%r1,%r2 # %r0|=%r1<<16
91 ashl $-16,%r1,%r1 # %r1=%r1>>16
102 subl2 $0x7fffffff,%r0
103 L1: movl %r0,randseed