2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-12 20:28:33 +00:00
|
|
|
#ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
|
|
|
#define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2013-03-18 10:12:21 +00:00
|
|
|
#include <sprout/assert.hpp>
|
2012-11-14 02:22:05 +00:00
|
|
|
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
|
|
|
# include <sprout/workaround/recursive_function_template.hpp>
|
|
|
|
#endif
|
2011-10-12 20:28:33 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
namespace detail {
|
|
|
|
template<typename IntType, IntType m>
|
|
|
|
class const_mod {
|
|
|
|
private:
|
|
|
|
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
|
|
|
private:
|
2013-03-18 10:12:21 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR IntType supress_warnings = m == 0;
|
|
|
|
SPROUT_STATIC_CONSTEXPR IntType modulus = m + supress_warnings;
|
2011-10-12 20:28:33 +00:00
|
|
|
private:
|
2013-03-18 10:12:21 +00:00
|
|
|
static_assert(supress_warnings == 0, "supress_warnings == 0");
|
2011-10-12 20:28:33 +00:00
|
|
|
static_assert(modulus == m, "modulus == m");
|
|
|
|
private:
|
|
|
|
static SPROUT_CONSTEXPR IntType pow_1(IntType a, std::uintmax_t exponent, IntType result = 1) {
|
|
|
|
return exponent != 0
|
|
|
|
? pow_1(mult(a, a), exponent / 2, exponent % 2 == 1 ? mult(result, a) : result)
|
|
|
|
: result
|
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult_small(IntType a, IntType x) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return a * x % (m + supress_warnings);
|
2011-10-12 20:28:33 +00:00
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult_schrage_1(IntType a, IntType value, IntType q, IntType r) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(r < q),
|
|
|
|
sub(a * (value % q), r * (value / q))
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult_schrage(IntType a, IntType value) {
|
|
|
|
return mult_schrage_1(a, value, m / a, m % a);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult_general(IntType a, IntType b) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return std::uintmax_t(modulus) <= sprout::numeric_limits<std::uintmax_t>::max() / modulus
|
2011-10-12 20:28:33 +00:00
|
|
|
? static_cast<IntType>(std::uintmax_t(a) * b % modulus)
|
2011-10-26 06:57:31 +00:00
|
|
|
//: static_cast<IntType>(sprout::random::detail::mulmod(a, b, modulus)) // ???
|
2013-03-18 10:12:21 +00:00
|
|
|
: (SPROUT_ASSERT_MSG(0, "Sorry, not implemented."), IntType())
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType sub(IntType a, IntType b) {
|
|
|
|
return a < b ? m - (b - a) : a - b;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR unsigned_type unsigned_m() {
|
2013-08-06 15:15:09 +00:00
|
|
|
return m == 0 ? unsigned_type((sprout::numeric_limits<IntType>::max)()) + 1 : unsigned_type(m);
|
2011-10-12 20:28:33 +00:00
|
|
|
}
|
2012-11-14 02:22:05 +00:00
|
|
|
#ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return n == 0 ? m - l1 : invert_euclidian_1<D + 1>(c, l1, l2, n, p);
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_3(IntType, IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return p == 0 ? l2 : invert_euclidian_3<D + 1>(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_2(IntType, IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return invert_euclidian_2<D + 1>(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_1(IntType, IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(c > 0),
|
|
|
|
c == 1 ? 1 : invert_euclidian_1<D + 1>(c, 0, 1, c, m)
|
2012-11-14 02:22:05 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return n == 0 ? m - l1 : invert_euclidian0_2<D + 1>(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType, IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return p == 0 ? l2 : invert_euclidian0_3<D + 1>(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_2(IntType, IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return SPROUT_ASSERT_MSG(sprout::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
2013-03-18 10:12:21 +00:00
|
|
|
invert_euclidian0_2<D + 1>(
|
2013-08-06 15:15:09 +00:00
|
|
|
c, l1 + (sprout::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
|
|
|
sprout::numeric_limits<IntType>::max() - (sprout::numeric_limits<IntType>::max() / n) * n + 1
|
2012-11-14 02:22:05 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType, IntType, IntType, IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_CONTINUE(D)>
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(c > 0),
|
2013-07-22 13:00:09 +00:00
|
|
|
c == 1 ? 1 : invert_euclidian0_1<D + 1>(c, 0, 1, c)
|
2012-11-14 02:22:05 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<int D = 16, SPROUT_RECURSIVE_FUNCTION_TEMPLATE_BREAK(D)>
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType) {
|
2012-11-14 02:22:05 +00:00
|
|
|
return sprout::throw_recursive_function_template_instantiation_exeeded();
|
|
|
|
}
|
|
|
|
#else
|
2011-10-12 20:28:33 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return n == 0 ? m - l1 : invert_euclidian_1(c, l1, l2, n, p);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return p == 0 ? l2 : invert_euclidian_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return invert_euclidian_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(c > 0),
|
|
|
|
c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return n == 0 ? m - l1 : invert_euclidian0_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
|
|
|
return p == 0 ? l2 : invert_euclidian0_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
|
|
|
}
|
2013-07-22 13:00:09 +00:00
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n) {
|
2013-08-06 15:15:09 +00:00
|
|
|
return SPROUT_ASSERT_MSG(sprout::numeric_limits<IntType>::max() % n != n - 1, "c must be relatively prime to m."),
|
2013-03-18 10:12:21 +00:00
|
|
|
invert_euclidian0_2(
|
2013-08-06 15:15:09 +00:00
|
|
|
c, l1 + (sprout::numeric_limits<IntType>::max() / n) * l2, l2, n,
|
|
|
|
sprout::numeric_limits<IntType>::max() - (sprout::numeric_limits<IntType>::max() / n) * n + 1
|
2012-11-14 02:22:05 +00:00
|
|
|
)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
2013-03-18 10:12:21 +00:00
|
|
|
return SPROUT_ASSERT(c > 0),
|
2013-07-22 13:00:09 +00:00
|
|
|
c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
2012-11-14 02:22:05 +00:00
|
|
|
#endif
|
2011-10-12 20:28:33 +00:00
|
|
|
public:
|
|
|
|
static SPROUT_CONSTEXPR IntType apply(IntType x) {
|
|
|
|
return ((unsigned_m() - 1) & unsigned_m()) == 0
|
|
|
|
? (unsigned_type(x)) & (unsigned_m() - 1)
|
2013-03-18 10:12:21 +00:00
|
|
|
: x % (m + supress_warnings)
|
2011-10-12 20:28:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType add(IntType x, IntType c) {
|
|
|
|
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
|
|
|
: c == 0 ? x
|
|
|
|
: x < m - c ? x + c
|
|
|
|
: x - (m - c)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult(IntType a, IntType x) {
|
|
|
|
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1)
|
|
|
|
: a == 0 ? 0
|
|
|
|
: a == 1 ? x
|
2013-08-06 15:15:09 +00:00
|
|
|
: m <= sprout::numeric_limits<IntType>::max() / a ? mult_small(a, x)
|
|
|
|
: sprout::numeric_limits<IntType>::is_signed && (m % a < m / a) ? mult_schrage(a, x)
|
2011-10-12 20:28:33 +00:00
|
|
|
: mult_general(a, x)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType mult_add(IntType a, IntType x, IntType c) {
|
|
|
|
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
|
|
|
: a == 0 ? c
|
2013-08-06 15:15:09 +00:00
|
|
|
: m <= (sprout::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + supress_warnings)
|
2011-10-12 20:28:33 +00:00
|
|
|
: add(mult(a, x), c)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType pow(IntType a, std::uintmax_t exponent) {
|
|
|
|
return pow_1(a, exponent);
|
|
|
|
}
|
|
|
|
static SPROUT_CONSTEXPR IntType invert(IntType x) {
|
|
|
|
return x == 0 ? 0
|
|
|
|
: m == 0 ? invert_euclidian0(x)
|
|
|
|
: invert_euclidian(x)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
private:
|
2012-12-17 14:10:23 +00:00
|
|
|
const_mod() SPROUT_DELETED_FUNCTION_DECL
|
2011-10-12 20:28:33 +00:00
|
|
|
};
|
2013-03-22 05:24:19 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace random
|
|
|
|
} // namespace sprout
|
2011-10-12 20:28:33 +00:00
|
|
|
|
2013-03-22 05:24:19 +00:00
|
|
|
#endif // #ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|