source: trunk/lib/hash.hpp @ 1923

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

Simplified hash functions.

File size: 4.8 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/* WARNING:     Pad 8 bytes.
11                Internal static casts of uint8_t * b0 to uint64_t * and deference. */
12
13static uint32_t bytes2bits(uint32_t bit) { return bit * 8; }
14
15static 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);
17
18/* pos (bits) */
19static 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);
22
23/* pos (bits) */
24static 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);
25
26static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size) {
27    assert(hash_size > 0);
28
29    const uint64_t ONE = 1;
30    uint64_t mask = (ONE << hash_size) - ONE;
31#ifndef NDEBUG
32    print_register<uint64_t>("mask", mask);
33#endif
34    return mask;
35}
36
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//}
41
42/* pos (bits) */
43static 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);
47    uint64_t mask = gen_mask(slice_size);
48    uint64_t r = shift & mask;
49
50#ifndef NDEBUG
51    print_register<uint64_t>("shift", shift);
52    print_register<uint64_t>("mask", mask);
53#endif
54
55    return r;
56}
57
58/* pos (bytes) */
59/*
60static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size) {
61    assert(slice_size > 0 && slice_size <= 64);
62
63    uint64_t shift = *((uint64_t *)(buffer+pos));
64    uint64_t mask = gen_mask(slice_size);
65    uint64_t r = shift & mask;
66
67#ifndef NDEBUG
68    print_register<uint64_t>("shift", shift);
69    print_register<uint64_t>("mask", mask);
70#endif
71    return r;
72}
73*/
74
75static 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
110/*
111static 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);
112static 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) {
113    assert(slice_size > 0 && slice_size <= 64);
114    //assert(slice_size <= hash_size);
115
116    uint64_t x0 = bit_offset_slice(b0,pos,slice_size);
117    uint64_t x1 = bit_offset_slice(b1,pos,slice_size);
118    uint64_t mask = gen_mask(hash_size);
119
120    assert(x0 != x1);
121
122    uint64_t t = x0 ^ x1;
123    uint64_t r = t;
124    int32_t shift = slice_size;
125
126    print_register<uint64_t>("t", t);
127    print_register<uint64_t>("r", r);
128
129    while(shift > 0) {
130
131#ifndef NDEBUG
132    cout << "Stream pos (bit):  " << pos << endl;
133    cout << "Symbol lgth (bits): " << slice_size << endl;
134    cout << "Hash size (bits):   " << hash_size << endl;
135    cout << "Shift (bits): " << shift << endl;
136
137    print_register<uint64_t>("b0", *(uint64_t *)b0);
138    print_register<uint64_t>("b1", *(uint64_t *)b1);
139    print_register<uint64_t>("x0", x0);
140    print_register<uint64_t>("x1", x1);
141    print_register<uint64_t>("r", r);
142#endif
143        r = r | (r << (uint32_t)shift);
144        shift -= slice_size;
145        print_register<uint64_t>("r", r);
146    }
147
148    return r & mask;
149}
150*/
Note: See TracBrowser for help on using the repository browser.