source: trunk/lib/hash.hpp @ 2719

Last change on this file since 2719 was 2719, checked in by cameron, 6 years ago

Some copyright and description fixes

File size: 5.5 KB
Line 
1#ifndef HASH_HPP
2#define HASH_HPP
3
4/*=============================================================================
5  hash.hpp - Hash Utilities.
6  Created on: 18-December-2011
7  Author: Ken Herdy
8=============================================================================*/
9
10// #define HASH_HPP_DEBUG
11
12//#define NDEBUG // if NDEBUG then disable assertions
13
14#include "bitblock.hpp"
15#include <cassert>
16#include <stdint.h>
17#include <iostream>
18
19///////////////////////////////////////////////////////////////////////////////
20//
21// WARNING: Pad or Perish
22//
23// 'bit_slice' and 'byte_slice' slice forward via a static cast to the
24// uint64_t type at the position of the base address + bit_idx
25// and require up to sizeof(uint64_t) bytes of additional padding.
26//
27///////////////////////////////////////////////////////////////////////////////
28
29static int32_t bytes2bits(int32_t bytes) { return bytes * 8; }
30static int32_t bits2bytes(int32_t bits) /*{ return (bits + 8) / (8); } */ { return ((bits & (8-1) != 0) ? (bits + 8) / (8) : (bits/8)); }
31static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits);
32
33//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes);
34//static IDISA_ALWAYS_INLINE uint64_t byte_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t byte_idx, const uint32_t slice_bytes, const uint32_t hash_bytes);
35
36static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits);
37static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t bit_idx, const uint32_t slice_bits, const uint32_t hash_bits);
38
39///////////////////////////////////////////////////////////////////////////////
40
41static IDISA_ALWAYS_INLINE uint64_t gen_mask(const uint32_t mask_bits) {
42    assert(mask_bits >= 0);
43
44    const uint64_t ONE = 1;
45    uint64_t mask = (ONE << mask_bits) - ONE;
46#ifdef HASH_HPP_DEBUG
47    print_register<uint64_t>("mask", mask);
48#endif
49    return mask;
50}
51
52//static IDISA_ALWAYS_INLINE uint64_t byte_slice(const uint8_t * base, const int32_t byte_idx, const uint32_t slice_bytes) {
53//    assert(slice_bytes >= 0 && slice_bytes <= sizeof(uint64_t));
54//    assert(byte_idx >= 0);
55
56//    uint64_t shift = *((uint64_t *)(base + byte_idx));
57//    uint64_t mask = gen_mask(bytes2bits(slice_bytes));
58//    uint64_t r = shift & mask;
59
60//#ifdef HASH_HPP_DEBUG
61//    print_register<BitBlock>("base", *(BitBlock *)base);
62//    std::cout << "byte index:" << byte_idx << std::endl;
63//    print_register<BitBlock>("shift", *(BitBlock *)&shift);
64//    print_register<uint64_t>("mask", mask);
65//    print_register<uint64_t>("r", r);
66//#endif
67
68//    return r;
69//}
70
71static IDISA_ALWAYS_INLINE uint64_t bit_slice(const uint8_t * base, const int32_t bit_idx, const uint32_t slice_bits) {
72    assert(slice_bits >= 0 && slice_bits <= bytes2bits(sizeof(uint64_t)));
73    assert(bit_idx >= 0);
74
75    uint64_t shift = *((uint64_t *)(base + (bit_idx/8))) >> (bit_idx & (8-1));
76    uint64_t mask = gen_mask(slice_bits);
77    uint64_t r = shift & mask;
78
79#ifdef HASH_HPP_DEBUG
80    print_register<uint64_t>("base", *(uint64_t *)base);
81    std::cout << "           bit index = " << bit_idx << std::endl;
82    print_register<uint64_t>("shift", *(uint64_t *)&shift);
83    print_register<uint64_t>("mask", mask);
84    print_register<uint64_t>("r", r);
85#endif
86
87    return r;
88}
89
90static IDISA_ALWAYS_INLINE uint64_t bit_compress_hash(const uint8_t * h0, const uint8_t * h1, const int32_t bit_idx, const uint32_t slice_bits, const uint32_t hash_bits) {
91
92    assert(hash_bits > 0 && hash_bits <= 64);
93    assert(slice_bits >= hash_bits);
94
95    uint64_t x0 = bit_slice(h0,bit_idx,hash_bits);
96    uint64_t x1 = bit_slice(h1,bit_idx+slice_bits-hash_bits,hash_bits);
97
98    //assert(x0 != x1);
99    uint64_t mask = gen_mask(slice_bits);
100    uint64_t r = x0 ^ x1;
101
102#ifdef HASH_HPP_DEBUG
103    print_register<uint64_t>("h0", *(uint64_t *)(h0));
104    print_register<uint64_t>("h1", *(uint64_t *)(h1));
105    print_register<uint64_t>("x0", x0);
106    print_register<uint64_t>("x1", x1);
107    print_register<uint64_t>("r", r);
108#endif
109
110    return r  & mask;
111}
112
113#endif // HASH_HPP
114
115/*
116static 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);
117static 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) {
118    assert(slice_size > 0 && slice_size <= 64);
119    //assert(slice_size <= hash_size);
120
121    uint64_t x0 = bit_slice(base,offset,slice_size);
122    uint64_t x1 = bit_slice(base1,offset,slice_size);
123    uint64_t mask = gen_mask(hash_size);
124
125    assert(x0 != x1);
126
127    uint64_t t = x0 ^ x1;
128    uint64_t r = t;
129    int32_t shift = slice_size;
130
131    print_register<uint64_t>("t", t);
132    print_register<uint64_t>("r", r);
133
134    while(shift > 0) {
135
136#ifndef NDEBUG
137    std::cout << "Stream offset (bit):  " << offset << std::endl;
138    std::cout << "Symbol lgth (bits): " << slice_size << std::endl;
139    std::cout << "Hash size (bits):   " << hash_size << std::endl;
140    std::cout << "Shift (bits): " << shift << std::endl;
141
142    print_register<uint64_t>("base", *(uint64_t *)base);
143    print_register<uint64_t>("base1", *(uint64_t *)base1);
144    print_register<uint64_t>("x0", x0);
145    print_register<uint64_t>("x1", x1);
146    print_register<uint64_t>("r", r);
147#endif
148        r = r | (r << (uint32_t)shift);
149        shift -= slice_size;
150        print_register<uint64_t>("r", r);
151    }
152
153    return r & mask;
154}
155*/
Note: See TracBrowser for help on using the repository browser.