minix/sys/lib/libkern/arch/m68k/random.S
2012-02-09 18:48:13 +01:00

70 lines
2.4 KiB
ArmAsm

/* $NetBSD: random.S,v 1.4 2009/01/06 01:24:56 pooka Exp $ */
/*
* Copyright (c) 1990,1993 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may be used to endorse
* or promote products derived from this software without specific prior
* written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Here is a very good random number generator. This implementation is
* based on ``Two Fast Implementations of the "Minimal Standard" Random
* Number Generator'', David G. Carta, Communications of the ACM, Jan 1990,
* Vol 33 No 1. Do NOT modify this code unless you have a very thorough
* understanding of the algorithm. It's trickier than you think. If
* you do change it, make sure that its 10,000'th invocation returns
* 1043618065.
*
* Here is easier-to-decipher pseudocode:
*
* p = (16807*seed)<30:0> # e.g., the low 31 bits of the product
* q = (16807*seed)<62:31> # e.g., the high 31 bits starting at bit 32
* if (p + q < 2^31)
* seed = p + q
* else
* seed = ((p + q) & (2^31 - 1)) + 1
* return (seed);
*
* The result is in (0,2^31), e.g., it's always positive.
*/
#include <machine/asm.h>
.data
ASLOCAL(randseed)
.long 1
ENTRY(random)
movl #16807, %d0
#ifdef PIC
lea %pc@(_GLOBAL_OFFSET_TABLE_@GOTPC), %a0
movl _ASM_LABEL(randseed)@GOT(%a0), %d2
mulsl (%d2), %d1:%d0
#else
mulsl _ASM_LABEL(randseed), %d1:%d0
#endif
lsll #1, %d0
roxll #2, %d1
addl %d1, %d0
moveql #1, %d1
addxl %d1, %d0
lsrl #1, %d0
#ifdef PIC
movl %d0, (%d2)
#else
movl %d0, _ASM_LABEL(randseed)
#endif
rts