source: trunk/lib/hash.hpp @ 1939

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

Updated assertion.

File size: 4.7 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 * base to uint64_t * and deference. */
12
13static int32_t bytes2bits(int32_t bits) { return bits * 8; }
14
15static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size);
16
17/* offset (bits) */
18static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size);
19
20/* offset (bytes) */
21//static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size);
22
23/* offset (bits) */
24static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * base, const int32_t offset, 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/* offset (bits) */
38static IDISA_ALWAYS_INLINE uint64_t bit_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size) {
39    assert(slice_size > 0 && slice_size <= 64);
40    cout << (offset & (8-1) >= 0) << endl;
41    assert((offset & (8-1)) >= 0); // negative shifts undefined in C standard
42
43    uint64_t shift = *((uint64_t *)(base + (offset/8))) >> (offset & (8-1));
44    uint64_t mask = gen_mask(slice_size);
45    uint64_t r = shift & mask;
46
47#ifndef NDEBUG
48    print_register<uint64_t>("shift", shift);
49    print_register<uint64_t>("mask", mask);
50#endif
51
52    return r;
53}
54
55static IDISA_ALWAYS_INLINE uint64_t compress_hash(const uint8_t * base, const int32_t offset, const uint32_t slice_size, const uint32_t hash_size) {
56    //assert(slice_size > 0 && slice_size <= 64);
57    //assert(slice_size >= hash_size); // negative shifts undefined in C standard
58
59    assert(hash_size > 0 && hash_size <= 64);
60    assert(slice_size >= hash_size);
61
62#ifndef NDEBUG
63    cout << endl;
64    cout << "Stream offset (byte):  " << offset << endl;
65    cout << "Slice size (bits): "  << slice_size << endl;
66    cout << "Hash size (bits):   " << hash_size << endl;
67#endif
68
69    uint64_t x0 = bit_offset_slice(base,offset,hash_size);
70    uint64_t x1 = bit_offset_slice(base,offset+slice_size-hash_size,hash_size);
71
72    //uint64_t x0 = //*((uint64_t *)(base+offset));
73    //uint64_t shift = (slice_size - hash_size);  // negative shifts undefined in C standard
74    uint64_t mask = gen_mask(hash_size);
75    uint64_t r = (x0 ^ x1) & mask;
76
77#ifndef NDEBUG
78    print_register<uint64_t>("base", *(uint64_t *)(base));
79    print_register<uint64_t>("x0", x0);
80    //print_register<uint64_t>("x0 >> shift", x0>>shift);
81    print_register<uint64_t>("x1", x1);
82    print_register<uint64_t>("r", r);
83#endif
84
85    return r;
86}
87
88#endif // BYTE_UTIL_HPP
89
90/* offset (bytes) */
91/*
92static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size) {
93    assert(slice_size > 0 && slice_size <= 64);
94
95    uint64_t shift = *((uint64_t *)(base+offset));
96    uint64_t mask = gen_mask(slice_size);
97    uint64_t r = shift & mask;
98
99#ifndef NDEBUG
100    print_register<uint64_t>("shift", shift);
101    print_register<uint64_t>("mask", mask);
102#endif
103    return r;
104}
105*/
106
107/*
108static 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);
109static 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) {
110    assert(slice_size > 0 && slice_size <= 64);
111    //assert(slice_size <= hash_size);
112
113    uint64_t x0 = bit_offset_slice(base,offset,slice_size);
114    uint64_t x1 = bit_offset_slice(base1,offset,slice_size);
115    uint64_t mask = gen_mask(hash_size);
116
117    assert(x0 != x1);
118
119    uint64_t t = x0 ^ x1;
120    uint64_t r = t;
121    int32_t shift = slice_size;
122
123    print_register<uint64_t>("t", t);
124    print_register<uint64_t>("r", r);
125
126    while(shift > 0) {
127
128#ifndef NDEBUG
129    cout << "Stream offset (bit):  " << offset << endl;
130    cout << "Symbol lgth (bits): " << slice_size << endl;
131    cout << "Hash size (bits):   " << hash_size << endl;
132    cout << "Shift (bits): " << shift << endl;
133
134    print_register<uint64_t>("base", *(uint64_t *)base);
135    print_register<uint64_t>("base1", *(uint64_t *)base1);
136    print_register<uint64_t>("x0", x0);
137    print_register<uint64_t>("x1", x1);
138    print_register<uint64_t>("r", r);
139#endif
140        r = r | (r << (uint32_t)shift);
141        shift -= slice_size;
142        print_register<uint64_t>("r", r);
143    }
144
145    return r & mask;
146}
147*/
Note: See TracBrowser for help on using the repository browser.