1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_GRAPPLER_GRAPH_ANALYZER_HASH_TOOLS_H_
17 #define TENSORFLOW_CORE_GRAPPLER_GRAPH_ANALYZER_HASH_TOOLS_H_
18 
19 #include <cstddef>
20 
21 namespace tensorflow {
22 namespace grappler {
23 namespace graph_analyzer {
24 
25 // Unfortunately, std::hash provides no way to combine hashes, so everyone
26 // is copying boost::hash_combine. This is a version that follows Google's
27 // guidelines on the arguments, and contains only the combination, without
28 // hashing.
CombineHash(size_t from,size_t * to)29 inline void CombineHash(size_t from, size_t* to) {
30   *to ^= from + 0x9e3779b9 + (*to << 6) + (*to >> 2);
31 }
32 
33 // Combine two hashes in such a way that the order of combination doesn't matter
34 // (so it's really both commutative and associative). The result is not a very
35 // high-quality hash but can be used in case if the order of sub-elements must
36 // not matter in the following comparison. An alternative would be to sort the
37 // hashes of the sub-elements and then combine them normally in the sorted
38 // order.
CombineHashCommutative(size_t from,size_t * to)39 inline void CombineHashCommutative(size_t from, size_t* to) {
40   *to = *to + from + 0x9e3779b9;
41 }
42 
43 }  // end namespace graph_analyzer
44 }  // end namespace grappler
45 }  // end namespace tensorflow
46 
47 #endif  // TENSORFLOW_CORE_GRAPPLER_GRAPH_ANALYZER_HASH_TOOLS_H_
48