xref: /netbsd-src/sys/lib/libkern/arch/vax/random.S (revision ad9091c30f98be59386ade05db65ad59d7ad9827)
1/*	$NetBSD: random.S,v 1.4 2007/01/14 13:26:18 ragge Exp $	*/
2
3/*
4 * Copyright (c) 1994 Ludd, University of Lule}, Sweden. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
16 *
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.
27 */
28
29/*
30 * Copyright (c) 1990,1993 The Regents of the University of California.
31 * All rights reserved.
32 *
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
44 * written permission.
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.
48 *
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
55 * 1043618065.
56 *
57 * Here is easier-to-decipher pseudocode:
58 *
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
61 * if (p + q < 2^31)
62 * 	seed = p + q
63 * else
64 *	seed = ((p + q) & (2^31 - 1)) + 1
65 * return (seed);
66 *
67 * The result is in (0,2^31), e.g., it's always positive.
68 */
69
70#include <machine/asm.h>
71
72	.data
73randseed:
74	.long	1
75
76ENTRY(random, 0)
77	movl	$16807,%r0
78
79	movl	randseed,%r1		# %r2=16807*loword(randseed)
80	bicl3	$0xffff0000,%r1,%r2
81	mull2	%r0,%r2
82	ashl	$-16,%r1,%r1		# %r1=16807*hiword(randseed)
83	bicl2	$0xffff0000,%r1
84	mull2	%r0,%r1
85	bicl3	$0xffff0000,%r2,%r0
86	ashl	$-16,%r2,%r2		# %r1+=(%r2>>16)
87	bicl2	$0xffff0000,%r2
88	addl2	%r2,%r1
89	ashl	$16,%r1,%r2		# %r0|=%r1<<16
90	bisl2	%r2,%r0
91	ashl	$-16,%r1,%r1		# %r1=%r1>>16
92
93	ashl	$1,%r1,%r1
94	movl	%r0,%r2
95	rotl	$1,%r0,%r0
96	bicl2	$0xfffffffe,%r0
97	bisl2	%r0,%r1
98	movl	%r2,%r0
99	bicl2	$0x80000000,%r0
100	addl2	%r1,%r0
101	bgeq	L1
102	subl2	$0x7fffffff,%r0
103L1:	movl	%r0,randseed
104	ret
105