HavoqGT
hash.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Lawrence Livermore National Security, LLC.
3  * Produced at the Lawrence Livermore National Laboratory.
4  * Written by Roger Pearce <rpearce@llnl.gov>.
5  * LLNL-CODE-644630.
6  * All rights reserved.
7  *
8  * This file is part of HavoqGT, Version 0.1.
9  * For details, see https://computation.llnl.gov/casc/dcca-pub/dcca/Downloads.html
10  *
11  * Please also read this link – Our Notice and GNU Lesser General Public License.
12  * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
13  *
14  * This program is free software; you can redistribute it and/or modify it under
15  * the terms of the GNU Lesser General Public License (as published by the Free
16  * Software Foundation) version 2.1 dated February 1999.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A
20  * PARTICULAR PURPOSE. See the terms and conditions of the GNU General Public
21  * License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * OUR NOTICE AND TERMS AND CONDITIONS OF THE GNU GENERAL PUBLIC LICENSE
28  *
29  * Our Preamble Notice
30  *
31  * A. This notice is required to be provided under our contract with the
32  * U.S. Department of Energy (DOE). This work was produced at the Lawrence
33  * Livermore National Laboratory under Contract No. DE-AC52-07NA27344 with the DOE.
34  *
35  * B. Neither the United States Government nor Lawrence Livermore National
36  * Security, LLC nor any of their employees, makes any warranty, express or
37  * implied, or assumes any liability or responsibility for the accuracy,
38  * completeness, or usefulness of any information, apparatus, product, or process
39  * disclosed, or represents that its use would not infringe privately-owned rights.
40  *
41  * C. Also, reference herein to any specific commercial products, process, or
42  * services by trade name, trademark, manufacturer or otherwise does not
43  * necessarily constitute or imply its endorsement, recommendation, or favoring by
44  * the United States Government or Lawrence Livermore National Security, LLC. The
45  * views and opinions of authors expressed herein do not necessarily state or
46  * reflect those of the United States Government or Lawrence Livermore National
47  * Security, LLC, and shall not be used for advertising or product endorsement
48  * purposes.
49  *
50  */
51 
52 
53 #ifndef HAVOQGT_DETAIL_HASH_HPP_INCLUDED
54 #define HAVOQGT_DETAIL_HASH_HPP_INCLUDED
55 
56 #include <stdint.h>
57 
58 namespace havoqgt { namespace detail {
64 
65 inline uint32_t hash32( uint32_t a)
66 {
67  a = (a+0x7ed55d16) + (a<<12);
68  a = (a^0xc761c23c) ^ (a>>19);
69  a = (a+0x165667b1) + (a<<5);
70  a = (a+0xd3a2646c) ^ (a<<9);
71  a = (a+0xfd7046c5) + (a<<3);
72  a = (a^0xb55a4f09) ^ (a>>16);
73  return a;
74 }
75 
76 inline uint16_t hash16( uint16_t a)
77 {
78  a = (a+0x5d16) + (a<<6);
79  a = (a^0xc23c) ^ (a>>9);
80  a = (a+0x67b1) + (a<<5);
81  a = (a+0x646c) ^ (a<<7);
82  a = (a+0x46c5) + (a<<3);
83  a = (a^0x4f09) ^ (a>>8);
84  return a;
85 }
86 
87 inline uint64_t shifted_n_hash32(uint64_t input, int n) {
88  uint64_t to_hash = input >> n;
89  uint64_t mask = 0xFFFFFFFF;
90  to_hash &= mask;
91  to_hash = hash32(to_hash);
92 
93  to_hash <<= n;
94  mask <<= n;
95  //clear bits
96  input &= ~mask;
97  input |= to_hash;
98  return input;
99 }
100 
101 inline uint64_t shifted_n_hash16(uint64_t input, int n) {
102  uint64_t to_hash = input >> n;
103  uint64_t mask = 0xFFFF;
104  to_hash &= mask;
105  to_hash = hash16(to_hash);
106 
107  to_hash <<= n;
108  mask <<= n;
109  //clear bits
110  input &= ~mask;
111  input |= to_hash;
112  return input;
113 }
114 
115 inline uint64_t hash_nbits(uint64_t input, int n) {
116  //std::cout << "hash_nbits(" << input << ", " << n << ") = ";
117  if(n==32) {
118  input = hash32(input);
119  } else if(n>32){
120  assert(n>32);
121  n -= 32;
122  for(int i=0; i<=n; ++i) {
123  input = shifted_n_hash32(input, i);
124  }
125  for(int i=n; i>=0; --i) {
126  input = shifted_n_hash32(input, i);
127  }
128  } else if(n<32) {
129  assert(n<32);
130  assert(n>16 && "Hashing less than 16bits is not supported");
131  n -= 16;
132  for(int i=0; i<=n; ++i) {
133  input = shifted_n_hash16(input, i);
134  }
135  for(int i=n; i>=0; --i) {
136  input = shifted_n_hash16(input, i);
137  }
138  }
139  //std::cout << input << std::endl;
140  return input;
141 }
142 
143 
144 }} // end namespace havoqgt::detail
145 
146 
147 
148 #endif
uint64_t shifted_n_hash16(uint64_t input, int n)
Definition: hash.hpp:101
uint64_t hash_nbits(uint64_t input, int n)
Definition: hash.hpp:115
uint16_t hash16(uint16_t a)
Definition: hash.hpp:76
uint32_t hash32(uint32_t a)
Definition: hash.hpp:65
uint64_t shifted_n_hash32(uint64_t input, int n)
Definition: hash.hpp:87