# HG changeset patch # User Me # Date 1290238899 -3600 # Node ID c83c27796fad1ff6bae9afdb3ca8eb1f18ab91cb # Parent fa6a281bd8548ba17e39c0a25b90d9c22ca9dd7d Added sub from hist, added "total" to print and return when bar is zero diff -r fa6a281bd854 -r c83c27796fad Histogram.c --- a/Histogram.c Sun Nov 14 11:11:44 2010 -0800 +++ b/Histogram.c Sat Nov 20 08:41:39 2010 +0100 @@ -109,6 +109,25 @@ hist->bins[ binIdx ] += 1; } +void inline +subFromHist( int32 value, Histogram *hist ) + { + int32 binIdx; + + if( value < hist->startOfRange ) + { binIdx = 0; + } + else if( value > hist->endOfRange ) + { binIdx = hist->numBins - 1; + } + else + { + binIdx = (value - hist->startOfRange) / hist->binWidth; + } + + hist->bins[ binIdx ] -= 1; + } + /*Inline because use with RDTSC in innermost code so need ultra-fast */ @@ -122,13 +141,24 @@ addToHist( value, hist ); } +void inline +subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) + { + int32 value; + + value = endIntvl - startIntvl; + if( value < 0 || value > 10000000 ) return; //sanity check + subFromHist( value, hist ); + } + void printHist( Histogram *hist ) { int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; - float32 total, binPercent, expectedValue1, expectedValue2; + float32 total, total2, binPercent, expectedValue1, expectedValue2; - + if( hist == NULL ) return; + //do all except the top bin maxHeight = 0; total = 0.0; expectedValue1 = 0.0; for( i = 0; i < hist->numBins -1; i++ ) @@ -139,9 +169,9 @@ expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); } //copy and calc expected value minus the top bin - expectedValue2 = expectedValue1; + expectedValue2 = expectedValue1; expectedValue2 /= total; - + total2 = total; //now do last iteration, to add the top bin if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; total += hist->bins[ i ]; @@ -155,10 +185,12 @@ printf( "histogram: " ); if( hist->name != NULL ) printf( "%s\n", hist->name ); else printf( "\n" ); - printf( "expected value: %3.2f \n", expectedValue1 ); - printf( "expected value minus top bin: %3.2f \n", expectedValue2 ); + printf( " num samples: %d | expected value: %3.2f \n", + (int)total, expectedValue1 ); + printf( "minus top bin, num samples: %d | expected value: %3.2f \n", + (int)total2, expectedValue2 ); - if( barValue == 0 ) printf("error printing histogram\n"); + if( barValue == 0 ) { printf("error: bar val zero\n"); return; } for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) { binStart = hist->startOfRange + hist->binWidth * binIdx; diff -r fa6a281bd854 -r c83c27796fad Histogram.h --- a/Histogram.h Sun Nov 14 11:11:44 2010 -0800 +++ b/Histogram.h Sat Nov 20 08:41:39 2010 +0100 @@ -59,6 +59,9 @@ void inline addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ); +void inline +subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist ); + void printHist( Histogram *hist );