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
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.
[1936]11                Internal static casts of uint8_t * base to uint64_t * and deference. */
[1914]12
[1936]13static int32_t bytes2bits(int32_t bits) { return bits * 8; }
[1868]14
[1918]15static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t hash_size);
[1867]16
[1936]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);
[1914]19
[1936]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);
[1919]22
[1936]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
[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
[1936]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) {
[1918]39    assert(slice_size > 0 && slice_size <= 64);
[1939]40    assert((offset & (8-1)) >= 0); // negative shifts undefined in C standard
[1918]41
[1936]42    uint64_t shift = *((uint64_t *)(base + (offset/8))) >> (offset & (8-1));
[1918]43    uint64_t mask = gen_mask(slice_size);
44    uint64_t r = shift & mask;
45
[1877]46#ifndef NDEBUG
47    print_register<uint64_t>("shift", shift);
[1918]48    print_register<uint64_t>("mask", mask);
[1877]49#endif
[1918]50
51    return r;
[1867]52}
53
[1936]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) {
[1919]55    //assert(slice_size > 0 && slice_size <= 64);
56    //assert(slice_size >= hash_size); // negative shifts undefined in C standard
[1877]57
[1919]58    assert(hash_size > 0 && hash_size <= 64);
59    assert(slice_size >= hash_size);
[1914]60
[1877]61#ifndef NDEBUG
[1918]62    cout << endl;
[1936]63    cout << "Stream offset (byte):  " << offset << endl;
[1918]64    cout << "Slice size (bits): "  << slice_size << endl;
65    cout << "Hash size (bits):   " << hash_size << endl;
66#endif
[1914]67
[1936]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);
[1919]70
[1936]71    //uint64_t x0 = //*((uint64_t *)(base+offset));
[1919]72    //uint64_t shift = (slice_size - hash_size);  // negative shifts undefined in C standard
[1918]73    uint64_t mask = gen_mask(hash_size);
[1919]74    uint64_t r = (x0 ^ x1) & mask;
[1914]75
[1918]76#ifndef NDEBUG
[1936]77    print_register<uint64_t>("base", *(uint64_t *)(base));
[1918]78    print_register<uint64_t>("x0", x0);
[1919]79    //print_register<uint64_t>("x0 >> shift", x0>>shift);
80    print_register<uint64_t>("x1", x1);
[1918]81    print_register<uint64_t>("r", r);
82#endif
83
84    return r;
85}
86
[1867]87#endif // BYTE_UTIL_HPP
88
[1936]89/* offset (bytes) */
[1918]90/*
[1936]91static IDISA_ALWAYS_INLINE uint64_t byte_offset_slice(const uint8_t * base, const int32_t offset, const uint32_t slice_size) {
[1918]92    assert(slice_size > 0 && slice_size <= 64);
[1936]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);
[1918]110    //assert(slice_size <= hash_size);
111
[1936]112    uint64_t x0 = bit_offset_slice(base,offset,slice_size);
113    uint64_t x1 = bit_offset_slice(base1,offset,slice_size);
[1918]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
[1936]128    cout << "Stream offset (bit):  " << offset << endl;
[1918]129    cout << "Symbol lgth (bits): " << slice_size << endl;
130    cout << "Hash size (bits):   " << hash_size << endl;
131    cout << "Shift (bits): " << shift << endl;
132
[1936]133    print_register<uint64_t>("base", *(uint64_t *)base);
134    print_register<uint64_t>("base1", *(uint64_t *)base1);
[1918]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.