Changeset 1912
 Timestamp:
 Feb 8, 2012, 4:27:08 PM (7 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/lib/hash_util.hpp
r1881 r1912 1 1 #ifndef BYTE_UTIL_HPP 2 2 #define BYTE_UTIL_HPP 3 4 //#define NDEBUG /* Define to remove assertions and debug statements. */5 3 6 4 #include "bitblock.hpp" … … 10 8 using namespace std; 11 9 12 /* WARNING: 10 /* 11 slice<1>  bit slice operation 12 FW=1 'pos' and 'lgth' args in terms of bits, 13 e.g. pos=1,lgth=7 => begin at bit index 1 and slice symbol of length 7 bits. 13 14 14 FW=1 indicates 'idx' and 'lgth' arguments are expressed in terms of bits. 15 FW=8 indicates 'idx' and 'lgth' arguments are expressed in terms of bytes. 15 slice<8>  byte slice operation 16 FW=8 'pos' and 'lgth' args in terms of bytes, i.e. byte at index 1 17 e.g. pos=1,lgth=4 => begin at byte index position 1 and slice symbol of length 4 bytes. 16 18 17 slice<1>  performs a bit slice operation 18 slice<8>  performs a byte slice operation 19 20 hash<1>  returns a hash value based on bit indexed and bit length symbols 21 hash<8>  returns a hash value based on byte indexed and byte length symbols 22 23 Assumes 64 bits safety. That is, it is safe to dereference 64 bits beyond 24 the address of the indexed position (&buffer[0] + idx (bitsbytes)). 19 shift_hash<1>  returns a hash value based on bit index and symbol length 20 shift_hash<8>  returns a hash value based on byte index and symbol length 25 21 */ 26 22 23 /* WARNING: Casts uint8_t * buffer at pos to uint64_t * and deferences. Segmentation faults w/o sufficient buffer padding. */ 27 24 template<uint32_t FW> 28 IDISA_ALWAYS_INLINE uint64_t slice(const uint8_t * buffer, const uint32_t idx, const uint32_t bytes);25 IDISA_ALWAYS_INLINE uint64_t slice(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth); 29 26 30 /* WARNING: Length of Symbol Bits >= Length of Hash Bits. */31 27 template<uint32_t FW> 32 IDISA_ALWAYS_INLINE uint64_t hash(const uint8_t * b0, const uint8_t * b1, const uint32_t idx, const uint32_t lgth, const uint32_t hash_bits);28 IDISA_ALWAYS_INLINE uint64_t shift_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t lgth, const uint32_t hash_bits); 33 29 34 30 template<> 35 IDISA_ALWAYS_INLINE uint64_t slice<1>(const uint8_t * buffer, const uint32_t idx, const uint32_t lgth) { 36 assert(lgth > 0); 37 assert((idx+lgth)<((sizeof(uint64_t)*8)+1)); 31 IDISA_ALWAYS_INLINE uint64_t slice<1>(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth) { 32 assert(lgth > 0 && lgth <= 64); 38 33 39 uint64_t shift = *((uint64_t *)(buffer + (idx/8))) >> (idx & 81); 40 uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)*8  lgth)); /* WARNING: Bitwise full register shift is compiler depedendent. 41 e.g. ~0 << 64 vs ~0 << n for n=64. */ 34 uint64_t shift = *((uint64_t *)(buffer + (pos/8))) >> (pos & 81); 35 uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)*8  lgth)); 42 36 #ifndef NDEBUG 43 37 print_register<uint64_t>("shift", shift); … … 48 42 49 43 template<> 50 IDISA_ALWAYS_INLINE uint64_t slice<8>(const uint8_t * buffer, const uint32_t idx, const uint32_t lgth) { 51 assert(lgth > 0); 52 assert((idx+(lgth*8)) < ((sizeof(uint64_t)*8)+1)); 44 IDISA_ALWAYS_INLINE uint64_t slice<8>(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth) { 45 assert(lgth > 0 && lgth <= 8); 53 46 54 uint64_t shift = *((uint64_t *)(buffer+idx)); 55 uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)  lgth)*8); /* WARNING: Bitwise full register shift is compiler depedendent. 56 e.g. ~0 << 64 vs ~0 << n for n=64. */ 47 uint64_t shift = *((uint64_t *)(buffer+pos)); 48 uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)  lgth)*8); 57 49 #ifndef NDEBUG 58 50 print_register<uint64_t>("shift", shift); … … 64 56 /* WARNING: Length of Symbol Bits >= Length of Hash Bits. */ 65 57 template<uint32_t FW> 66 IDISA_ALWAYS_INLINE uint64_t hash(const uint8_t * b0, const uint8_t * b1, const uint32_t idx, const uint32_t lgth, const uint32_t hash_bits) {58 IDISA_ALWAYS_INLINE uint64_t shift_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t lgth, const uint32_t hash_bits) { 67 59 68 assert(lgth > 0); 69 assert((idx+(lgth*FW)) < ((sizeof(uint64_t)*8)+1)); 60 assert(lgth > 0 && lgth*FW <= 64); 70 61 assert((lgth*FW) >= hash_bits); 71 62 72 uint64_t x0 = slice<FW>(b0, idx,lgth);73 uint64_t x1 = slice<FW>(b1, idx,lgth);63 uint64_t x0 = slice<FW>(b0,pos,lgth); 64 uint64_t x1 = slice<FW>(b1,pos,lgth); 74 65 uint64_t mask = (1 << hash_bits)  1; 75 66 76 67 #ifndef NDEBUG 77 cout << " Index Lgth: " << idx<< endl;68 cout << "Position: " << pos << endl; 78 69 cout << "Symbol Lgth: " << lgth << endl; 79 70 cout << "Hash Bits: " << hash_bits << endl; 71 80 72 uint64_t masked_x0 = x0 & mask; 81 73 uint64_t shift_masked_x1 = (x1 >> ((lgth*FW)  hash_bits)) & mask; 74 82 75 print_register<uint64_t>("b0", *(uint64_t *)b0); 83 76 print_register<uint64_t>("b1", *(uint64_t *)b1); … … 92 85 } 93 86 87 xor_hash(); 88 xor 94 89 95 90 #endif // BYTE_UTIL_HPP
Note: See TracChangeset
for help on using the changeset viewer.