/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

/* Basic random number generator. 
 * Algorithm: "Tausworthe generator" (L'Ecuyer). Period about 10^26.
 */
#include <iostypes.h>
#include <ios.h>
#include <ioslibc.h>

static u32 s1, s2, s3;
#define tau(s, A, b, c, d) \
    (((s & A) << b) & 0xffffffff)^((((s << c) & 0xffffffff)^s1)>>d)
u32
getRand32(void)
{
    s1 = tau(s1, 4294967294UL, 12, 13, 19);
    s2 = tau(s2, 4294967288UL, 4, 2, 25);
    s3 = tau(s3, 4294967280UL, 17, 3, 11);
    return (s1 ^ s2 ^ s3);
}

void
initRandSeeds(u32 s)
{
    u32 i;
    s1 = ((69069 * s) & 0xffffffffUL);
    if (s1 < 2) s1 += 2;
    s2 =  ((69069 * s1) & 0xffffffffUL);
    if (s1 < 8) s1 += 8;
    s3 =  ((69069 * s2) & 0xffffffffUL);
    if (s1 < 16) s1 += 16;

    for (i = 10; i > 0; --i) {
        getRand32();
    }
    return;
}
