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