random.S revision 1.3 1 /* $NetBSD: random.S,v 1.3 2002/02/24 00:08:23 matt Exp $ */
2
3 /*
4 * Copyright (c) 1990,1993 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * Here is a very good random number generator. This implementation is
24 * based on ``Two Fast Implementations of the "Minimal Standard" Random
25 * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
26 * Vol 33 No 1. Do NOT modify this code unless you have a very thorough
27 * understanding of the algorithm. It's trickier than you think. If
28 * you do change it, make sure that its 10,000'th invocation returns
29 * 1043618065.
30 *
31 * This implementation Copyright (c) 1994 Ludd, University of Lule}, Sweden
32 * All rights reserved.
33 *
34 * All bugs subject to removal without further notice.
35 *
36 * Here is easier-to-decipher pseudocode:
37 *
38 * p = (16807*seed)<30:0> # e.g., the low 31 bits of the product
39 * q = (16807*seed)<62:31> # e.g., the high 31 bits starting at bit 32
40 * if (p + q < 2^31)
41 * seed = p + q
42 * else
43 * seed = ((p + q) & (2^31 - 1)) + 1
44 * return (seed);
45 *
46 * The result is in (0,2^31), e.g., it's always positive.
47 */
48
49 #include <machine/asm.h>
50
51 .data
52 randseed:
53 .long 1
54
55 ENTRY(random, 0)
56 movl $16807,%r0
57
58 movl randseed,%r1 # %r2=16807*loword(randseed)
59 bicl3 $0xffff0000,%r1,%r2
60 mull2 %r0,%r2
61 ashl $-16,%r1,%r1 # %r1=16807*hiword(randseed)
62 bicl2 $0xffff0000,%r1
63 mull2 %r0,%r1
64 bicl3 $0xffff0000,%r2,%r0
65 ashl $-16,%r2,%r2 # %r1+=(%r2>>16)
66 bicl2 $0xffff0000,%r2
67 addl2 %r2,%r1
68 ashl $16,%r1,%r2 # %r0|=%r1<<16
69 bisl2 %r2,%r0
70 ashl $-16,%r1,%r1 # %r1=%r1>>16
71
72 ashl $1,%r1,%r1
73 movl %r0,%r2
74 rotl $1,%r0,%r0
75 bicl2 $0xfffffffe,%r0
76 bisl2 %r0,%r1
77 movl %r2,%r0
78 bicl2 $0x80000000,%r0
79 addl2 %r1,%r0
80 bgeq L1
81 subl2 $0x7fffffff,%r0
82 L1: movl %r0,randseed
83 ret
84