summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraappleby@google.com <aappleby@google.com@77a7d1d3-4c08-bdc2-d393-d5859734b01a>2011-03-21 20:55:06 +0000
committeraappleby@google.com <aappleby@google.com@77a7d1d3-4c08-bdc2-d393-d5859734b01a>2011-03-21 20:55:06 +0000
commit7f20a31b739a5465934433593061eb6c64ee9997 (patch)
tree800b8b11e492eca6055d6b2911cf6ba175be1a35
parent3ee4561b1347b8ad7a6cda7210c640d9288df957 (diff)
downloadsrc-7f20a31b739a5465934433593061eb6c64ee9997.tar.gz
verification code work
git-svn-id: http://smhasher.googlecode.com/svn/trunk@96 77a7d1d3-4c08-bdc2-d393-d5859734b01a
-rw-r--r--AvalancheTest.h12
-rw-r--r--Bitvec.cpp12
-rw-r--r--CMakeLists.txt2
-rw-r--r--KeysetTest.cpp10
-rw-r--r--KeysetTest.h4
-rw-r--r--SpeedTest.cpp23
-rw-r--r--SpeedTest.h4
-rw-r--r--main.cpp43
8 files changed, 83 insertions, 27 deletions
diff --git a/AvalancheTest.h b/AvalancheTest.h
index 966f8b0..30bc6ea 100644
--- a/AvalancheTest.h
+++ b/AvalancheTest.h
@@ -25,7 +25,7 @@ double maxBias ( std::vector<int> & counts, int reps );
//-----------------------------------------------------------------------------
template < typename keytype, typename hashtype >
-void calcBias ( pfHash hash, std::vector<int> & counts, int reps )
+void calcBias ( pfHash hash, std::vector<int> & counts, int reps, Rand & r )
{
const int keybytes = sizeof(keytype);
const int hashbytes = sizeof(hashtype);
@@ -40,7 +40,7 @@ void calcBias ( pfHash hash, std::vector<int> & counts, int reps )
{
if(irep % (reps/10) == 0) printf(".");
- rand_p(&K,keybytes);
+ r.rand_p(&K,keybytes);
hash(&K,keybytes,0,&A);
@@ -68,6 +68,8 @@ void calcBias ( pfHash hash, std::vector<int> & counts, int reps )
template < typename keytype, typename hashtype >
bool AvalancheTest ( pfHash hash, const int reps )
{
+ Rand r(48273);
+
const int keybytes = sizeof(keytype);
const int hashbytes = sizeof(hashtype);
@@ -80,7 +82,7 @@ bool AvalancheTest ( pfHash hash, const int reps )
std::vector<int> bins(keybits*hashbits,0);
- calcBias<keytype,hashtype>(hash,bins,reps);
+ calcBias<keytype,hashtype>(hash,bins,reps,r);
//----------
@@ -108,6 +110,8 @@ bool AvalancheTest ( pfHash hash, const int reps )
template< typename keytype, typename hashtype >
void BicTest ( pfHash hash, const int keybit, const int reps, double & maxBias, int & maxA, int & maxB, bool verbose )
{
+ Rand r(11938);
+
const int keybytes = sizeof(keytype);
const int hashbytes = sizeof(hashtype);
const int hashbits = hashbytes * 8;
@@ -124,7 +128,7 @@ void BicTest ( pfHash hash, const int keybit, const int reps, double & maxBias,
if(irep % (reps/10) == 0) printf(".");
}
- rand_p(&key,keybytes);
+ r.rand_p(&key,keybytes);
hash(&key,keybytes,0,&h1);
flipbit(key,keybit);
diff --git a/Bitvec.cpp b/Bitvec.cpp
index 667902d..932b200 100644
--- a/Bitvec.cpp
+++ b/Bitvec.cpp
@@ -594,6 +594,8 @@ uint32_t window32 ( void * blob, int len, int start, int count )
bool test_shift ( void )
{
+ Rand r(1123);
+
int nbits = 64;
int nbytes = nbits / 8;
int reps = 10000;
@@ -602,7 +604,7 @@ bool test_shift ( void )
{
if(j % (reps/10) == 0) printf(".");
- uint64_t a = rand_u64();
+ uint64_t a = r.rand_u64();
uint64_t b;
for(int i = 0; i < nbits; i++)
@@ -634,6 +636,8 @@ bool test_shift ( void )
template < int nbits >
bool test_window2 ( void )
{
+ Rand r(83874);
+
struct keytype
{
uint8_t bytes[nbits/8];
@@ -648,7 +652,7 @@ bool test_window2 ( void )
keytype k;
- rand_p(&k,nbytes);
+ r.rand_p(&k,nbytes);
for(int start = 0; start < nbits; start++)
{
@@ -671,6 +675,8 @@ bool test_window2 ( void )
bool test_window ( void )
{
+ Rand r(48402);
+
int reps = 10000;
for(int j = 0; j < reps; j++)
@@ -680,7 +686,7 @@ bool test_window ( void )
int nbits = 64;
int nbytes = nbits / 8;
- uint64_t x = rand_u64();
+ uint64_t x = r.rand_u64();
for(int start = 0; start < nbits; start++)
{
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7a5ecb9..9717f48 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,8 @@ project(SMHasher)
cmake_minimum_required(VERSION 2.4)
+set(CMAKE_BUILD_TYPE Release)
+
add_library(
SMHasherSupport
AvalancheTest.cpp
diff --git a/KeysetTest.cpp b/KeysetTest.cpp
index a59fda4..dda137c 100644
--- a/KeysetTest.cpp
+++ b/KeysetTest.cpp
@@ -64,6 +64,8 @@ bool VerificationTest ( pfHash hash, const int hashbits, uint32_t expected, bool
bool SanityTest ( pfHash hash, const int hashbits )
{
printf("Running sanity check 1");
+
+ Rand r(883741);
bool result = true;
@@ -92,8 +94,8 @@ bool SanityTest ( pfHash hash, const int hashbits )
uint8_t * key1 = &buffer1[pad];
uint8_t * key2 = &buffer2[pad+offset];
- rand_p(buffer1,buflen);
- rand_p(buffer2,buflen);
+ r.rand_p(buffer1,buflen);
+ r.rand_p(buffer2,buflen);
memcpy(key2,key1,len);
@@ -147,6 +149,8 @@ bool SanityTest ( pfHash hash, const int hashbits )
void AppendedZeroesTest ( pfHash hash, const int hashbits )
{
printf("Running sanity check 2");
+
+ Rand r(173994);
const int hashbytes = hashbits/8;
@@ -158,7 +162,7 @@ void AppendedZeroesTest ( pfHash hash, const int hashbits )
memset(key,0,sizeof(key));
- rand_p(key,32);
+ r.rand_p(key,32);
uint32_t h1[16];
uint32_t h2[16];
diff --git a/KeysetTest.h b/KeysetTest.h
index c2a5c33..936e535 100644
--- a/KeysetTest.h
+++ b/KeysetTest.h
@@ -248,6 +248,8 @@ bool CyclicKeyTest ( pfHash hash, int cycleLen, int cycleReps, const int keycoun
{
printf("Keyset 'Cyclic' - %d cycles of %d bytes - %d keys\n",cycleReps,cycleLen,keycount);
+ Rand r(483723);
+
std::vector<hashtype> hashes;
hashes.resize(keycount);
@@ -260,7 +262,7 @@ bool CyclicKeyTest ( pfHash hash, int cycleLen, int cycleReps, const int keycoun
for(int i = 0; i < keycount; i++)
{
- rand_p(cycle,cycleLen);
+ r.rand_p(cycle,cycleLen);
*(uint32_t*)cycle = f3mix(i ^ 0x746a94f1);
diff --git a/SpeedTest.cpp b/SpeedTest.cpp
index 16e183d..dc6d7cc 100644
--- a/SpeedTest.cpp
+++ b/SpeedTest.cpp
@@ -2,13 +2,16 @@
#include "Random.h"
-#include <stdio.h> // for printf
+#include <stdio.h> // for printf
+#include <memory.h> // for memset
//-----------------------------------------------------------------------------
// 256k blocks seem to give the best results.
-void BulkSpeedTest ( pfHash hash )
+void BulkSpeedTest ( pfHash hash, uint32_t seed )
{
+ Rand r(seed);
+
const int trials = 9999;
const int blocksize = 256 * 1024;
@@ -16,7 +19,7 @@ void BulkSpeedTest ( pfHash hash )
char * block = new char[blocksize + 16];
- rand_p(block,blocksize+16);
+ r.rand_p(block,blocksize+16);
uint32_t temp[16];
@@ -53,23 +56,30 @@ void BulkSpeedTest ( pfHash hash )
//-----------------------------------------------------------------------------
-void TinySpeedTest ( pfHash hash, int hashsize, int keysize, bool verbose, double & outCycles )
+void TinySpeedTest ( pfHash hash, int hashsize, int keysize, uint32_t seed, bool verbose, double & outCycles )
{
const int trials = 100000;
if(verbose) printf("Small key speed test - %4d-byte keys - ",keysize);
+
+ Rand r(seed);
uint8_t * h = new uint8_t[hashsize];
uint8_t * k = new uint8_t[keysize];
+
+ memset(h,0,hashsize);
+ memset(k,0,keysize);
double bestcycles = 1e9;
for(int itrial = 0; itrial < trials; itrial++)
{
- int64_t begin,end;
+ volatile int64_t begin,end;
rand_p(k,keysize);
+ MixVCode(h,4);
+
begin = rdtsc();
hash(k,keysize,itrial,h); hash(k,keysize,itrial,h); hash(k,keysize,itrial,h); hash(k,keysize,itrial,h);
@@ -94,7 +104,8 @@ void TinySpeedTest ( pfHash hash, int hashsize, int keysize, bool verbose, doubl
end = rdtsc();
- //blackhole(*(uint32_t*)(&h));
+ MixVCode(h,4);
+ //printf("0x%08x\n",g_verify);
double cycles = double(end-begin) / 64;
if((cycles > 0) && (cycles < bestcycles)) bestcycles = cycles;
diff --git a/SpeedTest.h b/SpeedTest.h
index a8f0086..b881a78 100644
--- a/SpeedTest.h
+++ b/SpeedTest.h
@@ -2,7 +2,7 @@
#include "Types.h"
-void BulkSpeedTest ( pfHash hash );
-void TinySpeedTest ( pfHash hash, int hashsize, int keysize, bool verbose, double & outCycles );
+void BulkSpeedTest ( pfHash hash, uint32_t seed );
+void TinySpeedTest ( pfHash hash, int hashsize, int keysize, uint32_t seed, bool verbose, double & outCycles );
//-----------------------------------------------------------------------------
diff --git a/main.cpp b/main.cpp
index 43e8c51..77cb771 100644
--- a/main.cpp
+++ b/main.cpp
@@ -137,14 +137,14 @@ void test ( hashfunc<hashtype> hash, HashInfo * info )
{
printf("[[[ Speed Tests ]]]\n\n");
- BulkSpeedTest(hash);
+ BulkSpeedTest(info->hash,info->verification);
printf("\n");
for(int i = 1; i < 32; i++)
{
double cycles;
- TinySpeedTest(hash,sizeof(hashtype),i,true,cycles);
+ TinySpeedTest(hashfunc<hashtype>(info->hash),sizeof(hashtype),i,info->verification,true,cycles);
}
printf("\n");
@@ -425,10 +425,28 @@ void test ( hashfunc<hashtype> hash, HashInfo * info )
//-----------------------------------------------------------------------------
+uint32_t g_inputVCode = 1;
+uint32_t g_outputVCode = 1;
+uint32_t g_resultVCode = 1;
+
+HashInfo * g_hashUnderTest = NULL;
+
+void VerifyHash ( const void * key, int len, uint32_t seed, void * out )
+{
+ g_inputVCode = MurmurOAAT(key,len,g_inputVCode);
+ g_inputVCode = MurmurOAAT(&seed,sizeof(uint32_t),g_inputVCode);
+
+ g_hashUnderTest->hash(key,len,seed,out);
+
+ g_outputVCode = MurmurOAAT(out,g_hashUnderTest->hashbits/8,g_outputVCode);
+}
+
+//-----------------------------------------------------------------------------
+
void testHash ( const char * name )
{
HashInfo * pInfo = findHash(name);
-
+
if(pInfo == NULL)
{
printf("Invalid hash '%s' specified\n",name);
@@ -436,9 +454,11 @@ void testHash ( const char * name )
}
else
{
+ g_hashUnderTest = pInfo;
+
if(pInfo->hashbits == 32)
{
- test<uint32_t>( pInfo->hash, pInfo );
+ test<uint32_t>( VerifyHash, pInfo );
}
else if(pInfo->hashbits == 64)
{
@@ -462,6 +482,12 @@ void testHash ( const char * name )
int main ( int argc, char ** argv )
{
+ if(argc < 2)
+ {
+ printf("Bad args\n");
+ exit(1);
+ }
+
SetAffinity(2);
SelfTest();
@@ -470,22 +496,23 @@ int main ( int argc, char ** argv )
g_testAll = false;
- //g_testSanity = true;
- //g_testSpeed = true;
- g_testAvalanche = true;
+ g_testSanity = true;
+ g_testSpeed = true;
+ //g_testAvalanche = true;
//g_testCyclic = true;
//g_testDiff = true;
//g_testSparse = true;
//g_testPermutation = true;
//g_testZeroes = true;
- testHash("murmur3a");
+ testHash(argv[1]);
//----------
int timeEnd = clock();
printf("\n");
+ printf("Input vcode 0x%08x, Output vcode 0x%08x, Result vcode 0x%08x\n",g_inputVCode,g_outputVCode,g_resultVCode);
printf("Verification value is 0x%08x - Testing took %f seconds\n",g_verify,double(timeEnd-timeBegin)/double(CLOCKS_PER_SEC));
printf("-------------------------------------------------------------------------------\n");
return 0;