xref: /netbsd-src/sys/lib/libkern/arch/sparc/random.S (revision 1b088eaae22f74ef4b82bd80fb2facfcdb007183)
1*1b088eaaSnakayama/*	$NetBSD: random.S,v 1.5 2014/03/07 20:23:09 nakayama Exp $	*/
28a66afbcSmycroft
38a66afbcSmycroft/*
48a66afbcSmycroft * Copyright (c) 1990,1993 The Regents of the University of California.
58a66afbcSmycroft * All rights reserved.
68a66afbcSmycroft *
78a66afbcSmycroft * Redistribution and use in source and binary forms, with or without
88a66afbcSmycroft * modification, are permitted provided that: (1) source code distributions
98a66afbcSmycroft * retain the above copyright notice and this paragraph in its entirety, (2)
108a66afbcSmycroft * distributions including binary code include the above copyright notice and
118a66afbcSmycroft * this paragraph in its entirety in the documentation or other materials
128a66afbcSmycroft * provided with the distribution, and (3) all advertising materials mentioning
138a66afbcSmycroft * features or use of this software display the following acknowledgement:
148a66afbcSmycroft * ``This product includes software developed by the University of California,
158a66afbcSmycroft * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
168a66afbcSmycroft * the University nor the names of its contributors may be used to endorse
178a66afbcSmycroft * or promote products derived from this software without specific prior
188a66afbcSmycroft * written permission.
198a66afbcSmycroft * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
208a66afbcSmycroft * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
218a66afbcSmycroft * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
228a66afbcSmycroft *
238a66afbcSmycroft * Here is a very good random number generator.  This implementation is
248a66afbcSmycroft * based on ``Two Fast Implementations of the "Minimal Standard" Random
258a66afbcSmycroft * Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
268a66afbcSmycroft * Vol 33 No 1.  Do NOT modify this code unless you have a very thorough
278a66afbcSmycroft * understanding of the algorithm.  It's trickier than you think.  If
288a66afbcSmycroft * you do change it, make sure that its 10,000'th invocation returns
298a66afbcSmycroft * 1043618065.
308a66afbcSmycroft *
318a66afbcSmycroft * Here is easier-to-decipher pseudocode:
328a66afbcSmycroft *
338a66afbcSmycroft * p = (16807*seed)<30:0>	# e.g., the low 31 bits of the product
348a66afbcSmycroft * q = (16807*seed)<62:31>	# e.g., the high 31 bits starting at bit 32
358a66afbcSmycroft * if (p + q < 2^31)
368a66afbcSmycroft * 	seed = p + q
378a66afbcSmycroft * else
388a66afbcSmycroft *	seed = ((p + q) & (2^31 - 1)) + 1
398a66afbcSmycroft * return (seed);
408a66afbcSmycroft *
418a66afbcSmycroft * The result is in (0,2^31), e.g., it's always positive.
428a66afbcSmycroft */
438a66afbcSmycroft#include <machine/asm.h>
448a66afbcSmycroft
458a66afbcSmycroft	.data
46*1b088eaaSnakayama	.align	4
478a66afbcSmycroftrandseed:
488a66afbcSmycroft	.long	1
49cbd549e7Spooka
508a66afbcSmycroft	.text
518a66afbcSmycroftENTRY(random)
528a66afbcSmycroft	sethi	%hi(16807), %o1
538a66afbcSmycroft	wr	%o1, %lo(16807), %y
544d12bfcdSjoerg#ifdef __PIC__
55cbd549e7Spooka	PIC_PROLOGUE(%o5, %o2)
56cbd549e7Spooka	set	randseed, %g1
57cbd549e7Spooka	ld	[%o5 + %g1], %g1
58cbd549e7Spooka	ld	[%g1], %o0
59c5889d57Spooka#else
608a66afbcSmycroft	sethi	%hi(randseed), %g1
618a66afbcSmycroft	ld	[%g1 + %lo(randseed)], %o0
62c5889d57Spooka#endif
638a66afbcSmycroft	andcc	%g0, 0, %o2
648a66afbcSmycroft	mulscc  %o2, %o0, %o2
658a66afbcSmycroft	mulscc  %o2, %o0, %o2
668a66afbcSmycroft	mulscc  %o2, %o0, %o2
678a66afbcSmycroft	mulscc  %o2, %o0, %o2
688a66afbcSmycroft	mulscc  %o2, %o0, %o2
698a66afbcSmycroft	mulscc  %o2, %o0, %o2
708a66afbcSmycroft	mulscc  %o2, %o0, %o2
718a66afbcSmycroft	mulscc  %o2, %o0, %o2
728a66afbcSmycroft	mulscc  %o2, %o0, %o2
738a66afbcSmycroft	mulscc  %o2, %o0, %o2
748a66afbcSmycroft	mulscc  %o2, %o0, %o2
758a66afbcSmycroft	mulscc  %o2, %o0, %o2
768a66afbcSmycroft	mulscc  %o2, %o0, %o2
778a66afbcSmycroft	mulscc  %o2, %o0, %o2
788a66afbcSmycroft	mulscc  %o2, %o0, %o2
798a66afbcSmycroft	mulscc  %o2, %g0, %o2
808a66afbcSmycroft	rd	%y, %o3
818a66afbcSmycroft	srl	%o2, 16, %o1
828a66afbcSmycroft	set	0xffff, %o4
838a66afbcSmycroft	and	%o4, %o2, %o0
848a66afbcSmycroft	sll	%o0, 15, %o0
858a66afbcSmycroft	srl	%o3, 17, %o3
868a66afbcSmycroft	or	%o3, %o0, %o0
878a66afbcSmycroft	addcc	%o0, %o1, %o0
888a66afbcSmycroft	bneg	1f
898a66afbcSmycroft	sethi	%hi(0x7fffffff), %o1
908a66afbcSmycroft	retl
914d12bfcdSjoerg#ifdef __PIC__
92cbd549e7Spooka	st	%o0, [%g1]
93c5889d57Spooka#else
948a66afbcSmycroft	st	%o0, [%g1 + %lo(randseed)]
95c5889d57Spooka#endif
968a66afbcSmycroft1:
978a66afbcSmycroft	or	%o1, %lo(0x7fffffff), %o1
988a66afbcSmycroft	add	%o0, 1, %o0
998a66afbcSmycroft	and	%o1, %o0, %o0
1008a66afbcSmycroft	retl
1014d12bfcdSjoerg#ifdef __PIC__
102cbd549e7Spooka	st	%o0, [%g1]
103c5889d57Spooka#else
1048a66afbcSmycroft	st	%o0, [%g1 + %lo(randseed)]
105c5889d57Spooka#endif
106