random.S revision 1.2 1 /* $NetBSD: random.S,v 1.2 1998/08/15 03:02:49 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1995 Charles M. Hannum.
5 * Copyright (c) 1990,1993 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that: (1) source code distributions
10 * retain the above copyright notice and this paragraph in its entirety, (2)
11 * distributions including binary code include the above copyright notice and
12 * this paragraph in its entirety in the documentation or other materials
13 * provided with the distribution, and (3) all advertising materials mentioning
14 * features or use of this software display the following acknowledgement:
15 * ``This product includes software developed by the University of California,
16 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17 * the University nor the names of its contributors may be used to endorse
18 * or promote products derived from this software without specific prior
19 * written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 *
24 * Here is a very good random number generator. This implementation is
25 * based on ``Two Fast Implementations of the "Minimal Standard" Random
26 * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
27 * Vol 33 No 1. Do NOT modify this code unless you have a very thorough
28 * understanding of the algorithm. It's trickier than you think. If
29 * you do change it, make sure that its 10,000'th invocation returns
30 * 1043618065.
31 *
32 * Here is easier-to-decipher pseudocode:
33 *
34 * p = (16807*seed)<30:0> # e.g., the low 31 bits of the product
35 * q = (16807*seed)<62:31> # e.g., the high 31 bits starting at bit 32
36 * if (p + q < 2^31)
37 * seed = p + q
38 * else
39 * seed = ((p + q) & (2^31 - 1)) + 1
40 * return (seed);
41 *
42 * The result is in (0,2^31), e.g., it's always positive.
43 */
44 #include <machine/asm.h>
45
46 .data
47 randseed:
48 .long 1
49 .text
50 ENTRY(random)
51 movl $16807,%eax
52 imull randseed
53 shld $1,%eax,%edx
54 andl $0x7fffffff,%eax
55 addl %edx,%eax
56 js 1f
57 movl %eax,randseed
58 ret
59 1:
60 subl $0x7fffffff,%eax
61 movl %eax,randseed
62 ret
63