thrust/random/subtract_with_carry_engine.h

File members: thrust/random/subtract_with_carry_engine.h

/*
 *  Copyright 2008-2013 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#pragma once

#include <thrust/detail/config.h>

#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
#  pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
#  pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
#  pragma system_header
#endif // no system header

#include <thrust/random/detail/random_core_access.h>

#include <cstddef> // for size_t
#include <cstdint>
#include <iostream>

THRUST_NAMESPACE_BEGIN

namespace random
{

template <typename UIntType, size_t w, size_t s, size_t r>
class subtract_with_carry_engine
{
private:
  static const UIntType modulus = UIntType(1) << w;
public:
  // types

  using result_type = UIntType;

  // engine characteristics

  static const size_t word_size = w;

  static const size_t short_lag = s;

  static const size_t long_lag = r;

  static const result_type min = 0;

  static const result_type max = modulus - 1;

  static const result_type default_seed = 19780503u;

  // constructors and seeding functions

  _CCCL_HOST_DEVICE explicit subtract_with_carry_engine(result_type value = default_seed);

  _CCCL_HOST_DEVICE void seed(result_type value = default_seed);

  // generating functions

  _CCCL_HOST_DEVICE result_type operator()(void);

  _CCCL_HOST_DEVICE void discard(unsigned long long z);

private:
  result_type m_x[long_lag];
  unsigned int m_k;
  int m_carry;

  friend struct thrust::random::detail::random_core_access;

  _CCCL_HOST_DEVICE bool equal(const subtract_with_carry_engine& rhs) const;

  template <typename CharT, typename Traits>
  std::basic_ostream<CharT, Traits>& stream_out(std::basic_ostream<CharT, Traits>& os) const;

  template <typename CharT, typename Traits>
  std::basic_istream<CharT, Traits>& stream_in(std::basic_istream<CharT, Traits>& is);

}; // end subtract_with_carry_engine

template <typename UIntType_, size_t w_, size_t s_, size_t r_>
_CCCL_HOST_DEVICE bool operator==(const subtract_with_carry_engine<UIntType_, w_, s_, r_>& lhs,
                                  const subtract_with_carry_engine<UIntType_, w_, s_, r_>& rhs);

template <typename UIntType_, size_t w_, size_t s_, size_t r_>
_CCCL_HOST_DEVICE bool operator!=(const subtract_with_carry_engine<UIntType_, w_, s_, r_>& lhs,
                                  const subtract_with_carry_engine<UIntType_, w_, s_, r_>& rhs);

template <typename UIntType_, size_t w_, size_t s_, size_t r_, typename CharT, typename Traits>
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const subtract_with_carry_engine<UIntType_, w_, s_, r_>& e);

template <typename UIntType_, size_t w_, size_t s_, size_t r_, typename CharT, typename Traits>
std::basic_istream<CharT, Traits>&
operator>>(std::basic_istream<CharT, Traits>& is, subtract_with_carry_engine<UIntType_, w_, s_, r_>& e);

// XXX N2111 uses uint_fast32_t here

using ranlux24_base = subtract_with_carry_engine<std::uint32_t, 24, 10, 24>;

// XXX N2111 uses uint_fast64_t here

using ranlux48_base = subtract_with_carry_engine<std::uint64_t, 48, 5, 12>;

} // namespace random

// import names into thrust::
using random::ranlux24_base;
using random::ranlux48_base;
using random::subtract_with_carry_engine;

THRUST_NAMESPACE_END

#include <thrust/random/detail/subtract_with_carry_engine.inl>