Changeset 1912 for trunk/lib


Ignore:
Timestamp:
Feb 8, 2012, 4:27:08 PM (7 years ago)
Author:
ksherdy
Message:

Updated debug statements.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/hash_util.hpp

    r1881 r1912  
    11#ifndef BYTE_UTIL_HPP
    22#define BYTE_UTIL_HPP
    3 
    4 //#define NDEBUG /* Define to remove assertions and debug statements. */
    53
    64#include "bitblock.hpp"
     
    108using namespace std;
    119
    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.
    1314
    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.
    1618
    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 (bits|bytes)).
     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
    2521*/
    2622
     23/* WARNING: Casts uint8_t * buffer at pos to uint64_t * and deferences. Segmentation faults w/o sufficient buffer padding.  */
    2724template<uint32_t FW>
    28 IDISA_ALWAYS_INLINE uint64_t slice(const uint8_t * buffer, const uint32_t idx, const uint32_t bytes);
     25IDISA_ALWAYS_INLINE uint64_t slice(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth);
    2926
    30 /* WARNING: Length of Symbol Bits >= Length of Hash Bits.  */
    3127template<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);
     28IDISA_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);
    3329
    3430template<>
    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));
     31IDISA_ALWAYS_INLINE uint64_t slice<1>(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth) {
     32    assert(lgth > 0 && lgth <= 64);
    3833
    39     uint64_t shift = *((uint64_t *)(buffer + (idx/8))) >> (idx & 8-1);
    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 & 8-1);
     35    uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)*8 - lgth));
    4236#ifndef NDEBUG
    4337    print_register<uint64_t>("shift", shift);
     
    4842
    4943template<>
    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));
     44IDISA_ALWAYS_INLINE uint64_t slice<8>(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth) {
     45    assert(lgth > 0 && lgth <= 8);
    5346
    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);
    5749#ifndef NDEBUG
    5850    print_register<uint64_t>("shift", shift);
     
    6456/* WARNING: Length of Symbol Bits >= Length of Hash Bits.  */
    6557template<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) {
     58IDISA_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) {
    6759
    68     assert(lgth > 0);
    69     assert((idx+(lgth*FW)) < ((sizeof(uint64_t)*8)+1));
     60    assert(lgth > 0 && lgth*FW <= 64);
    7061    assert((lgth*FW) >= hash_bits);
    7162
    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);
    7465    uint64_t mask = (1 << hash_bits) - 1;
    7566
    7667#ifndef NDEBUG
    77     cout << "Index Lgth:  " << idx << endl;
     68    cout << "Position:  " << pos << endl;
    7869    cout << "Symbol Lgth: " << lgth << endl;
    7970    cout << "Hash Bits:   " << hash_bits << endl;
     71
    8072    uint64_t masked_x0 = x0 & mask;
    8173    uint64_t shift_masked_x1 = (x1 >> ((lgth*FW) - hash_bits)) & mask;
     74
    8275    print_register<uint64_t>("b0", *(uint64_t *)b0);
    8376    print_register<uint64_t>("b1", *(uint64_t *)b1);
     
    9285}
    9386
     87xor_hash();
     88xor
    9489
    9590#endif // BYTE_UTIL_HPP
Note: See TracChangeset for help on using the changeset viewer.