MongoDB  2.7.0
checksum.h
1 
29 #pragma once
30 
31 #include "mongo/pch.h"
32 
33 namespace mongo {
35  struct Checksum {
36  union {
37  unsigned char bytes[16];
38  unsigned long long words[2];
39  };
40 
41  // if you change this you must bump dur::CurrentVersion
42  void gen(const void *buf, unsigned len) {
43  wassert( ((size_t)buf) % 8 == 0 ); // performance warning
44  unsigned n = len / 8 / 2;
45  const unsigned long long *p = (const unsigned long long *) buf;
46  unsigned long long a = 0;
47  for( unsigned i = 0; i < n; i++ ) {
48  a += (*p ^ i);
49  p++;
50  }
51  unsigned long long b = 0;
52  for( unsigned i = 0; i < n; i++ ) {
53  b += (*p ^ i);
54  p++;
55  }
56  unsigned long long c = 0;
57  for( unsigned i = n * 2 * 8; i < len; i++ ) { // 0-7 bytes left
58  c = (c << 8) | ((const char *)buf)[i];
59  }
60  words[0] = a ^ len;
61  words[1] = b ^ c;
62  }
63 
64  bool operator==(const Checksum& rhs) const { return words[0]==rhs.words[0] && words[1]==rhs.words[1]; }
65  bool operator!=(const Checksum& rhs) const { return words[0]!=rhs.words[0] || words[1]!=rhs.words[1]; }
66  };
67 }
a simple, rather dumb, but very fast checksum.
Definition: checksum.h:35