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