/* Obtain a value in [0..val-1] from a PRNG. < Like other alternatives to MOD, LIM exhibits a slightly inferior distribution over the range of possible values while Sigma approaches zero more slowly than with MOD. Lim is a shade slower than MOD, but there's not much in it. Given a high quality RNG, Lim is otherwise unbiased. */ static ub4 Lim(enum CSPRNG ng) { register ub4 dv = 0xFFFFFFFF/MODU; register ub4 n; do { n = rRandom(ng) / dv; } while (n > MODM1); return n; }
/* Obtain a value in [0..val-1] from a pseudo-random bitstream by sampling b-bit segments as n until n is in range. < Like other alternatives to MOD, SAM exhibits a slightly inferior distribution over the range of possible values while Sigma approaches zero more slowly than with MOD. tcc : SAM w/ MODBITS constant is the same speed as MOD; cl : SAM w/ MODBITS constant is 1.5 to 2.1 x faster than MOD. Given a high quality RNG, SAM is otherwise unbiased. > */ static ub4 Sam(enum CSPRNG ng) { register ub4 m,n,b,r; register long int i; int f=0; b = MODBITS; for(;;) { r = rRandom(ng); i = -b; for(;;) { i+=b; m = ((1 << b) - 1) << i; n = (r & m) >> i; f = n<MODU; if (f || i>=32-b) break; } if (f) break; } return n; }
void main( int argc, char **argv) { char match; char **remArgs; int rv; GrScreenResolution_t resolution = GR_RESOLUTION_640x480; float scrWidth = 640.0f; float scrHeight = 480.0f; int frames = -1; GrContext_t gc = 0; static unsigned short colorBuf[64][64]; static unsigned short grabBuf[64][64]; int x,y; /* Initialize Glide */ grGlideInit(); assert( hwconfig = tlVoodooType() ); /* Process Command Line Arguments */ while( rv = tlGetOpt( argc, argv, "nr", &match, &remArgs ) ) { if ( rv == -1 ) { printf( "Unrecognized command line argument\n" ); printf( "%s %s\n", name, usage ); printf( "Available resolutions:\n%s\n", tlGetResolutionList() ); return; } switch( match ) { case 'n': frames = atoi( remArgs[0] ); break; case 'r': resolution = tlGetResolutionConstant( remArgs[0], &scrWidth, &scrHeight ); break; } } tlSetScreen( scrWidth, scrHeight ); version = grGetString( GR_VERSION ); printf( "%s:\n%s\n", name, purpose ); printf( "%s\n", version ); printf( "Resolution: %s\n", tlGetResolutionString( resolution ) ); if ( frames == -1 ) { printf( "Press A Key To Begin Test.\n" ); tlGetCH(); } grSstSelect( 0 ); gc = grSstWinOpen(tlGethWnd(), resolution, GR_REFRESH_60Hz, GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ); if (!gc) { printf("Could not allocate glide fullscreen context.\n"); goto __errExit; } tlConSet( 0.0f, 0.0f, 1.0f, 0.5f, 60, 15, 0xffffff ); /* Set up Render State - disable dithering*/ grDitherMode( GR_DITHER_DISABLE ); /* Create Source Bitmap to be copied to framebuffer */ for( y = 0; y < 64; y++ ) { for( x = 0; x < 64; x++ ) { FxU8 red = x << 2; FxU8 grn = y << 2; FxU8 blu = ( x + y )<<1; colorBuf[y][x] = (red & 0xF8) << 8; colorBuf[y][x] |= (grn & 0xFC) << 3; colorBuf[y][x] |= (blu & 0xF8) >> 3; } } tlConOutput( "Press any key to quit\n" ); while( frames-- && tlOkToRender()) { GrLfbInfo_t info; int startX, startY; if (hwconfig == TL_VOODOORUSH) { tlGetDimsByConst(resolution, &scrWidth, &scrHeight ); grClipWindow(0, 0, (FxU32) scrWidth, (FxU32) scrHeight); } grBufferClear( 0x00, 0, 0 ); /* prepare info structure */ info.size = sizeof( GrLfbInfo_t ); /* lock back buffer */ if ( grLfbLock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER, GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE, &info )==FXFALSE) { tlConOutput( "Error, failed to take write lock\n" ); break; } if ( tlScaleX(1.0f) < 64.0 || tlScaleY(1.0f) < 64.0 ) return; /* generate random start position */ startX = (int)rRandom( 64, (int)tlScaleX(1.0f) - 65 ); startY = (int)rRandom( 64, (int)tlScaleY(1.0f) - 65 ); /* render image to back buffer */ for( y = 0; y < 64; y++ ) { for( x = 0; x < 64; x++ ) { FxU16 *pixel = (FxU16*)(((char*)info.lfbPtr) + (y+startY)*info.strideInBytes+ (x+startX)*2); *pixel = colorBuf[y][x]; } } /* unlock the backbuffer */ grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_BACKBUFFER ); /* swap to front buffer */ grBufferSwap( 1 ); grBufferClear( 0,0,0 ); tlSleep( 1 ); /* lock the front buffer */ if ( grLfbLock( GR_LFB_READ_ONLY, GR_BUFFER_FRONTBUFFER, GR_LFBWRITEMODE_ANY, GR_ORIGIN_UPPER_LEFT, FXFALSE, &info )==FXFALSE) { tlConOutput( "Error, failed to take read lock\n" ); break; } /* grab the source image out of the front buffer */ for( y = 0; y < 64; y++ ) { for( x = 0; x < 64; x++ ) { FxU16 *pixel = (FxU16*)(((char*)info.lfbPtr) + (y+startY)*info.strideInBytes+ (x+startX)*2); grabBuf[y][x] = *pixel; } } /* unlock the front buffer */ grLfbUnlock( GR_LFB_READ_ONLY, GR_BUFFER_FRONTBUFFER ); tlConClear(); /* compare the source image to the readback image */ if ( memcmp( colorBuf, grabBuf, sizeof( colorBuf ) ) ) { tlConOutput( "Failed readback test\n" ); } else { tlConOutput( "Passed readback test\n" ); } grBufferSwap( 1 ); tlConOutput( "Press any key to quit\n" ); tlConRender(); grBufferSwap( 1 ); tlSleep( 1 ); while( tlKbHit() ) { switch( tlGetCH() ) { default: frames = 0; break; } } } __errExit: grGlideShutdown(); return; }
void main( int argc, char **argv) { char match; char **remArgs; int rv; GrScreenResolution_t resolution = GR_RESOLUTION_640x480; float scrWidth = 640.0f; float scrHeight = 480.0f; int frames = -1; int doNothing = 0; int cycles; FxU32 wrange[2]; /* Initialize Glide */ grGlideInit(); assert( hwconfig = tlVoodooType() ); /* Process Command Line Arguments */ while( rv = tlGetOpt( argc, argv, "Nnr", &match, &remArgs ) ) { if ( rv == -1 ) { printf( "Unrecognized command line argument\n" ); printf( "%s %s\n", name, usage ); printf( "Available resolutions:\n%s\n", tlGetResolutionList() ); return; } switch( match ) { case 'n': frames = atoi( remArgs[0] ); break; case 'r': resolution = tlGetResolutionConstant( remArgs[0], &scrWidth, &scrHeight ); break; case 'N': doNothing = 1; break; } } tlSetScreen( scrWidth, scrHeight ); version = grGetString( GR_VERSION ); printf( "%s:\n%s\n", name, purpose ); printf( "%s\n", version ); printf( "Resolution: %s\n", tlGetResolutionString( resolution ) ); if ( frames == -1 ) { printf( "Press A Key To Begin Test.\n" ); tlGetCH(); } grSstSelect( 0 ); cycles = 0; while( frames-- && tlOkToRender()) { GrVertex a, b, c; GrContext_t context; char inchar; context = grSstWinOpen(tlGethWnd(), resolution, GR_REFRESH_60Hz, GR_COLORFORMAT_ABGR, GR_ORIGIN_UPPER_LEFT, 2, 1 ); assert(context); /* * Don't like gotos? In the immortal words of Schwarzenegger * (Total Recall): "...so sue me d**khead" */ if (doNothing) goto doNothing; grVertexLayout(GR_PARAM_XY, 0, GR_PARAM_ENABLE); grVertexLayout(GR_PARAM_RGB, GR_VERTEX_R_OFFSET << 2, GR_PARAM_ENABLE); grGet(GR_WDEPTH_MIN_MAX, 8, wrange); grColorCombine( GR_COMBINE_FUNCTION_LOCAL, GR_COMBINE_FACTOR_NONE, GR_COMBINE_LOCAL_ITERATED, GR_COMBINE_OTHER_NONE, FXFALSE ); tlConSet( 0.0f, 0.0f, 1.0f, 1.0f, 60, 30, 0xffffff ); /* deal with dynamic resizing */ if (hwconfig == TL_VOODOORUSH) { tlGetDimsByConst(resolution, &scrWidth, &scrHeight); grClipWindow(0, 0, (FxU32) scrWidth, (FxU32) scrHeight); } tlConOutput( "Press <space> to cycle hardware\n" ); tlConOutput( "Any other key to quit\n" ); grBufferClear( 0x000000, 0, wrange[1] ); a.r = a.g = a.b = 0.0f; b = c = a; a.x = tlScaleX(((float)rRandom( 0, 100 ))/100.0f); a.y = tlScaleY(((float)rRandom( 0, 100 ))/100.0f); a.r = 255.0f; b.x = tlScaleX(((float)rRandom( 0, 100 ))/100.0f); b.y = tlScaleY(((float)rRandom( 0, 100 ))/100.0f); b.g = 255.0f; c.x = tlScaleX(((float)rRandom( 0, 100 ))/100.0f); c.y = tlScaleY(((float)rRandom( 0, 100 ))/100.0f); c.b = 255.0f; grDrawTriangle( &a, &b, &c ); tlConOutput( "Cycle: %d\r", cycles ); tlConRender(); grBufferSwap( 1 ); doNothing: inchar = tlGetCH(); if ( inchar != ' ' ) frames = 0; grSstWinClose(context); cycles++; } grGlideShutdown(); return; }
int main(int argc, char *argv[]) { ub4 tots=0; ub4 ts, r, bts, div; ub4 q1 = 24; ub4 q2 = 28; double totSigma=0.0; double totRange=0.0; double sigma,range,mSigma,mRange; // timer time_t a,z; // rounds qi = pow(2,q1); // check the command line if (argc<=1) { usage(); exit(0); } if (argc>=2) q1 = atoi(argv[1]); if (argc>=3) q2 = atoi(argv[2]); if (argc>=4) rng= atoi(argv[3]) % 9; if (argc>=5) strcpy(s,argv[4]); #ifdef TEST #ifdef __TINYC__ puts("Tiny C"); #endif #ifdef __WATCOMC__ puts("Open Watcom C"); #endif #ifdef _MSC_VER puts("Microsoft Visual C"); #endif #ifdef __GNUC__ puts("GNU C"); #endif #endif #if __STDC_VERSION__ >= 199901L puts("C99 supported."); #endif #ifdef MOD printf("MOD: "); #endif #ifdef LIM printf("LIM: "); #endif #ifdef SAM printf("SAM: "); #endif div = q2-q1+1; printf("%d %s trial sets in [2**%d..2**%d]\n",div,RNGs[rng],q1,q2); puts("Trial Range Sigma Time"); puts("------------------------------------------"); for (j=q1; j<=q2; j++) { for (i=0; i<MODU; i++) totals[i]=0; probtot = 0.0; qi = pow(2,j); rSeed(rng,s,rStateSize(rng)*7); time(&a); for (q=0; q<qi; q++) { #ifdef MOD r=rRandom(rng) % MODU; #else #ifdef LIM r=Lim(rng); #endif r=Sam(rng); #endif totals[r]++; } time(&z); ts=(size_t)(z-a); tots+=ts; for (i=0; i<MODU; i++) { // expected probabilities expect[i] = (double)1/MODU; // actual probabilities prob[i]=(double)totals[i]/qi; // probtot holds total of probabilities - it should converge to 1.0 probtot=probtot+prob[i]; // collect value-names & decide output format values[i] = i + 'A'; } sigma = Sigma(0,MODM1); range = prob[MaxP(0,MODM1)]-prob[MinP(0,MODM1)]; totSigma+=sigma; totRange+=range; printf("2**%d %1.7f %1.7f %3d s\n",j,range,sigma,ts); } puts("------------------------------------------"); mSigma = (double)totSigma/div; mRange = (double)totRange/div; printf("Mean: %1.7f %1.7f %4d s\n",mRange,mSigma,tots); #ifdef TEST puts(""); for (i=0; i<MODU; i++) printf("%2c) %1.7f\n",values[i],prob[i]); #endif return 0; }