Back to index

radiance  4R0+20100331
urind.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: urind.c,v 2.4 2003/07/17 09:21:29 schorsch Exp $";
00003 #endif
00004 /*
00005  * Compute pseudo-asyncronous entry point for urand(3)
00006  */
00007 
00008 #include "copyright.h"
00009 #include "random.h"
00010 
00011 #define NBITS        32            /* number of bits in an integer */
00012 
00013 static char  bctab[256] = {
00014               0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
00015               1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00016               1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00017               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00018               1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00019               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00020               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00021               3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00022               1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00023               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00024               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00025               3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00026               2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00027               3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00028               3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00029               4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
00030        };
00031 
00032 #if NBITS==32
00033 #define  bitcount(i) (bctab[(i)>>24&0xff]+bctab[(i)>>16&0xff]+ \
00034                             bctab[(i)>>8&0xff]+bctab[(i)&0xff])
00035 #endif
00036 #if  NBITS==16
00037 #define  bitcount(i) (bctab[(i)>>8&0xff]+bctab[(i)&0xff])
00038 #endif
00039 
00040 
00041 int
00042 urind(s, i)                 /* compute i'th index from seed s */
00043 int    s, i;
00044 {
00045        register int  ss, k;
00046        int  left;
00047 
00048        ss = s*1103515245 + 12345;
00049        left = 0;
00050        for (k = i/NBITS; k--; ) {
00051               left += bitcount(ss);
00052               ss = ss*1103515245 + 12345;
00053        }
00054        for (k = i&(NBITS-1); k--; ss >>= 1)
00055               left += ss & 1;
00056        if (ss & 1)
00057               return(s-left-1);
00058        return(s-left+i);
00059 }