source: trunk/lib/hash_util.hpp @ 1912

Last change on this file since 1912 was 1912, checked in by ksherdy, 7 years ago

Updated debug statements.

File size: 3.1 KB
Line 
1#ifndef BYTE_UTIL_HPP
2#define BYTE_UTIL_HPP
3
4#include "bitblock.hpp"
5#include <cassert>
6#include <stdint.h>
7#include <iostream>
8using namespace std;
9
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.
14
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.
18
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
21*/
22
23/* WARNING: Casts uint8_t * buffer at pos to uint64_t * and deferences. Segmentation faults w/o sufficient buffer padding.  */
24template<uint32_t FW>
25IDISA_ALWAYS_INLINE uint64_t slice(const uint8_t * buffer, const uint32_t pos, const uint32_t lgth);
26
27template<uint32_t FW>
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);
29
30template<>
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);
33
34    uint64_t shift = *((uint64_t *)(buffer + (pos/8))) >> (pos & 8-1);
35    uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t)*8 - lgth));
36#ifndef NDEBUG
37    print_register<uint64_t>("shift", shift);
38    print_register<uint64_t>("slice mask", slice_mask);
39#endif
40    return shift & slice_mask;
41}
42
43template<>
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);
46
47    uint64_t shift = *((uint64_t *)(buffer+pos));
48    uint64_t slice_mask = UINT64_MAX >> ((sizeof(uint64_t) - lgth)*8);
49#ifndef NDEBUG
50    print_register<uint64_t>("shift", shift);
51    print_register<uint64_t>("slice mask", slice_mask);
52#endif
53    return shift & slice_mask;
54}
55
56/* WARNING: Length of Symbol Bits >= Length of Hash Bits.  */
57template<uint32_t FW>
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) {
59
60    assert(lgth > 0 && lgth*FW <= 64);
61    assert((lgth*FW) >= hash_bits);
62
63    uint64_t x0 = slice<FW>(b0,pos,lgth);
64    uint64_t x1 = slice<FW>(b1,pos,lgth);
65    uint64_t mask = (1 << hash_bits) - 1;
66
67#ifndef NDEBUG
68    cout << "Position:  " << pos << endl;
69    cout << "Symbol Lgth: " << lgth << endl;
70    cout << "Hash Bits:   " << hash_bits << endl;
71
72    uint64_t masked_x0 = x0 & mask;
73    uint64_t shift_masked_x1 = (x1 >> ((lgth*FW) - hash_bits)) & mask;
74
75    print_register<uint64_t>("b0", *(uint64_t *)b0);
76    print_register<uint64_t>("b1", *(uint64_t *)b1);
77    print_register<uint64_t>("x0", x0);
78    print_register<uint64_t>("x1", x1);
79    print_register<uint64_t>("mask", mask);
80    print_register<uint64_t>("masked x0", masked_x0);
81    print_register<uint64_t>("shift masked x1", shift_masked_x1);
82#endif
83
84    return (x0 ^ (x1 >> ((lgth*FW) - hash_bits))) & mask;
85}
86
87xor_hash();
88xor
89
90#endif // BYTE_UTIL_HPP
91
Note: See TracBrowser for help on using the repository browser.