Changeset 1919 for trunk/lib/hash.hpp


Ignore:
Timestamp:
Feb 11, 2012, 12:01:17 AM (7 years ago)
Author:
ksherdy
Message:

Simplified hash functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/hash.hpp

    r1918 r1919  
    1111                Internal static casts of uint8_t * b0 to uint64_t * and deference. */
    1212
     13static uint32_t bytes2bits(uint32_t bit) { return bit * 8; }
    1314
    1415static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size);
    15 static IDISA_ALWAYS_INLINE uint64_t byte_mask_hash(const uint8_t *b0, const uint32_t pos, const uint32_t hash_size);
     16//static IDISA_ALWAYS_INLINE uint64_t byte_mask_hash(const uint8_t *b0, const uint32_t pos, const uint32_t hash_size);
    1617
    1718/* pos (bits) */
    1819static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size);
    19 static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size);
     20/* pos (bytes) */
     21//static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size);
    2022
    21 /* pos (bytes) */
    22 static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size);
    23 static IDISA_ALWAYS_INLINE uint64_t byte_compress_hash(const uint8_t * b0, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size);
     23/* pos (bits) */
     24static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * b0, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size);
    2425
    2526static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size) {
     
    3435}
    3536
    36 static IDISA_ALWAYS_INLINE uint64_t byte_mask_hash(const uint8_t *b0, const uint32_t pos, const uint32_t hash_size) {
    37     uint64_t mask = gen_mask(hash_size);
    38     return *((uint64_t *)(b0 + pos)) & mask;
    39 }
     37//static IDISA_ALWAYS_INLINE uint64_t byte_mask_hash(const uint8_t *b0, const uint32_t pos, const uint32_t hash_size) {
     38//    uint64_t mask = gen_mask(hash_size);
     39//    return *((uint64_t *)(b0 + pos)) & mask;
     40//}
    4041
    4142/* pos (bits) */
     
    5657
    5758/* pos (bytes) */
     59/*
    5860static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size) {
    5961    assert(slice_size > 0 && slice_size <= 64);
     
    6971    return r;
    7072}
     73*/
    7174
    72 static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size) {
    73     assert(slice_size > 0 && slice_size <= 64);
    74     assert(slice_size >= hash_size); /* negative shifts undefined in C standard, (x1 >> shift) */
     75static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * b0, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size) {
     76    //assert(slice_size > 0 && slice_size <= 64);
     77    //assert(slice_size >= hash_size); // negative shifts undefined in C standard
    7578
    76 #ifndef NDEBUG
    77     cout << endl;
    78     cout << "Stream pos (bit):  " << pos << endl;
    79     cout << "Slice size (bits): "  << slice_size << endl;
    80     cout << "Hash size (bits):   " << hash_size << endl;
    81 #endif
    82 
    83     uint64_t x0 = bit_offset_slice(b0,pos,slice_size);
    84     uint64_t x1 = bit_offset_slice(b1,pos,slice_size);
    85     uint64_t shift = (slice_size - hash_size);
    86     uint64_t mask = gen_mask(hash_size);
    87     uint64_t r = (x0 ^ (x1 >> shift)) & mask;
    88 
    89 #ifndef NDEBUG
    90     print_register<uint64_t>("b0", *(uint64_t *)b0);
    91     print_register<uint64_t>("b1", *(uint64_t *)b1);
    92     print_register<uint64_t>("x0", x0);
    93     print_register<uint64_t>("x1", x1);
    94     print_register<uint64_t>("shift x1", (x1 >> shift));
    95     print_register<uint64_t>("r", r);
    96 #endif
    97 
    98     return r;
    99 }
    100 
    101 static IDISA_ALWAYS_INLINE uint64_t byte_compress_hash(const uint8_t * b0, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size) {
    102     assert(slice_size > 0 && slice_size <= 64);
    103     assert(slice_size >= hash_size); // negative shifts undefined in C standard
     79    assert(hash_size > 0 && hash_size <= 64);
     80    assert(slice_size >= hash_size);
    10481
    10582#ifndef NDEBUG
     
    11087#endif
    11188
    112     uint64_t x0 = *((uint64_t *)(b0+pos));
    113     uint64_t shift = (slice_size - hash_size);  // negative shifts undefined in C standard
     89    uint64_t x0 = bit_offset_slice(b0,pos,hash_size);
     90    uint64_t x1 = bit_offset_slice(b0,pos+slice_size-hash_size,hash_size);
     91
     92    //uint64_t x0 = //*((uint64_t *)(b0+pos));
     93    //uint64_t shift = (slice_size - hash_size);  // negative shifts undefined in C standard
    11494    uint64_t mask = gen_mask(hash_size);
    115     uint64_t r = (x0 ^ (x0 >> shift)) & mask;
     95    uint64_t r = (x0 ^ x1) & mask;
    11696
    11797#ifndef NDEBUG
    11898    print_register<uint64_t>("b0", *(uint64_t *)(b0));
    11999    print_register<uint64_t>("x0", x0);
    120     print_register<uint64_t>("x0 >> shift", x0>>shift);
     100    //print_register<uint64_t>("x0 >> shift", x0>>shift);
     101    print_register<uint64_t>("x1", x1);
    121102    print_register<uint64_t>("r", r);
    122103#endif
Note: See TracChangeset for help on using the changeset viewer.