source: trunk/lib/hash.hpp @ 1940

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

Removed debug statement.

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