Changeset 1919 for trunk


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

Simplified hash functions.

Location:
trunk/lib
Files:
2 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
  • trunk/lib/test/hash_test.cpp

    r1918 r1919  
    101101    */
    102102
    103     /*
    104     for(uint32_t mask_bits=1;mask_bits<64;mask_bits++) {
    105         r = byte_mask_hash((uint8_t *) &b0, 0, mask_bits);
    106         print_register("r", r);
    107     }
    108     */
    109 
    110     // Length 8 (bits)
    111     /*
    112     hash_size = 8;
    113     for(uint32_t pos=0;pos<sizeof(uint64_t);pos++) {
    114 
    115         r = bit_compress_hash((uint8_t *) &b0,(uint8_t *) &b2, pos*8, 8, hash_size);
    116         cout << dec << "bit_compress_hash = " << r << endl << endl;
    117         cout << endl << endl;
    118 
    119     }
    120     */
    121 
    122     /*
    123103    uint8_t pos, slice;
    124104    hash_size = 8;
     
    126106    slice = 16;
    127107    pos = 0;
    128     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     108    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    129109
    130110    pos = 1;
    131     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     111    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    132112
    133113    slice = 24;
    134114    pos = 0;
    135     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     115    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    136116
    137117    pos = 1;
    138     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     118    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    139119
    140120    slice = 32;
    141121
    142122    pos = 0;
    143     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     123    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    144124
    145125    pos = 1;
    146     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     126    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    147127
    148128    pos = 2;
    149     r = byte_compress_hash((uint8_t *) &b0, pos, slice, hash_size);
    150     */
     129    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     130
     131    pos = 0;
     132    slice = 8*10;
     133    hash_size = 16;
     134    r = compress_hash((uint8_t *) &b0, pos, slice, hash_size);
     135
    151136
    152137    return 1;
Note: See TracChangeset for help on using the changeset viewer.