Changeset 1936


Ignore:
Timestamp:
Feb 16, 2012, 8:56:53 PM (7 years ago)
Author:
ksherdy
Message:

Updated hash functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/hash.hpp

    r1919 r1936  
    99
    1010/* WARNING:     Pad 8 bytes.
    11                 Internal static casts of uint8_t * b0 to uint64_t * and deference. */
     11                Internal static casts of uint8_t * base to uint64_t * and deference. */
    1212
    13 static uint32_t bytes2bits(uint32_t bit) { return bit * 8; }
     13static int32_t bytes2bits(int32_t bits) { return bits * 8; }
    1414
    1515static IDISA_ALWAYS_INLINE uint64_t gen_mask(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);
    1716
    18 /* pos (bits) */
    19 static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_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);
     17/* offset (bits) */
     18static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size);
    2219
    23 /* pos (bits) */
    24 static 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);
     20/* offset (bytes) */
     21//static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size);
     22
     23/* offset (bits) */
     24static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * base, const int32_t offset, const uint32_t slice_size, const uint32_t hash_size);
    2525
    2626static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size) {
     
    3535}
    3636
    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 //}
     37/* offset (bits) */
     38static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size) {
     39    assert(slice_size > 0 && slice_size <= 64);
     40    assert(offset & (8-1) >= 0); // negative shifts undefined in C standard
    4141
    42 /* pos (bits) */
    43 static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size) {
    44     assert(slice_size > 0 && slice_size <= 64);
    45 
    46     uint64_t shift = *((uint64_t *)(buffer + (pos/8))) >> (pos & 8-1);
     42    uint64_t shift = *((uint64_t *)(base + (offset/8))) >> (offset & (8-1));
    4743    uint64_t mask = gen_mask(slice_size);
    4844    uint64_t r = shift & mask;
     
    5652}
    5753
    58 /* pos (bytes) */
     54static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * base, const int32_t offset, const uint32_t slice_size, const uint32_t hash_size) {
     55    //assert(slice_size > 0 && slice_size <= 64);
     56    //assert(slice_size >= hash_size); // negative shifts undefined in C standard
     57
     58    assert(hash_size > 0 && hash_size <= 64);
     59    assert(slice_size >= hash_size);
     60
     61#ifndef NDEBUG
     62    cout << endl;
     63    cout << "Stream offset (byte):  " << offset << endl;
     64    cout << "Slice size (bits): "  << slice_size << endl;
     65    cout << "Hash size (bits):   " << hash_size << endl;
     66#endif
     67
     68    uint64_t x0 = bit_offset_slice(base,offset,hash_size);
     69    uint64_t x1 = bit_offset_slice(base,offset+slice_size-hash_size,hash_size);
     70
     71    //uint64_t x0 = //*((uint64_t *)(base+offset));
     72    //uint64_t shift = (slice_size - hash_size);  // negative shifts undefined in C standard
     73    uint64_t mask = gen_mask(hash_size);
     74    uint64_t r = (x0 ^ x1) & mask;
     75
     76#ifndef NDEBUG
     77    print_register<uint64_t>("base", *(uint64_t *)(base));
     78    print_register<uint64_t>("x0", x0);
     79    //print_register<uint64_t>("x0 >> shift", x0>>shift);
     80    print_register<uint64_t>("x1", x1);
     81    print_register<uint64_t>("r", r);
     82#endif
     83
     84    return r;
     85}
     86
     87#endif // BYTE_UTIL_HPP
     88
     89/* offset (bytes) */
    5990/*
    60 static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size) {
     91static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size) {
    6192    assert(slice_size > 0 && slice_size <= 64);
    6293
    63     uint64_t shift = *((uint64_t *)(buffer+pos));
     94    uint64_t shift = *((uint64_t *)(base+offset));
    6495    uint64_t mask = gen_mask(slice_size);
    6596    uint64_t r = shift & mask;
     
    73104*/
    74105
    75 static 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
    78 
    79     assert(hash_size > 0 && hash_size <= 64);
    80     assert(slice_size >= hash_size);
    81 
    82 #ifndef NDEBUG
    83     cout << endl;
    84     cout << "Stream pos (byte):  " << pos << endl;
    85     cout << "Slice size (bits): "  << slice_size << endl;
    86     cout << "Hash size (bits):   " << hash_size << endl;
    87 #endif
    88 
    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
    94     uint64_t mask = gen_mask(hash_size);
    95     uint64_t r = (x0 ^ x1) & mask;
    96 
    97 #ifndef NDEBUG
    98     print_register<uint64_t>("b0", *(uint64_t *)(b0));
    99     print_register<uint64_t>("x0", x0);
    100     //print_register<uint64_t>("x0 >> shift", x0>>shift);
    101     print_register<uint64_t>("x1", x1);
    102     print_register<uint64_t>("r", r);
    103 #endif
    104 
    105     return r;
    106 }
    107 
    108 #endif // BYTE_UTIL_HPP
    109 
    110106/*
    111 static IDISA_ALWAYS_INLINE uint64_t bit_expand_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size);
    112 static IDISA_ALWAYS_INLINE uint64_t bit_expand_hash(const uint8_t * b0, const uint8_t * b1, const uint32_t pos, const uint32_t slice_size, const uint32_t hash_size) {
     107static IDISA_ALWAYS_INLINE uint64_t bit_expand_hash(const uint8_t * base, const uint8_t * base1, const int32_t offset, const uint32_t slice_size, const uint32_t hash_size);
     108static IDISA_ALWAYS_INLINE uint64_t bit_expand_hash(const uint8_t * base, const uint8_t * base1, const int32_t offset, const uint32_t slice_size, const uint32_t hash_size) {
    113109    assert(slice_size > 0 && slice_size <= 64);
    114110    //assert(slice_size <= hash_size);
    115111
    116     uint64_t x0 = bit_offset_slice(b0,pos,slice_size);
    117     uint64_t x1 = bit_offset_slice(b1,pos,slice_size);
     112    uint64_t x0 = bit_offset_slice(base,offset,slice_size);
     113    uint64_t x1 = bit_offset_slice(base1,offset,slice_size);
    118114    uint64_t mask = gen_mask(hash_size);
    119115
     
    130126
    131127#ifndef NDEBUG
    132     cout << "Stream pos (bit):  " << pos << endl;
     128    cout << "Stream offset (bit):  " << offset << endl;
    133129    cout << "Symbol lgth (bits): " << slice_size << endl;
    134130    cout << "Hash size (bits):   " << hash_size << endl;
    135131    cout << "Shift (bits): " << shift << endl;
    136132
    137     print_register<uint64_t>("b0", *(uint64_t *)b0);
    138     print_register<uint64_t>("b1", *(uint64_t *)b1);
     133    print_register<uint64_t>("base", *(uint64_t *)base);
     134    print_register<uint64_t>("base1", *(uint64_t *)base1);
    139135    print_register<uint64_t>("x0", x0);
    140136    print_register<uint64_t>("x1", x1);
Note: See TracChangeset for help on using the changeset viewer.