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