source: trunk/lib/hash.hpp @ 1933

Last change on this file since 1933 was 1919, checked in by ksherdy, 8 years ago

Simplified hash functions.

File size: 4.8 KB
RevLine 
[1867]1#ifndef BYTE_UTIL_HPP
2#define BYTE_UTIL_HPP
3
4#include "bitblock.hpp"
5#include <cassert>
[1877]6#include <stdint.h>
[1867]7#include <iostream>
8using namespace std;
9
[1918]10/* WARNING:     Pad 8 bytes.
11                Internal static casts of uint8_t * b0 to uint64_t * and deference. */
[1914]12
[1919]13static uint32_t bytes2bits(uint32_t bit) { return bit * 8; }
[1868]14
[1918]15static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size);
[1919]16//static IDISA_ALWAYS_INLINE uint64_t byte_mask_hash(const uint8_t *b0, const uint32_t pos, const uint32_t hash_size);
[1867]17
[1918]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) */
[1919]21//static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * buffer, const uint32_t pos, const uint32_t slice_size);
[1914]22
[1919]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
[1918]26static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size) {
27    assert(hash_size > 0);
[1869]28
[1918]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}
[1869]36
[1919]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//}
[1877]41
[1918]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
[1912]46    uint64_t shift = *((uint64_t *)(buffer + (pos/8))) >> (pos & 8-1);
[1918]47    uint64_t mask = gen_mask(slice_size);
48    uint64_t r = shift & mask;
49
[1877]50#ifndef NDEBUG
51    print_register<uint64_t>("shift", shift);
[1918]52    print_register<uint64_t>("mask", mask);
[1877]53#endif
[1918]54
55    return r;
[1867]56}
57
[1918]58/* pos (bytes) */
[1919]59/*
[1918]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);
[1877]62
[1912]63    uint64_t shift = *((uint64_t *)(buffer+pos));
[1918]64    uint64_t mask = gen_mask(slice_size);
65    uint64_t r = shift & mask;
66
[1877]67#ifndef NDEBUG
68    print_register<uint64_t>("shift", shift);
[1918]69    print_register<uint64_t>("mask", mask);
[1877]70#endif
[1918]71    return r;
[1877]72}
[1919]73*/
[1877]74
[1919]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
[1877]78
[1919]79    assert(hash_size > 0 && hash_size <= 64);
80    assert(slice_size >= hash_size);
[1914]81
[1877]82#ifndef NDEBUG
[1918]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
[1914]88
[1919]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
[1918]94    uint64_t mask = gen_mask(hash_size);
[1919]95    uint64_t r = (x0 ^ x1) & mask;
[1914]96
[1918]97#ifndef NDEBUG
98    print_register<uint64_t>("b0", *(uint64_t *)(b0));
99    print_register<uint64_t>("x0", x0);
[1919]100    //print_register<uint64_t>("x0 >> shift", x0>>shift);
101    print_register<uint64_t>("x1", x1);
[1918]102    print_register<uint64_t>("r", r);
103#endif
104
105    return r;
106}
107
[1867]108#endif // BYTE_UTIL_HPP
109
[1918]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.