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