MCKL
Monte Carlo Kernel Library
philox_generic.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/internal/philox_generic.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_RANDOM_INTERNAL_PHILOX_GENERIC_HPP
33 #define MCKL_RANDOM_INTERNAL_PHILOX_GENERIC_HPP
34 
41 
42 namespace mckl {
43 
44 namespace internal {
45 
46 template <typename T, std::size_t K, std::size_t Rounds, typename Constants,
47  int = std::numeric_limits<T>::digits>
49 {
50  public:
51  static void eval(
52  const void *plain, void *cipher, const std::array<T, K / 2> &key)
53  {
54  alignas(MCKL_ALIGNMENT) union {
55  std::array<T, K> s;
56  std::array<char, sizeof(T) * K> r;
57  } buf;
58 
59  std::memcpy(buf.s.data(), plain, sizeof(T) * K);
60  union_le<char>(buf.s);
62  union_le<T>(buf.r);
63  std::memcpy(cipher, buf.s.data(), sizeof(T) * K);
64  }
65 
66  template <typename ResultType>
67  static void eval(
68  Counter<T, K> &ctr, ResultType *r, const std::array<T, K / 2> &key)
69  {
70  alignas(MCKL_ALIGNMENT) union {
71  std::array<T, K> s;
72  Counter<T, K> c;
73  std::array<ResultType, sizeof(T) * K / sizeof(ResultType)> r;
74  } buf;
75 
77  buf.c = ctr;
78 #if MCKL_REQUIRE_ENDIANNESS_NEUTURAL
79  union_le<typename Counter<T, K>::value_type>(buf.s);
80 #endif
82 #if MCKL_REQUIRE_ENDIANNESS_NEUTURAL
83  union_le<T>(buf.r);
84 #endif
85  std::memcpy(r, buf.r.data(), sizeof(T) * K);
86  }
87 
88  template <typename ResultType>
89  static void eval(Counter<T, K> &ctr, std::size_t n, ResultType *r,
90  const std::array<T, K / 2> &key)
91  {
92  constexpr std::size_t R = sizeof(T) * K / sizeof(ResultType);
93 
94  for (std::size_t i = 0; i != n; ++i, r += R) {
95  eval(ctr, r, key);
96  }
97  }
98 
99  private:
100  template <std::size_t>
101  static void round(
102  std::array<T, K> &, const std::array<T, K / 2> &, std::false_type)
103  {
104  }
105 
106  template <std::size_t N>
107  static void round(
108  std::array<T, K> &s, const std::array<T, K / 2> &k, std::true_type)
109  {
111  }
112 
113  template <std::size_t N>
114  MCKL_INLINE static void rbox(
115  std::array<T, K> &s, const std::array<T, K / 2> &k)
116  {
117  rbox<N>(s, k, std::integral_constant<bool, (N > 0 && N <= Rounds)>());
118  }
119 
120  template <std::size_t>
121  static void rbox(
122  std::array<T, K> &, const std::array<T, K / 2> &, std::false_type)
123  {
124  }
125 
126  template <std::size_t N>
127  static void rbox(
128  std::array<T, K> &s, const std::array<T, K / 2> &k, std::true_type)
129  {
131  }
132 }; // class PhiloxGeneratorGenericImpl
133 
134 template <typename T, typename Constants, int D>
135 class PhiloxGeneratorGenericImpl<T, 2, 10, Constants, D>
136  : public Philox2xGeneratorGenericImpl<T, Constants>
137 {
138 }; // class PhiloxGeneratorGenericImpl
139 
140 template <typename T, typename Constants>
141 class PhiloxGeneratorGenericImpl<T, 2, 10, Constants, 64>
142  : public Philox2x64GeneratorGenericImpl<T, Constants>
143 {
144 }; // class PhiloxGeneratorGenericImpl
145 
146 template <typename T, typename Constants, int D>
147 class PhiloxGeneratorGenericImpl<T, 4, 10, Constants, D>
148  : public Philox4xGeneratorGenericImpl<T, Constants>
149 {
150 }; // class PhiloxGeneratorGenericImpl
151 
152 template <typename T, typename Constants>
153 class PhiloxGeneratorGenericImpl<T, 4, 10, Constants, 64>
154  : public Philox4x64GeneratorGenericImpl<T, Constants>
155 {
156 }; // class PhiloxGeneratorGenericImpl
157 
158 } // namespace internal
159 
160 } // namespace mckl
161 
162 #endif // MCKL_RANDOM_INTERNAL_PHILOX_GENERIC_HPP
static void eval(Counter< T, K > &ctr, ResultType *r, const std::array< T, K/2 > &key)
static void eval(const void *plain, void *cipher, const std::array< T, K/2 > &key)
#define MCKL_INLINE_CALL
Definition: intel.h:142
static void eval(std::array< T, K > &s, const std::array< T, K/2 > &k)
typename internal::CounterImpl< T, K >::type Counter
A counter type with the same width as std::array<T, K> but with possibly fewer elements.
Definition: increment.hpp:104
#define MCKL_RANDOM_INTERNAL_PHILOX_UNROLL_ROUND(N, s, rk)
static void eval(Counter< T, K > &ctr, std::size_t n, ResultType *r, const std::array< T, K/2 > &key)
void increment(std::array< T, K > &ctr, std::integral_constant< T, NSkip >)
Increment a counter by given steps.
Definition: mcmc.hpp:40
#define MCKL_ALIGNMENT
The default alignment for scalar type.
Definition: config.h:187
#define MCKL_INLINE
Definition: clang.h:147