2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <thrust/detail/cstdint.h>
20 #include <thrust/random/detail/mod.h>
32 template<typename UIntType, UIntType a, unsigned long long c, UIntType m>
33 struct linear_congruential_engine_discard_implementation
36 static void discard(UIntType &state, unsigned long long z)
40 state = detail::mod<UIntType,a,c,m>(state);
43 }; // end linear_congruential_engine_discard
46 // specialize for small integers and c == 0
47 // XXX figure out a robust implemenation of this for any unsigned integer type later
48 template<thrust::detail::uint32_t a, thrust::detail::uint32_t m>
49 struct linear_congruential_engine_discard_implementation<thrust::detail::uint32_t,a,0,m>
52 static void discard(thrust::detail::uint32_t &state, unsigned long long z)
54 const thrust::detail::uint32_t modulus = m;
56 // XXX we need to use unsigned long long here or we will encounter overflow in the
58 // figure out a robust implementation of this later
59 unsigned long long multiplier = a;
60 unsigned long long multiplier_to_z = 1;
62 // see http://en.wikipedia.org/wiki/Modular_exponentiation
67 // multiply in this bit's contribution while using modulus to keep result small
68 multiplier_to_z = (multiplier_to_z * multiplier) % modulus;
71 // move to the next bit of the exponent, square (and mod) the base accordingly
73 multiplier = (multiplier * multiplier) % modulus;
76 state = static_cast<thrust::detail::uint32_t>((multiplier_to_z * state) % modulus);
78 }; // end linear_congruential_engine_discard
81 struct linear_congruential_engine_discard
83 template<typename LinearCongruentialEngine>
85 static void discard(LinearCongruentialEngine &lcg, unsigned long long z)
87 typedef typename LinearCongruentialEngine::result_type result_type;
88 const result_type c = LinearCongruentialEngine::increment;
89 const result_type a = LinearCongruentialEngine::multiplier;
90 const result_type m = LinearCongruentialEngine::modulus;
92 // XXX WAR unused variable warnings
97 linear_congruential_engine_discard_implementation<result_type,a,c,m>::discard(lcg.m_x, z);
99 }; // end linear_congruential_engine_discard