1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-12-03 21:15:42 +00:00
Sprout/sprout/random/detail/const_mod.hpp

219 lines
11 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 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>
#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)
//: 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
}
#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) {
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) {
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) {
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)
;
}
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) {
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) {
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) {
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
)
;
}
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) {
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)
;
}
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) {
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
)
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
;
}
#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