random.S revision 1.3
11.3Smaxv/*	$NetBSD: random.S,v 1.3 2017/10/30 17:13:39 maxv Exp $	*/
21.1Sfvdl
31.3Smaxv/*
41.1Sfvdl * Copyright (c) 1998 The NetBSD Foundation, Inc.
51.1Sfvdl * All rights reserved.
61.1Sfvdl *
71.1Sfvdl * This code is derived from software contributed to The NetBSD Foundation
81.1Sfvdl * by Charles M. Hannum.
91.1Sfvdl *
101.1Sfvdl * Redistribution and use in source and binary forms, with or without
111.1Sfvdl * modification, are permitted provided that the following conditions
121.1Sfvdl * are met:
131.1Sfvdl * 1. Redistributions of source code must retain the above copyright
141.1Sfvdl *    notice, this list of conditions and the following disclaimer.
151.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright
161.1Sfvdl *    notice, this list of conditions and the following disclaimer in the
171.1Sfvdl *    documentation and/or other materials provided with the distribution.
181.1Sfvdl *
191.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sfvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sfvdl * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sfvdl * POSSIBILITY OF SUCH DAMAGE.
301.1Sfvdl */
311.1Sfvdl
321.1Sfvdl/*
331.1Sfvdl * Copyright (c) 1990,1993 The Regents of the University of California.
341.1Sfvdl * All rights reserved.
351.1Sfvdl *
361.1Sfvdl * Redistribution and use in source and binary forms, with or without
371.1Sfvdl * modification, are permitted provided that: (1) source code distributions
381.1Sfvdl * retain the above copyright notice and this paragraph in its entirety, (2)
391.1Sfvdl * distributions including binary code include the above copyright notice and
401.1Sfvdl * this paragraph in its entirety in the documentation or other materials
411.1Sfvdl * provided with the distribution, and (3) all advertising materials mentioning
421.1Sfvdl * features or use of this software display the following acknowledgement:
431.1Sfvdl * ``This product includes software developed by the University of California,
441.1Sfvdl * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
451.1Sfvdl * the University nor the names of its contributors may be used to endorse
461.1Sfvdl * or promote products derived from this software without specific prior
471.1Sfvdl * written permission.
481.1Sfvdl * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
491.1Sfvdl * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
501.1Sfvdl * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
511.1Sfvdl *
521.1Sfvdl * Here is a very good random number generator.  This implementation is
531.1Sfvdl * based on ``Two Fast Implementations of the "Minimal Standard" Random
541.1Sfvdl * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
551.1Sfvdl * Vol 33 No 1.  Do NOT modify this code unless you have a very thorough
561.1Sfvdl * understanding of the algorithm.  It's trickier than you think.  If
571.1Sfvdl * you do change it, make sure that its 10,000'th invocation returns
581.1Sfvdl * 1043618065.
591.1Sfvdl *
601.1Sfvdl * Here is easier-to-decipher pseudocode:
611.1Sfvdl *
621.1Sfvdl * p = (16807*seed)<30:0>	# e.g., the low 31 bits of the product
631.1Sfvdl * q = (16807*seed)<62:31>	# e.g., the high 31 bits starting at bit 32
641.1Sfvdl * if (p + q < 2^31)
651.1Sfvdl * 	seed = p + q
661.1Sfvdl * else
671.1Sfvdl *	seed = ((p + q) & (2^31 - 1)) + 1
681.1Sfvdl * return (seed);
691.1Sfvdl *
701.1Sfvdl * The result is in (0,2^31), e.g., it's always positive.
711.1Sfvdl */
721.1Sfvdl#include <machine/asm.h>
731.1Sfvdl
741.1Sfvdl	.data
751.1Sfvdlrandseed:
761.1Sfvdl	.long	1
771.1Sfvdl	.text
781.1SfvdlENTRY(random)
791.1Sfvdl	movl	$16807,%eax
801.1Sfvdl	imull	randseed(%rip)
811.1Sfvdl	shld	$1,%eax,%edx
821.1Sfvdl	andl	$0x7fffffff,%eax
831.1Sfvdl	addl	%edx,%eax
841.1Sfvdl	js	1f
851.1Sfvdl	movl	%eax,randseed(%rip)
861.1Sfvdl	ret
871.1Sfvdl1:
881.1Sfvdl	subl	$0x7fffffff,%eax
891.1Sfvdl	movl	%eax,randseed(%rip)
901.1Sfvdl	ret
911.3SmaxvEND(random)
92