Home | History | Annotate | Line # | Download | only in sparc64
random.S revision 1.1.1.1.160.1
      1  1.1.1.1.160.1  skrll /*	$NetBSD: random.S,v 1.1.1.1.160.1 2009/01/19 13:19:57 skrll Exp $	*/
      2            1.1    eeh 
      3            1.1    eeh /*
      4            1.1    eeh  * Copyright (c) 1990,1993 The Regents of the University of California.
      5            1.1    eeh  * All rights reserved.
      6            1.1    eeh  *
      7            1.1    eeh  * Redistribution and use in source and binary forms, with or without
      8            1.1    eeh  * modification, are permitted provided that: (1) source code distributions
      9            1.1    eeh  * retain the above copyright notice and this paragraph in its entirety, (2)
     10            1.1    eeh  * distributions including binary code include the above copyright notice and
     11            1.1    eeh  * this paragraph in its entirety in the documentation or other materials
     12            1.1    eeh  * provided with the distribution, and (3) all advertising materials mentioning
     13            1.1    eeh  * features or use of this software display the following acknowledgement:
     14            1.1    eeh  * ``This product includes software developed by the University of California,
     15            1.1    eeh  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     16            1.1    eeh  * the University nor the names of its contributors may be used to endorse
     17            1.1    eeh  * or promote products derived from this software without specific prior
     18            1.1    eeh  * written permission.
     19            1.1    eeh  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     20            1.1    eeh  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     21            1.1    eeh  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     22            1.1    eeh  *
     23            1.1    eeh  * Here is a very good random number generator.  This implementation is
     24            1.1    eeh  * based on ``Two Fast Implementations of the "Minimal Standard" Random
     25            1.1    eeh  * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
     26            1.1    eeh  * Vol 33 No 1.  Do NOT modify this code unless you have a very thorough
     27            1.1    eeh  * understanding of the algorithm.  It's trickier than you think.  If
     28            1.1    eeh  * you do change it, make sure that its 10,000'th invocation returns
     29            1.1    eeh  * 1043618065.
     30            1.1    eeh  *
     31            1.1    eeh  * Here is easier-to-decipher pseudocode:
     32            1.1    eeh  *
     33            1.1    eeh  * p = (16807*seed)<30:0>	# e.g., the low 31 bits of the product
     34            1.1    eeh  * q = (16807*seed)<62:31>	# e.g., the high 31 bits starting at bit 32
     35            1.1    eeh  * if (p + q < 2^31)
     36            1.1    eeh  * 	seed = p + q
     37            1.1    eeh  * else
     38            1.1    eeh  *	seed = ((p + q) & (2^31 - 1)) + 1
     39            1.1    eeh  * return (seed);
     40            1.1    eeh  *
     41            1.1    eeh  * The result is in (0,2^31), e.g., it's always positive.
     42            1.1    eeh  */
     43            1.1    eeh #include <machine/asm.h>
     44            1.1    eeh 
     45            1.1    eeh 	.data
     46            1.1    eeh randseed:
     47            1.1    eeh 	.long	1
     48  1.1.1.1.160.1  skrll 
     49            1.1    eeh 	.text
     50            1.1    eeh ENTRY(random)
     51            1.1    eeh 	sethi	%hi(16807), %o1
     52            1.1    eeh 	wr	%o1, %lo(16807), %y
     53  1.1.1.1.160.1  skrll #ifdef PIC
     54  1.1.1.1.160.1  skrll 	PIC_PROLOGUE(%o5, %o2)
     55  1.1.1.1.160.1  skrll 	set	randseed, %g1
     56  1.1.1.1.160.1  skrll 	ldx	[%o5 + %g1], %g1
     57  1.1.1.1.160.1  skrll 	ld	[%g1], %o0
     58  1.1.1.1.160.1  skrll #else
     59  1.1.1.1.160.1  skrll 	sethi	%hi(randseed), %g1
     60  1.1.1.1.160.1  skrll 	ld	[%g1 + %lo(randseed)], %o0
     61  1.1.1.1.160.1  skrll #endif
     62  1.1.1.1.160.1  skrll 	andcc	%g0, 0, %o2
     63            1.1    eeh 	mulscc  %o2, %o0, %o2
     64            1.1    eeh 	mulscc  %o2, %o0, %o2
     65            1.1    eeh 	mulscc  %o2, %o0, %o2
     66            1.1    eeh 	mulscc  %o2, %o0, %o2
     67            1.1    eeh 	mulscc  %o2, %o0, %o2
     68            1.1    eeh 	mulscc  %o2, %o0, %o2
     69            1.1    eeh 	mulscc  %o2, %o0, %o2
     70            1.1    eeh 	mulscc  %o2, %o0, %o2
     71            1.1    eeh 	mulscc  %o2, %o0, %o2
     72            1.1    eeh 	mulscc  %o2, %o0, %o2
     73            1.1    eeh 	mulscc  %o2, %o0, %o2
     74            1.1    eeh 	mulscc  %o2, %o0, %o2
     75            1.1    eeh 	mulscc  %o2, %o0, %o2
     76            1.1    eeh 	mulscc  %o2, %o0, %o2
     77            1.1    eeh 	mulscc  %o2, %o0, %o2
     78            1.1    eeh 	mulscc  %o2, %g0, %o2
     79            1.1    eeh 	rd	%y, %o3
     80            1.1    eeh 	srl	%o2, 16, %o1
     81            1.1    eeh 	set	0xffff, %o4
     82            1.1    eeh 	and	%o4, %o2, %o0
     83            1.1    eeh 	sll	%o0, 15, %o0
     84            1.1    eeh 	srl	%o3, 17, %o3
     85            1.1    eeh 	or	%o3, %o0, %o0
     86            1.1    eeh 	addcc	%o0, %o1, %o0
     87            1.1    eeh 	bneg	1f
     88  1.1.1.1.160.1  skrll 	sethi	%hi(0x7fffffff), %o1
     89            1.1    eeh 	retl
     90  1.1.1.1.160.1  skrll #ifdef PIC
     91  1.1.1.1.160.1  skrll 	st	%o0, [%g1]
     92  1.1.1.1.160.1  skrll #else
     93  1.1.1.1.160.1  skrll 	st	%o0, [%g1 + %lo(randseed)]
     94  1.1.1.1.160.1  skrll #endif
     95            1.1    eeh 1:
     96            1.1    eeh 	or	%o1, %lo(0x7fffffff), %o1
     97            1.1    eeh 	add	%o0, 1, %o0
     98            1.1    eeh 	and	%o1, %o0, %o0
     99            1.1    eeh 	retl
    100  1.1.1.1.160.1  skrll #ifdef PIC
    101  1.1.1.1.160.1  skrll 	st	%o0, [%g1]
    102  1.1.1.1.160.1  skrll #else
    103  1.1.1.1.160.1  skrll 	st	%o0, [%g1 + %lo(randseed)]
    104  1.1.1.1.160.1  skrll #endif
    105