1 // (C) Copyright Jeremy William Murphy 2016.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_INTEGER_COMMON_FACTOR_RT_HPP
8 #define BOOST_INTEGER_COMMON_FACTOR_RT_HPP
10 #include <boost/assert.hpp>
11 #include <boost/core/enable_if.hpp>
13 #include <boost/config.hpp> // for BOOST_NESTED_TEMPLATE, etc.
14 #include <boost/limits.hpp> // for std::numeric_limits
15 #include <climits> // for CHAR_MIN
16 #include <boost/detail/workaround.hpp>
20 #ifndef BOOST_NO_CXX11_HDR_TYPE_TRAITS
21 #include <type_traits>
23 #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
27 #if ((defined(BOOST_MSVC) && (BOOST_MSVC >= 1600)) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
33 #pragma warning(disable:4127 4244) // Conditional expression is constant
36 #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) && !defined(BOOST_NO_CXX11_NOEXCEPT)
37 #define BOOST_GCD_NOEXCEPT(T) noexcept(std::is_arithmetic<T>::value)
39 #define BOOST_GCD_NOEXCEPT(T)
52 // some helper functions which really should be constexpr already, but sadly aren't:
54 #ifndef BOOST_NO_CXX14_CONSTEXPR
56 inline constexpr T constexpr_min(T const& a, T const& b) BOOST_GCD_NOEXCEPT(T)
61 inline constexpr auto constexpr_swap(T&a, T& b) BOOST_GCD_NOEXCEPT(T) -> decltype(a.swap(b))
65 template <class T, class U>
66 inline constexpr void constexpr_swap(T&a, U& b...) BOOST_GCD_NOEXCEPT(T)
68 T t(static_cast<T&&>(a));
69 a = static_cast<T&&>(b);
70 b = static_cast<T&&>(t);
74 inline T constexpr_min(T const& a, T const& b) BOOST_GCD_NOEXCEPT(T)
79 inline void constexpr_swap(T&a, T& b) BOOST_GCD_NOEXCEPT(T)
86 template <class T, bool a =
87 #ifndef BOOST_NO_CXX11_HDR_TYPE_TRAITS
88 std::is_unsigned<T>::value ||
90 (std::numeric_limits<T>::is_specialized && !std::numeric_limits<T>::is_signed)>
91 struct gcd_traits_abs_defaults
93 inline static BOOST_CXX14_CONSTEXPR const T& abs(const T& val) BOOST_GCD_NOEXCEPT(T) { return val; }
96 struct gcd_traits_abs_defaults<T, false>
98 inline static T BOOST_CXX14_CONSTEXPR abs(const T& val) BOOST_GCD_NOEXCEPT(T)
100 // This sucks, but std::abs is not constexpr :(
101 return val < T(0) ? -val : val;
115 any_convert(const T&);
123 unlikely_size operator <<= (any_convert, any_convert);
124 unlikely_size operator >>= (any_convert, any_convert);
127 struct gcd_traits_defaults : public gcd_traits_abs_defaults<T>
129 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(T& val) BOOST_GCD_NOEXCEPT(T)
132 while(0 == (val & 1u))
134 #ifdef _MSC_VER // VC++ can't handle operator >>= in constexpr code for some reason
143 inline static BOOST_CXX14_CONSTEXPR bool less(const T& a, const T& b) BOOST_GCD_NOEXCEPT(T)
148 static T& get_value();
150 #ifndef BOOST_NO_SFINAE
151 static const bool has_operator_left_shift_equal = sizeof(get_value() <<= 2) != sizeof(unlikely_size);
152 static const bool has_operator_right_shift_equal = sizeof(get_value() >>= 2) != sizeof(unlikely_size);
154 static const bool has_operator_left_shift_equal = true;
155 static const bool has_operator_right_shift_equal = true;
157 static const method_type method = std::numeric_limits<T>::is_specialized && std::numeric_limits<T>::is_integer && has_operator_left_shift_equal && has_operator_right_shift_equal ? method_mixed : method_euclid;
160 // Default gcd_traits just inherits from defaults:
163 struct gcd_traits : public gcd_traits_defaults<T> {};
166 // Some platforms have fast bitscan operations, that allow us to implement
167 // make_odd much more efficiently, unfortunately we can't use these if we want
168 // the functions to be constexpr as the compiler intrinsics aren't constexpr.
170 #if defined(BOOST_NO_CXX14_CONSTEXPR) && ((defined(BOOST_MSVC) && (BOOST_MSVC >= 1600)) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
171 #pragma intrinsic(_BitScanForward,)
173 struct gcd_traits<unsigned long> : public gcd_traits_defaults<unsigned long>
175 BOOST_FORCEINLINE static unsigned find_lsb(unsigned long val) BOOST_NOEXCEPT
177 unsigned long result;
178 _BitScanForward(&result, val);
181 BOOST_FORCEINLINE static unsigned make_odd(unsigned long& val) BOOST_NOEXCEPT
183 unsigned result = find_lsb(val);
190 #pragma intrinsic(_BitScanForward64)
192 struct gcd_traits<unsigned __int64> : public gcd_traits_defaults<unsigned __int64>
194 BOOST_FORCEINLINE static unsigned find_lsb(unsigned __int64 mask) BOOST_NOEXCEPT
196 unsigned long result;
197 _BitScanForward64(&result, mask);
200 BOOST_FORCEINLINE static unsigned make_odd(unsigned __int64& val) BOOST_NOEXCEPT
202 unsigned result = find_lsb(val);
209 // Other integer type are trivial adaptations of the above,
210 // this works for signed types too, as by the time these functions
211 // are called, all values are > 0.
213 template <> struct gcd_traits<long> : public gcd_traits_defaults<long>
214 { BOOST_FORCEINLINE static unsigned make_odd(long& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
215 template <> struct gcd_traits<unsigned int> : public gcd_traits_defaults<unsigned int>
216 { BOOST_FORCEINLINE static unsigned make_odd(unsigned int& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
217 template <> struct gcd_traits<int> : public gcd_traits_defaults<int>
218 { BOOST_FORCEINLINE static unsigned make_odd(int& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
219 template <> struct gcd_traits<unsigned short> : public gcd_traits_defaults<unsigned short>
220 { BOOST_FORCEINLINE static unsigned make_odd(unsigned short& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
221 template <> struct gcd_traits<short> : public gcd_traits_defaults<short>
222 { BOOST_FORCEINLINE static unsigned make_odd(short& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
223 template <> struct gcd_traits<unsigned char> : public gcd_traits_defaults<unsigned char>
224 { BOOST_FORCEINLINE static unsigned make_odd(unsigned char& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
225 template <> struct gcd_traits<signed char> : public gcd_traits_defaults<signed char>
226 { BOOST_FORCEINLINE static signed make_odd(signed char& val)BOOST_NOEXCEPT{ signed result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
227 template <> struct gcd_traits<char> : public gcd_traits_defaults<char>
228 { BOOST_FORCEINLINE static unsigned make_odd(char& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
229 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
230 template <> struct gcd_traits<wchar_t> : public gcd_traits_defaults<wchar_t>
231 { BOOST_FORCEINLINE static unsigned make_odd(wchar_t& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
234 template <> struct gcd_traits<__int64> : public gcd_traits_defaults<__int64>
235 { BOOST_FORCEINLINE static unsigned make_odd(__int64& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned __int64>::find_lsb(val); val >>= result; return result; } };
238 #elif defined(BOOST_GCC) || defined(__clang__) || (defined(BOOST_INTEL) && defined(__GNUC__))
241 struct gcd_traits<unsigned> : public gcd_traits_defaults<unsigned>
243 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(unsigned mask)BOOST_NOEXCEPT
245 return __builtin_ctz(mask);
247 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned& val)BOOST_NOEXCEPT
249 unsigned result = find_lsb(val);
255 struct gcd_traits<unsigned long> : public gcd_traits_defaults<unsigned long>
257 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(unsigned long mask)BOOST_NOEXCEPT
259 return __builtin_ctzl(mask);
261 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned long& val)BOOST_NOEXCEPT
263 unsigned result = find_lsb(val);
269 struct gcd_traits<boost::ulong_long_type> : public gcd_traits_defaults<boost::ulong_long_type>
271 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(boost::ulong_long_type mask)BOOST_NOEXCEPT
273 return __builtin_ctzll(mask);
275 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(boost::ulong_long_type& val)BOOST_NOEXCEPT
277 unsigned result = find_lsb(val);
283 // Other integer type are trivial adaptations of the above,
284 // this works for signed types too, as by the time these functions
285 // are called, all values are > 0.
287 template <> struct gcd_traits<boost::long_long_type> : public gcd_traits_defaults<boost::long_long_type>
289 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(boost::long_long_type& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<boost::ulong_long_type>::find_lsb(val); val >>= result; return result; }
291 template <> struct gcd_traits<long> : public gcd_traits_defaults<long>
293 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(long& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; }
295 template <> struct gcd_traits<int> : public gcd_traits_defaults<int>
297 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(int& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; }
299 template <> struct gcd_traits<unsigned short> : public gcd_traits_defaults<unsigned short>
301 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned short& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
303 template <> struct gcd_traits<short> : public gcd_traits_defaults<short>
305 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(short& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
307 template <> struct gcd_traits<unsigned char> : public gcd_traits_defaults<unsigned char>
309 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned char& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
311 template <> struct gcd_traits<signed char> : public gcd_traits_defaults<signed char>
313 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR signed make_odd(signed char& val)BOOST_NOEXCEPT { signed result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
315 template <> struct gcd_traits<char> : public gcd_traits_defaults<char>
317 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(char& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
319 #ifndef BOOST_NO_INTRINSIC_WCHAR_T
320 template <> struct gcd_traits<wchar_t> : public gcd_traits_defaults<wchar_t>
322 BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(wchar_t& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
327 // The Mixed Binary Euclid Algorithm
328 // Sidi Mohamed Sedjelmaci
329 // Electronic Notes in Discrete Mathematics 35 (2009) 169-176
332 BOOST_CXX14_CONSTEXPR T mixed_binary_gcd(T u, T v) BOOST_GCD_NOEXCEPT(T)
334 if(gcd_traits<T>::less(u, v))
335 constexpr_swap(u, v);
344 shifts = constexpr_min(gcd_traits<T>::make_odd(u), gcd_traits<T>::make_odd(v));
346 while(gcd_traits<T>::less(1, v))
354 gcd_traits<T>::make_odd(u);
355 gcd_traits<T>::make_odd(v);
356 if(gcd_traits<T>::less(u, v))
357 constexpr_swap(u, v);
359 return (v == 1 ? v : u) << shifts;
362 /** Stein gcd (aka 'binary gcd')
364 * From Mathematics to Generic Programming, Alexander Stepanov, Daniel Rose
366 template <typename SteinDomain>
367 BOOST_CXX14_CONSTEXPR SteinDomain Stein_gcd(SteinDomain m, SteinDomain n) BOOST_GCD_NOEXCEPT(SteinDomain)
369 BOOST_ASSERT(m >= 0);
370 BOOST_ASSERT(n >= 0);
371 if (m == SteinDomain(0))
373 if (n == SteinDomain(0))
376 int d_m = gcd_traits<SteinDomain>::make_odd(m);
377 int d_n = gcd_traits<SteinDomain>::make_odd(n);
382 constexpr_swap(n, m);
384 gcd_traits<SteinDomain>::make_odd(m);
387 m <<= constexpr_min(d_m, d_n);
392 /** Euclidean algorithm
394 * From Mathematics to Generic Programming, Alexander Stepanov, Daniel Rose
397 template <typename EuclideanDomain>
398 inline BOOST_CXX14_CONSTEXPR EuclideanDomain Euclid_gcd(EuclideanDomain a, EuclideanDomain b) BOOST_GCD_NOEXCEPT(EuclideanDomain)
400 while (b != EuclideanDomain(0))
403 constexpr_swap(a, b);
409 template <typename T>
410 inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_mixed, T>::type
411 optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
413 return gcd_detail::mixed_binary_gcd(a, b);
416 template <typename T>
417 inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_binary, T>::type
418 optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
420 return gcd_detail::Stein_gcd(a, b);
423 template <typename T>
424 inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_euclid, T>::type
425 optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
427 return gcd_detail::Euclid_gcd(a, b);
431 inline BOOST_CXX14_CONSTEXPR T lcm_imp(const T& a, const T& b) BOOST_GCD_NOEXCEPT(T)
433 T temp = boost::integer::gcd_detail::optimal_gcd_select(a, b);
434 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
435 return (temp != T(0)) ? T(a / temp * b) : T(0);
437 return temp != T(0) ? T(a / temp * b) : T(0);
441 } // namespace detail
444 template <typename Integer>
445 inline BOOST_CXX14_CONSTEXPR Integer gcd(Integer const &a, Integer const &b) BOOST_GCD_NOEXCEPT(Integer)
447 if(a == (std::numeric_limits<Integer>::min)())
448 return a == static_cast<Integer>(0) ? gcd_detail::gcd_traits<Integer>::abs(b) : boost::integer::gcd(static_cast<Integer>(a % b), b);
449 else if (b == (std::numeric_limits<Integer>::min)())
450 return b == static_cast<Integer>(0) ? gcd_detail::gcd_traits<Integer>::abs(a) : boost::integer::gcd(a, static_cast<Integer>(b % a));
451 return gcd_detail::optimal_gcd_select(static_cast<Integer>(gcd_detail::gcd_traits<Integer>::abs(a)), static_cast<Integer>(gcd_detail::gcd_traits<Integer>::abs(b)));
454 template <typename Integer>
455 inline BOOST_CXX14_CONSTEXPR Integer lcm(Integer const &a, Integer const &b) BOOST_GCD_NOEXCEPT(Integer)
457 return gcd_detail::lcm_imp(static_cast<Integer>(gcd_detail::gcd_traits<Integer>::abs(a)), static_cast<Integer>(gcd_detail::gcd_traits<Integer>::abs(b)));
459 #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
461 // This looks slightly odd, but the variadic forms must have 3 or more arguments, and the variadic argument pack may be empty.
462 // This matters not at all for most compilers, but Oracle C++ selects the wrong overload in the 2-arg case unless we do this.
464 template <typename Integer, typename... Args>
465 inline BOOST_CXX14_CONSTEXPR Integer gcd(Integer const &a, Integer const &b, const Integer& c, Args const&... args) BOOST_GCD_NOEXCEPT(Integer)
467 Integer t = gcd(b, c, args...);
468 return t == 1 ? 1 : gcd(a, t);
471 template <typename Integer, typename... Args>
472 inline BOOST_CXX14_CONSTEXPR Integer lcm(Integer const &a, Integer const &b, Integer const& c, Args const&... args) BOOST_GCD_NOEXCEPT(Integer)
474 return lcm(a, lcm(b, c, args...));
478 // Special handling for rationals:
480 template <typename Integer>
481 inline typename boost::enable_if_c<std::numeric_limits<Integer>::is_specialized, boost::rational<Integer> >::type gcd(boost::rational<Integer> const &a, boost::rational<Integer> const &b)
483 return boost::rational<Integer>(static_cast<Integer>(gcd(a.numerator(), b.numerator())), static_cast<Integer>(lcm(a.denominator(), b.denominator())));
486 template <typename Integer>
487 inline typename boost::enable_if_c<std::numeric_limits<Integer>::is_specialized, boost::rational<Integer> >::type lcm(boost::rational<Integer> const &a, boost::rational<Integer> const &b)
489 return boost::rational<Integer>(static_cast<Integer>(lcm(a.numerator(), b.numerator())), static_cast<Integer>(gcd(a.denominator(), b.denominator())));
492 * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
493 * Chapter 4.5.2, Algorithm C: Greatest common divisor of n integers.
495 * Knuth counts down from n to zero but we naturally go from first to last.
496 * We also return the termination position because it might be useful to know.
498 * Partly by quirk, partly by design, this algorithm is defined for n = 1,
499 * because the gcd of {x} is x. It is not defined for n = 0.
501 * @tparam I Input iterator.
502 * @return The gcd of the range and the iterator position at termination.
504 template <typename I>
505 std::pair<typename std::iterator_traits<I>::value_type, I>
506 gcd_range(I first, I last) BOOST_GCD_NOEXCEPT(I)
508 BOOST_ASSERT(first != last);
509 typedef typename std::iterator_traits<I>::value_type T;
512 while (d != T(1) && first != last)
517 return std::make_pair(d, first);
519 template <typename I>
520 std::pair<typename std::iterator_traits<I>::value_type, I>
521 lcm_range(I first, I last) BOOST_GCD_NOEXCEPT(I)
523 BOOST_ASSERT(first != last);
524 typedef typename std::iterator_traits<I>::value_type T;
527 while (d != T(1) && first != last)
532 return std::make_pair(d, first);
535 template < typename IntegerType >
537 #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
538 : public std::binary_function<IntegerType, IntegerType, IntegerType>
542 #ifndef BOOST_NO_CXX11_HDR_FUNCTIONAL
543 typedef IntegerType first_argument_type;
544 typedef IntegerType second_argument_type;
545 typedef IntegerType result_type;
547 IntegerType operator()(IntegerType const &a, IntegerType const &b)const
549 return boost::integer::gcd(a, b);
553 template < typename IntegerType >
555 #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
556 : public std::binary_function<IntegerType, IntegerType, IntegerType>
560 #ifndef BOOST_NO_CXX11_HDR_FUNCTIONAL
561 typedef IntegerType first_argument_type;
562 typedef IntegerType second_argument_type;
563 typedef IntegerType result_type;
565 IntegerType operator()(IntegerType const &a, IntegerType const &b)const
567 return boost::integer::lcm(a, b);
571 } // namespace integer
578 #endif // BOOST_INTEGER_COMMON_FACTOR_RT_HPP