VTK-m  2.0
ArrayHandleRandomUniformReal.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_count_ArrayHandleRandomUniformReal_h
11 #define vtk_m_count_ArrayHandleRandomUniformReal_h
12 
15 
16 namespace vtkm
17 {
18 namespace cont
19 {
20 
21 namespace detail
22 {
23 template <typename Real>
24 struct CanonicalFunctor;
25 
26 template <>
27 struct CanonicalFunctor<vtkm::Float64>
28 {
31  // We take 53 bits (number of bits in mantissa in a double) from the 64 bits random source
32  // and divide it by (1 << 53).
33  static constexpr vtkm::Float64 DIVISOR = static_cast<vtkm::Float64>(vtkm::UInt64{ 1 } << 53);
34  static constexpr vtkm::UInt64 MASK = (vtkm::UInt64{ 1 } << 53) - vtkm::UInt64{ 1 };
35 
37  vtkm::Float64 operator()(vtkm::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
38 };
39 
40 template <>
41 struct CanonicalFunctor<vtkm::Float32>
42 {
43  // We take 24 bits (number of bits in mantissa in a double) from the 64 bits random source
44  // and divide it by (1 << 24).
45  static constexpr vtkm::Float32 DIVISOR = static_cast<vtkm::Float32>(vtkm::UInt32{ 1 } << 24);
46  static constexpr vtkm::UInt32 MASK = (vtkm::UInt32{ 1 } << 24) - vtkm::UInt32{ 1 };
47 
49  vtkm::Float32 operator()(vtkm::UInt64 bits) const { return (bits & MASK) / DIVISOR; }
50 };
51 } // detail
52 
53 template <typename Real = vtkm::Float64>
55  : public vtkm::cont::ArrayHandleTransform<vtkm::cont::ArrayHandleRandomUniformBits,
56  detail::CanonicalFunctor<Real>>
57 {
58 public:
60 
65  detail::CanonicalFunctor<Real>>));
66 
67  explicit ArrayHandleRandomUniformReal(vtkm::Id length, SeedType seed = { std::random_device{}() })
68  : Superclass(vtkm::cont::ArrayHandleRandomUniformBits{ length, seed },
69  detail::CanonicalFunctor<Real>{})
70  {
71  }
72 };
73 
74 } // cont
75 } // vtkm
76 #endif //vtk_m_count_ArrayHandleRandomUniformReal_h
ArrayHandleRandomUniformBits.h
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:226
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::cont::ArrayHandleRandomUniformReal::ArrayHandleRandomUniformReal
ArrayHandleRandomUniformReal(vtkm::Id length, SeedType seed={ std::random_device{}() })
Definition: ArrayHandleRandomUniformReal.h:67
ArrayHandleTransform.h
vtkm::cont::ArrayHandleRandomUniformReal
Definition: ArrayHandleRandomUniformReal.h:54
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::ArrayHandleTransform
Implicitly transform values of one array to another with a functor.
Definition: ArrayHandleTransform.h:437
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Float64
double Float64
Definition: Types.h:155
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::cont::ArrayHandleRandomUniformBits
An ArrayHandle that provides a source of random bits.
Definition: ArrayHandleRandomUniformBits.h:71