MCKL
Monte Carlo Kernel Library
threefry_generic.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/internal/threefry_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_THREEFRY_GENERIC_HPP
33 #define MCKL_RANDOM_INTERNAL_THREEFRY_GENERIC_HPP
34 
46 
47 namespace mckl {
48 
49 namespace internal {
50 
51 template <typename T, std::size_t K, std::size_t Rounds, typename Constants,
52  int = std::numeric_limits<T>::digits>
54 {
55  public:
56  static void eval(
57  const void *plain, void *cipher, const std::array<T, K + 4> &par)
58  {
59  alignas(MCKL_ALIGNMENT) union {
60  std::array<T, K> s;
61  std::array<char, sizeof(T) * K> r;
62  } buf;
63 
64  std::memcpy(buf.s.data(), plain, sizeof(T) * K);
65  union_le<char>(buf.s);
67  union_le<T>(buf.r);
68  std::memcpy(cipher, buf.s.data(), sizeof(T) * K);
69  }
70 
71  template <typename ResultType>
72  static void eval(
73  Counter<T, K> &ctr, ResultType *r, const std::array<T, K + 4> &par)
74  {
75  alignas(MCKL_ALIGNMENT) union {
76  std::array<T, K> s;
77  Counter<T, K> c;
78  std::array<ResultType, sizeof(T) * K / sizeof(ResultType)> r;
79  } buf;
80 
82  buf.c = ctr;
83 #if MCKL_REQUIRE_ENDIANNESS_NEUTURAL
84  union_le<typename Counter<T, K>::value_type>(buf.s);
85 #endif
87 #if MCKL_REQUIRE_ENDIANNESS_NEUTURAL
88  union_le<T>(buf.r);
89 #endif
90  std::memcpy(r, buf.r.data(), sizeof(T) * K);
91  }
92 
93  template <typename ResultType>
94  static void eval(Counter<T, K> &ctr, std::size_t n, ResultType *r,
95  const std::array<T, K + 4> &par)
96  {
97  constexpr std::size_t rstride = sizeof(T) * K / sizeof(ResultType);
98 
99  for (std::size_t i = 0; i != n; ++i, r += rstride) {
100  eval(ctr, r, par);
101  }
102  }
103 
104  private:
105  template <std::size_t>
106  static void round(
107  std::array<T, K> &, const std::array<T, K + 4> &, std::false_type)
108  {
109  }
110 
111  template <std::size_t N>
112  static void round(
113  std::array<T, K> &s, const std::array<T, K + 4> &par, std::true_type)
114  {
116  }
117 
118  template <std::size_t N>
119  MCKL_INLINE static void kbox(
120  std::array<T, K> &s, const std::array<T, K + 4> &par)
121  {
122  kbox<N>(s, par,
123  std::integral_constant<bool, (N % 4 == 0 && N <= Rounds)>());
124  }
125 
126  template <std::size_t N>
127  static void kbox(
128  std::array<T, K> &, const std::array<T, K + 4> &, std::false_type)
129  {
130  }
131 
132  template <std::size_t N>
133  static void kbox(
134  std::array<T, K> &s, const std::array<T, K + 4> &par, std::true_type)
135  {
137  }
138 
139  template <std::size_t N>
140  MCKL_INLINE static void rbox(std::array<T, K> &s)
141  {
142  rbox<N>(s, std::integral_constant<bool, (N > 0 && N <= Rounds)>());
143  }
144 
145  template <std::size_t N>
146  static void rbox(std::array<T, K> &, std::false_type)
147  {
148  }
149 
150  template <std::size_t N>
151  static void rbox(std::array<T, K> &s, std::true_type)
152  {
154  }
155 }; // class ThreefryGeneratorGenericImpl
156 
157 template <typename T>
160 {
161 }; // class ThreefryGeneratorGenericImpl
162 
163 template <typename T>
166 {
167 }; // class ThreefryGeneratorGenericImpl
168 
169 template <typename T>
172 {
173 }; // class ThreefryGeneratorGenericImpl
174 
175 template <typename T>
178 {
179 }; // class ThreefryGeneratorGenericImpl
180 
181 template <typename T>
184 {
185 }; // class ThreefryGeneratorGenericImpl
186 
187 template <typename T>
190 {
191 }; // class ThreefryGeneratorGenericImpl
192 
193 template <typename T>
196 {
197 }; // class ThreefryGeneratorGenericImpl
198 
199 template <typename T>
202 {
203 }; // class ThreefryGeneratorGenericImpl
204 
205 template <typename T>
208 {
209 }; // class ThreefryGeneratorGenericImpl
210 
211 } // namespace internal
212 
213 } // namespace mckl
214 
215 #endif // MCKL_RANDOM_INTERNAL_THREEFRY_GENERIC_HPP
static void eval(const void *plain, void *cipher, const std::array< T, K+4 > &par)
#define MCKL_INLINE_CALL
Definition: intel.h:142
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
static void eval(Counter< T, K > &ctr, std::size_t n, ResultType *r, const std::array< T, K+4 > &par)
static void eval(std::array< T, K > &s)
#define MCKL_RANDOM_INTERNAL_THREEFRY_UNROLL_ROUND(N, s, par)
Default Threefry constants.
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
static void eval(Counter< T, K > &ctr, ResultType *r, const std::array< T, K+4 > &par)
static void eval(std::array< T, K > &s, const std::array< T, K+4 > &par)
#define MCKL_INLINE
Definition: clang.h:147