source: trunk/lib/hash.hpp @ 1951

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

Updated hash.hpp bit_slice method warning.

File size: 5.2 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 + idx
24// and require sizeof(uint64_t) bytes of addtional 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));
30
31static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits);
32//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes);
33static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits);
34static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * h0, const uint8_t * h1, const uint32_t slice_bits, const uint32_t hash_bits);
35
36///////////////////////////////////////////////////////////////////////////////
37
38static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits) {
39    assert(mask_bits >= 0);
40
41    const uint64_t ONE = 1;
42    uint64_t mask = (ONE << mask_bits) - ONE;
43#ifdef HASH_HPP_DEBUG
44    print_register<uint64_t>("mask", mask);
45#endif
46    return mask;
47}
48
49//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes) {
50//    assert(slice_bytes >= 0 && slice_bytes <= sizeof(uint64_t));
51//    assert(byte_idx >= 0);
52
53//    uint64_t shift = *((uint64_t *)(base + byte_idx));
54//    uint64_t mask = gen_mask(bytes2bits(slice_bytes));
55//    uint64_t r = shift & mask;
56
57//#ifdef HASH_HPP_DEBUG
58//    print_register<BitBlock>("base", *(BitBlock *)base);
59//    cout << "byte index:" << byte_idx << endl;
60//    print_register<BitBlock>("shift", *(BitBlock *)&shift);
61//    print_register<uint64_t>("mask", mask);
62//    print_register<uint64_t>("r", r);
63//#endif
64
65//    return r;
66//}
67
68static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits) {
69    assert(slice_bits >= 0 && slice_bits <= bytes2bits(sizeof(uint64_t)));
70    assert(bit_idx >= 0);
71
72    uint64_t shift = *((uint64_t *)(base + (bit_idx/8))) >> (bit_idx & (8-1));
73    uint64_t mask = gen_mask(slice_bits);
74    uint64_t r = shift & mask;
75
76#ifdef HASH_HPP_DEBUG
77    print_register<uint64_t>("base", *(uint64_t *)base);
78    cout << "           bit index = " << bit_idx << endl;
79    print_register<uint64_t>("shift", *(uint64_t *)&shift);
80    print_register<uint64_t>("mask", mask);
81    print_register<uint64_t>("r", r);
82#endif
83
84    return r;
85}
86
87static IDISA_ALWAYS_INLINE uint64_t 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) {
88
89    assert(hash_size > 0 && hash_size <= 64);
90    assert(slice_size >= hash_size);
91
92    uint64_t x0 = bit_slice(h0,bit_idx,hash_bits);
93    uint64_t x1 = bit_slice(h1,bit_idx+slice_bits-hash_bits,hash_bits);
94
95    assert(x0 != x1);
96    uint64_t r = x0 ^ x1;
97
98#ifdef HASH_HPP_DEBUG
99    print_register<uint64_t>("h0", *(uint64_t *)(h0));
100    print_register<uint64_t>("h1", *(uint64_t *)(h1));
101    print_register<uint64_t>("x0", x0);
102    print_register<uint64_t>("x1", x1);
103    print_register<uint64_t>("r", r);
104#endif
105
106    return r;
107}
108
109#endif // HASH_HPP
110
111/*
112static 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);
113static 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) {
114    assert(slice_size > 0 && slice_size <= 64);
115    //assert(slice_size <= hash_size);
116
117    uint64_t x0 = bit_slice(base,offset,slice_size);
118    uint64_t x1 = bit_slice(base1,offset,slice_size);
119    uint64_t mask = gen_mask(hash_size);
120
121    assert(x0 != x1);
122
123    uint64_t t = x0 ^ x1;
124    uint64_t r = t;
125    int32_t shift = slice_size;
126
127    print_register<uint64_t>("t", t);
128    print_register<uint64_t>("r", r);
129
130    while(shift > 0) {
131
132#ifndef NDEBUG
133    cout << "Stream offset (bit):  " << offset << endl;
134    cout << "Symbol lgth (bits): " << slice_size << endl;
135    cout << "Hash size (bits):   " << hash_size << endl;
136    cout << "Shift (bits): " << shift << endl;
137
138    print_register<uint64_t>("base", *(uint64_t *)base);
139    print_register<uint64_t>("base1", *(uint64_t *)base1);
140    print_register<uint64_t>("x0", x0);
141    print_register<uint64_t>("x1", x1);
142    print_register<uint64_t>("r", r);
143#endif
144        r = r | (r << (uint32_t)shift);
145        shift -= slice_size;
146        print_register<uint64_t>("r", r);
147    }
148
149    return r & mask;
150}
151*/
Note: See TracBrowser for help on using the repository browser.