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