Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:42

0001 #include "simpleRandom.h"
0002 #include <math.h>
0003 #if defined(SunOS) || defined(Linux) || defined(OSF1)
0004 #include <strings.h>
0005 #else
0006 #include <bstring.h>
0007 #endif
0008 
0009 
0010 /*
0011  * Shuffle the bytes into little-endian order within words, as per the
0012  * MD5 spec.  Note: this code works regardless of the byte order.
0013  */
0014 void
0015 simpleRandom::byteSwap(word32 *buf, unsigned words)
0016 {
0017         xbyte *p = (xbyte *)buf;
0018 
0019         do {
0020                 *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
0021                         ((unsigned)p[1] << 8 | p[0]);
0022                 p += 4;
0023         } while (--words);
0024 }
0025 
0026 /*
0027  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
0028  * initialization constants.
0029  */
0030 void
0031 simpleRandom::xMD5Init(struct xMD5Context *ctx)
0032 {
0033         ctx->buf[0] = 0x67452301;
0034         ctx->buf[1] = 0xefcdab89;
0035         ctx->buf[2] = 0x98badcfe;
0036         ctx->buf[3] = 0x10325476;
0037 
0038         ctx->bytes[0] = 0;
0039         ctx->bytes[1] = 0;
0040 }
0041 
0042 /*
0043  * Update context to reflect the concatenation of another buffer full
0044  * of bytes.
0045  */
0046 void
0047 simpleRandom::xMD5Update(struct xMD5Context *ctx, xbyte const *buf, unsigned int len)
0048 {
0049         word32 t;
0050 
0051         /* Update byte count */
0052 
0053         t = ctx->bytes[0];
0054         if ((ctx->bytes[0] = t + len) < t)
0055                 ctx->bytes[1]++;        /* Carry from low to high */
0056 
0057         t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
0058         if ( t > len) {
0059                 bcopy(buf, (xbyte *)ctx->in + 64 - (unsigned)t, len);
0060                 return;
0061         }
0062         /* First chunk is an odd size */
0063         bcopy(buf,(xbyte *)ctx->in + 64 - (unsigned)t, (unsigned)t);
0064         byteSwap(ctx->in, 16);
0065         xMD5Transform(ctx->buf, ctx->in);
0066         buf += (unsigned)t;
0067         len -= (unsigned)t;
0068 
0069         /* Process data in 64-byte chunks */
0070         while (len >= 64) {
0071                 bcopy(buf, ctx->in, 64);
0072                 byteSwap(ctx->in, 16);
0073                 xMD5Transform(ctx->buf, ctx->in);
0074                 buf += 64;
0075                 len -= 64;
0076         }
0077 
0078         /* Handle any remaining bytes of data. */
0079         bcopy(buf, ctx->in, len);
0080 }
0081 
0082 /*
0083  * Final wrapup - pad to 64-byte boundary with the bit pattern 
0084  * 1 0* (64-bit count of bits processed, MSB-first)
0085  */
0086 void
0087 simpleRandom::xMD5Final(xbyte bdigest[16], struct xMD5Context *ctx)
0088 {
0089         int count = (int)(ctx->bytes[0] & 0x3f); /* Bytes in ctx->in */
0090         xbyte *p = (xbyte *)ctx->in + count;      /* First unused byte */
0091 
0092         /* Set the first char of padding to 0x80.  There is always room. */
0093         *p++ = 0x80;
0094 
0095         /* Bytes of padding needed to make 56 bytes (-8..55) */
0096         count = 56 - 1 - count;
0097 
0098         if (count < 0) {        /* Padding forces an extra block */
0099                 bzero(p, count+8);
0100                 byteSwap(ctx->in, 16);
0101                 xMD5Transform(ctx->buf, ctx->in);
0102                 p = (xbyte *)ctx->in;
0103                 count = 56;
0104         }
0105         bzero(p, count+8);
0106         byteSwap(ctx->in, 14);
0107 
0108         /* Append length in bits and transform */
0109         ctx->in[14] = ctx->bytes[0] << 3;
0110         ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
0111         xMD5Transform(ctx->buf, ctx->in);
0112 
0113         byteSwap(ctx->buf, 4);
0114         bcopy(ctx->buf, bdigest, 16);
0115         bzero(ctx,sizeof(*ctx));
0116 }
0117 
0118 
0119 /* The four core functions - F1 is optimized somewhat */
0120 
0121 /* #define F1(x, y, z) (x & y | ~x & z) */
0122 #define F1(x, y, z) (z ^ (x & (y ^ z)))
0123 #define F2(x, y, z) F1(z, x, y)
0124 #define F3(x, y, z) (x ^ y ^ z)
0125 #define F4(x, y, z) (y ^ (x | ~z))
0126 
0127 /* This is the central step in the MD5 algorithm. */
0128 #define MD5STEP(f,w,x,y,z,in,s) \
0129          (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
0130 
0131 /*
0132  * The core of the MD5 algorithm, this alters an existing MD5 hash to
0133  * reflect the addition of 16 longwords of new data.  MD5Update blocks
0134  * the data and converts bytes into longwords for this routine.
0135  */
0136 void
0137 simpleRandom::xMD5Transform(word32 buf[4], word32 const in[16])
0138 {
0139         word32 a, b, c, d;
0140 
0141         a = buf[0];
0142         b = buf[1];
0143         c = buf[2];
0144         d = buf[3];
0145 
0146         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
0147         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
0148         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
0149         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
0150         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
0151         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
0152         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
0153         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
0154         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
0155         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
0156         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
0157         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
0158         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
0159         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
0160         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
0161         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
0162 
0163         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
0164         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
0165         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
0166         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
0167         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
0168         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
0169         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
0170         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
0171         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
0172         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
0173         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
0174         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
0175         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
0176         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
0177         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
0178         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
0179 
0180         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
0181         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
0182         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
0183         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
0184         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
0185         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
0186         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
0187         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
0188         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
0189         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
0190         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
0191         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
0192         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
0193         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
0194         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
0195         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
0196 
0197         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
0198         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
0199         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
0200         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
0201         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
0202         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
0203         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
0204         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
0205         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
0206         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
0207         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
0208         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
0209         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
0210         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
0211         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
0212         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
0213 
0214         buf[0] += a;
0215         buf[1] += b;
0216         buf[2] += c;
0217         buf[3] += d;
0218 }
0219 
0220 
0221 void simpleRandom::MD5(xbyte *dest, const xbyte *orig, unsigned int len)
0222 {
0223         struct xMD5Context context;
0224 
0225         xMD5Init(&context);
0226         xMD5Update(&context, orig, len);
0227         xMD5Final(dest, &context);
0228 }
0229 
0230 /*****************************************************************/
0231 
0232 simpleRandom::simpleRandom()
0233 {
0234   digest[0] = 7657635;
0235   digest[1] = 5565649;
0236   digest[2] = 9827729;
0237   digest[3] = 9892898;
0238 
0239 
0240 }
0241 
0242 simpleRandom::simpleRandom( const int iseed)
0243 {
0244   digest[0] = iseed;
0245   digest[1] = 565649;
0246   digest[2] = 6827729;
0247   digest[3] = 2892898;
0248 
0249 }
0250 
0251 float simpleRandom::gauss(const float mean, const float sigma)
0252 {
0253 #define BIGNUMBER 100000
0254 
0255   float y = 0.;
0256   while(y == 0.)
0257     {
0258       y = rnd(0,BIGNUMBER);
0259     }
0260   float z = rnd(0,BIGNUMBER);
0261   
0262   y /= BIGNUMBER;
0263   z /= BIGNUMBER;
0264 
0265   float x = z * 6.283185;
0266 
0267 #if defined(SunOS) || defined(Linux)
0268   float result = mean + sigma* sin(x) * sqrt(-2 * log(y) );
0269 #else
0270   float result = mean + sigma* sinf(x) * sqrtf(-2 * logf(y) );
0271 #endif
0272 
0273   return result;
0274 }
0275 
0276 float simpleRandom::rnd(int low, int high)
0277 {
0278         unsigned int range = high - low;
0279         unsigned int mask = 0;
0280         unsigned int num;
0281         int r;
0282 
0283         for (r = range; r; r >>= 1)
0284                 mask |= r;
0285 
0286         do {
0287                 MD5((xbyte*)digest,(const xbyte *) digest, sizeof(digest));
0288                 num = digest[0] & mask;
0289         } while (num > range);
0290 
0291         return num + low;
0292 }