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