source: trunk/lib/hash.hpp @ 2039

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

Removed assertion that x0 != x1.

File size: 5.4 KB
Line 
1#ifndef HASH_HPP
2#define HASH_HPP
3
4/*=============================================================================
5  allocator.hpp - Hash Utilities.
6  Created on: 18-December-2011
7  Author: Ken Herdy
8=============================================================================*/
9
10// #define HASH_HPP_DEBUG
11
12#include "../lib/bitblock.hpp"
13#include <cassert>
14#include <stdint.h>
15#include <iostream>
16using namespace std;
17
18///////////////////////////////////////////////////////////////////////////////
19//
20// WARNING: Pad or Perish
21//
22// 'bit_slice' and 'byte_slice' slice forward via a static cast to the
23// uint64_t type at the position of the base address + bit_idx
24// and require up to sizeof(uint64_t) bytes of additional padding.
25//
26///////////////////////////////////////////////////////////////////////////////
27
28static int32_t bytes2bits(int32_t bytes) { return bytes * 8; }
29static int32_t bits2bytes(int32_t bits) /*{ return (bits + 8) / (8); } */ { return ((bits & (8-1) != 0) ? (bits + 8) / (8) : (bits/8)); }
30static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits);
31
32//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes);
33//static IDISA_ALWAYS_INLINE uint64_t byte_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t byte_idx, const uint32_t slice_bytes, const uint32_t hash_bytes);
34
35static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits);
36static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t bit_idx, const uint32_t slice_bits, const uint32_t hash_bits);
37
38///////////////////////////////////////////////////////////////////////////////
39
40static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits) {
41    assert(mask_bits >= 0);
42
43    const uint64_t ONE = 1;
44    uint64_t mask = (ONE << mask_bits) - ONE;
45#ifdef HASH_HPP_DEBUG
46    print_register<uint64_t>("mask", mask);
47#endif
48    return mask;
49}
50
51//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes) {
52//    assert(slice_bytes >= 0 && slice_bytes <= sizeof(uint64_t));
53//    assert(byte_idx >= 0);
54
55//    uint64_t shift = *((uint64_t *)(base + byte_idx));
56//    uint64_t mask = gen_mask(bytes2bits(slice_bytes));
57//    uint64_t r = shift & mask;
58
59//#ifdef HASH_HPP_DEBUG
60//    print_register<BitBlock>("base", *(BitBlock *)base);
61//    cout << "byte index:" << byte_idx << endl;
62//    print_register<BitBlock>("shift", *(BitBlock *)&shift);
63//    print_register<uint64_t>("mask", mask);
64//    print_register<uint64_t>("r", r);
65//#endif
66
67//    return r;
68//}
69
70static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits) {
71    assert(slice_bits >= 0 && slice_bits <= bytes2bits(sizeof(uint64_t)));
72    assert(bit_idx >= 0);
73
74    uint64_t shift = *((uint64_t *)(base + (bit_idx/8))) >> (bit_idx & (8-1));
75    uint64_t mask = gen_mask(slice_bits);
76    uint64_t r = shift & mask;
77
78#ifdef HASH_HPP_DEBUG
79    print_register<uint64_t>("base", *(uint64_t *)base);
80    cout << "           bit index = " << bit_idx << endl;
81    print_register<uint64_t>("shift", *(uint64_t *)&shift);
82    print_register<uint64_t>("mask", mask);
83    print_register<uint64_t>("r", r);
84#endif
85
86    return r;
87}
88
89static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t bit_idx, const uint32_t slice_bits, const uint32_t hash_bits) {
90
91    assert(hash_bits > 0 && hash_bits <= 64);
92    assert(slice_bits >= hash_bits);
93
94    uint64_t x0 = bit_slice(h0,bit_idx,hash_bits);
95    uint64_t x1 = bit_slice(h1,bit_idx+slice_bits-hash_bits,hash_bits);
96
97    //assert(x0 != x1);
98    uint64_t mask = gen_mask(slice_bits);
99    uint64_t r = x0 ^ x1;
100
101#ifdef HASH_HPP_DEBUG
102    print_register<uint64_t>("h0", *(uint64_t *)(h0));
103    print_register<uint64_t>("h1", *(uint64_t *)(h1));
104    print_register<uint64_t>("x0", x0);
105    print_register<uint64_t>("x1", x1);
106    print_register<uint64_t>("r", r);
107#endif
108
109    return r  & mask;
110}
111
112#endif // HASH_HPP
113
114/*
115static 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);
116static 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) {
117    assert(slice_size > 0 && slice_size <= 64);
118    //assert(slice_size <= hash_size);
119
120    uint64_t x0 = bit_slice(base,offset,slice_size);
121    uint64_t x1 = bit_slice(base1,offset,slice_size);
122    uint64_t mask = gen_mask(hash_size);
123
124    assert(x0 != x1);
125
126    uint64_t t = x0 ^ x1;
127    uint64_t r = t;
128    int32_t shift = slice_size;
129
130    print_register<uint64_t>("t", t);
131    print_register<uint64_t>("r", r);
132
133    while(shift > 0) {
134
135#ifndef NDEBUG
136    cout << "Stream offset (bit):  " << offset << endl;
137    cout << "Symbol lgth (bits): " << slice_size << endl;
138    cout << "Hash size (bits):   " << hash_size << endl;
139    cout << "Shift (bits): " << shift << endl;
140
141    print_register<uint64_t>("base", *(uint64_t *)base);
142    print_register<uint64_t>("base1", *(uint64_t *)base1);
143    print_register<uint64_t>("x0", x0);
144    print_register<uint64_t>("x1", x1);
145    print_register<uint64_t>("r", r);
146#endif
147        r = r | (r << (uint32_t)shift);
148        shift -= slice_size;
149        print_register<uint64_t>("r", r);
150    }
151
152    return r & mask;
153}
154*/
Note: See TracBrowser for help on using the repository browser.