VTK-m  2.2
Hash.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_Hash_h
11 #define vtk_m_Hash_h
12 
13 #include <vtkm/TypeTraits.h>
14 #include <vtkm/Types.h>
15 #include <vtkm/VecTraits.h>
16 
17 namespace vtkm
18 {
19 
21 
22 namespace detail
23 {
24 
25 static constexpr vtkm::HashType FNV1A_OFFSET = 2166136261;
26 static constexpr vtkm::HashType FNV1A_PRIME = 16777619;
27 
30 template <typename InVecType>
31 VTKM_EXEC_CONT inline vtkm::HashType HashFNV1a32(const InVecType& inVec)
32 {
33  using Traits = vtkm::VecTraits<InVecType>;
34  const vtkm::IdComponent numComponents = Traits::GetNumberOfComponents(inVec);
35 
36  vtkm::HashType hash = FNV1A_OFFSET;
37  for (vtkm::IdComponent index = 0; index < numComponents; index++)
38  {
39  vtkm::HashType dataBits = static_cast<vtkm::HashType>(Traits::GetComponent(inVec, index));
40  hash = (hash * FNV1A_PRIME) ^ dataBits;
41  }
42 
43  return hash;
44 }
45 
48 template <typename InVecType>
49 VTKM_EXEC_CONT inline vtkm::HashType HashFNV1a64(const InVecType& inVec)
50 {
51  using Traits = vtkm::VecTraits<InVecType>;
52  const vtkm::IdComponent numComponents = Traits::GetNumberOfComponents(inVec);
53 
54  vtkm::HashType hash = FNV1A_OFFSET;
55  for (vtkm::IdComponent index = 0; index < numComponents; index++)
56  {
57  vtkm::UInt64 allDataBits = static_cast<vtkm::UInt64>(Traits::GetComponent(inVec, index));
58  vtkm::HashType upperDataBits =
59  static_cast<vtkm::HashType>((allDataBits & 0xFFFFFFFF00000000L) >> 32);
60  hash = (hash * FNV1A_PRIME) ^ upperDataBits;
61  vtkm::HashType lowerDataBits = static_cast<vtkm::HashType>(allDataBits & 0x00000000FFFFFFFFL);
62  hash = (hash * FNV1A_PRIME) ^ lowerDataBits;
63  }
64 
65  return hash;
66 }
67 
68 // If you get a compile error saying that there is no implementation of the class HashChooser,
69 // then you have tried to make a hash from an invalid type (like a float).
70 template <typename NumericTag, std::size_t DataSize>
71 struct HashChooser;
72 
73 template <>
74 struct HashChooser<vtkm::TypeTraitsIntegerTag, 4>
75 {
76  template <typename InVecType>
77  VTKM_EXEC_CONT static vtkm::HashType Hash(const InVecType& inVec)
78  {
79  return vtkm::detail::HashFNV1a32(inVec);
80  }
81 };
82 
83 template <>
84 struct HashChooser<vtkm::TypeTraitsIntegerTag, 8>
85 {
86  template <typename InVecType>
87  VTKM_EXEC_CONT static vtkm::HashType Hash(const InVecType& inVec)
88  {
89  return vtkm::detail::HashFNV1a64(inVec);
90  }
91 };
92 
93 } // namespace detail
94 
105 template <typename InVecType>
106 VTKM_EXEC_CONT inline vtkm::HashType Hash(const InVecType& inVec)
107 {
109  using ComponentType = typename VecTraits::ComponentType;
110  using ComponentTraits = vtkm::TypeTraits<ComponentType>;
111  using Chooser = detail::HashChooser<typename ComponentTraits::NumericTag, sizeof(ComponentType)>;
112  return Chooser::Hash(inVec);
113 }
114 
115 } // namespace vtkm
116 
117 #endif //vtk_m_Hash_h
vtkm::TypeTraitsIntegerTag
Tag used to identify types that store integer numbers.
Definition: TypeTraits.h:36
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::TypeTraits
The TypeTraits class provides helpful compile-time information about the basic types used in VTKm (an...
Definition: TypeTraits.h:61
Types.h
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::VecTraits::ComponentType
T ComponentType
Type of the components in the vector.
Definition: VecTraits.h:71
vtkm::HashType
vtkm::UInt32 HashType
Definition: Hash.h:20
TypeTraits.h
vtkm::UInt32
uint32_t UInt32
Base type to use for 32-bit unsigned integer numbers.
Definition: Types.h:185
vtkm::UInt64
unsigned long long UInt64
Base type to use for 64-bit signed integer numbers.
Definition: Types.h:207
vtkm::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:61
vtkm::Hash
vtkm::HashType Hash(const InVecType &inVec)
Returns a 32-bit hash on a group of integer-type values.
Definition: Hash.h:106
VecTraits.h