common_factor_rt.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. // (C) Copyright Jeremy William Murphy 2016.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_INTEGER_COMMON_FACTOR_RT_HPP
  6. #define BOOST_INTEGER_COMMON_FACTOR_RT_HPP
  7. #include <boost/assert.hpp>
  8. #include <boost/core/enable_if.hpp>
  9. #include <boost/config.hpp> // for BOOST_NESTED_TEMPLATE, etc.
  10. #include <boost/limits.hpp> // for std::numeric_limits
  11. #include <climits> // for CHAR_MIN
  12. #include <boost/detail/workaround.hpp>
  13. #include <iterator>
  14. #include <algorithm>
  15. #include <limits>
  16. #ifndef BOOST_NO_CXX11_HDR_TYPE_TRAITS
  17. #include <type_traits>
  18. #endif
  19. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  20. #include <functional>
  21. #endif
  22. #if ((defined(BOOST_MSVC) && (BOOST_MSVC >= 1600)) || (defined(__clang__) && defined(__c2__)) || (defined(BOOST_INTEL) && defined(_MSC_VER))) && (defined(_M_IX86) || defined(_M_X64))
  23. #include <intrin.h>
  24. #endif
  25. #ifdef BOOST_MSVC
  26. #pragma warning(push)
  27. #pragma warning(disable:4127 4244) // Conditional expression is constant
  28. #endif
  29. #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) && !defined(BOOST_NO_CXX11_NOEXCEPT)
  30. #define BOOST_GCD_NOEXCEPT(T) noexcept(std::is_arithmetic<T>::value)
  31. #else
  32. #define BOOST_GCD_NOEXCEPT(T)
  33. #endif
  34. namespace boost {
  35. template <class I>
  36. class rational;
  37. namespace integer {
  38. namespace gcd_detail{
  39. //
  40. // some helper functions which really should be constexpr already, but sadly aren't:
  41. //
  42. template <class T>
  43. inline BOOST_CONSTEXPR T constexpr_min(T const& a, T const& b) BOOST_GCD_NOEXCEPT(T)
  44. {
  45. return a < b ? a : b;
  46. }
  47. #ifndef BOOST_NO_CXX14_CONSTEXPR
  48. template <class T>
  49. inline constexpr auto constexpr_swap(T& a, T& b) BOOST_GCD_NOEXCEPT(T) -> decltype(a.swap(b))
  50. {
  51. return a.swap(b);
  52. }
  53. template <class T, class U>
  54. inline constexpr void constexpr_swap(T& a, U& b, ...) BOOST_GCD_NOEXCEPT(T)
  55. {
  56. T t(static_cast<T&&>(a));
  57. a = static_cast<T&&>(b);
  58. b = static_cast<T&&>(t);
  59. }
  60. #else
  61. template <class T>
  62. inline void constexpr_swap(T& a, T& b) BOOST_GCD_NOEXCEPT(T)
  63. {
  64. using std::swap;
  65. swap(a, b);
  66. }
  67. #endif
  68. template <class T, bool a =
  69. #ifndef BOOST_NO_CXX11_HDR_TYPE_TRAITS
  70. std::is_unsigned<T>::value ||
  71. #endif
  72. (std::numeric_limits<T>::is_specialized && !std::numeric_limits<T>::is_signed)>
  73. struct gcd_traits_abs_defaults
  74. {
  75. inline static BOOST_CXX14_CONSTEXPR const T& abs(const T& val) BOOST_GCD_NOEXCEPT(T) { return val; }
  76. };
  77. template <class T>
  78. struct gcd_traits_abs_defaults<T, false>
  79. {
  80. inline static T BOOST_CXX14_CONSTEXPR abs(const T& val) BOOST_GCD_NOEXCEPT(T)
  81. {
  82. // This sucks, but std::abs is not constexpr :(
  83. return val < T(0) ? -val : val;
  84. }
  85. };
  86. enum method_type
  87. {
  88. method_euclid = 0,
  89. method_binary = 1,
  90. method_mixed = 2
  91. };
  92. struct any_convert
  93. {
  94. template <class T>
  95. any_convert(const T&);
  96. };
  97. struct unlikely_size
  98. {
  99. char buf[9973];
  100. };
  101. unlikely_size operator <<= (any_convert, any_convert);
  102. unlikely_size operator >>= (any_convert, any_convert);
  103. template <class T>
  104. struct gcd_traits_defaults : public gcd_traits_abs_defaults<T>
  105. {
  106. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(T& val) BOOST_GCD_NOEXCEPT(T)
  107. {
  108. unsigned r = 0;
  109. while (T(0) == (val & 1u))
  110. {
  111. #ifdef _MSC_VER // VC++ can't handle operator >>= in constexpr code for some reason
  112. val = val >> 1;
  113. #else
  114. val >>= 1;
  115. #endif
  116. ++r;
  117. }
  118. return r;
  119. }
  120. inline static BOOST_CXX14_CONSTEXPR bool less(const T& a, const T& b) BOOST_GCD_NOEXCEPT(T)
  121. {
  122. return a < b;
  123. }
  124. static T& get_value();
  125. #ifndef BOOST_NO_SFINAE
  126. static const bool has_operator_left_shift_equal = sizeof(get_value() <<= 2) != sizeof(unlikely_size);
  127. static const bool has_operator_right_shift_equal = sizeof(get_value() >>= 2) != sizeof(unlikely_size);
  128. #else
  129. static const bool has_operator_left_shift_equal = true;
  130. static const bool has_operator_right_shift_equal = true;
  131. #endif
  132. 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;
  133. };
  134. //
  135. // Default gcd_traits just inherits from defaults:
  136. //
  137. template <class T>
  138. struct gcd_traits : public gcd_traits_defaults<T> {};
  139. //
  140. // Some platforms have fast bitscan operations, that allow us to implement
  141. // make_odd much more efficiently, unfortunately we can't use these if we want
  142. // the functions to be constexpr as the compiler intrinsics aren't constexpr.
  143. //
  144. #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))
  145. #pragma intrinsic(_BitScanForward,)
  146. template <>
  147. struct gcd_traits<unsigned long> : public gcd_traits_defaults<unsigned long>
  148. {
  149. BOOST_FORCEINLINE static unsigned find_lsb(unsigned long val) BOOST_NOEXCEPT
  150. {
  151. unsigned long result;
  152. _BitScanForward(&result, val);
  153. return result;
  154. }
  155. BOOST_FORCEINLINE static unsigned make_odd(unsigned long& val) BOOST_NOEXCEPT
  156. {
  157. unsigned result = find_lsb(val);
  158. val >>= result;
  159. return result;
  160. }
  161. };
  162. #ifdef _M_X64
  163. #pragma intrinsic(_BitScanForward64)
  164. template <>
  165. struct gcd_traits<unsigned __int64> : public gcd_traits_defaults<unsigned __int64>
  166. {
  167. BOOST_FORCEINLINE static unsigned find_lsb(unsigned __int64 mask) BOOST_NOEXCEPT
  168. {
  169. unsigned long result;
  170. _BitScanForward64(&result, mask);
  171. return result;
  172. }
  173. BOOST_FORCEINLINE static unsigned make_odd(unsigned __int64& val) BOOST_NOEXCEPT
  174. {
  175. unsigned result = find_lsb(val);
  176. val >>= result;
  177. return result;
  178. }
  179. };
  180. #endif
  181. //
  182. // Other integer type are trivial adaptations of the above,
  183. // this works for signed types too, as by the time these functions
  184. // are called, all values are > 0.
  185. //
  186. template <> struct gcd_traits<long> : public gcd_traits_defaults<long>
  187. { BOOST_FORCEINLINE static unsigned make_odd(long& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  188. template <> struct gcd_traits<unsigned int> : public gcd_traits_defaults<unsigned int>
  189. { BOOST_FORCEINLINE static unsigned make_odd(unsigned int& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  190. template <> struct gcd_traits<int> : public gcd_traits_defaults<int>
  191. { BOOST_FORCEINLINE static unsigned make_odd(int& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  192. template <> struct gcd_traits<unsigned short> : public gcd_traits_defaults<unsigned short>
  193. { BOOST_FORCEINLINE static unsigned make_odd(unsigned short& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  194. template <> struct gcd_traits<short> : public gcd_traits_defaults<short>
  195. { BOOST_FORCEINLINE static unsigned make_odd(short& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  196. template <> struct gcd_traits<unsigned char> : public gcd_traits_defaults<unsigned char>
  197. { BOOST_FORCEINLINE static unsigned make_odd(unsigned char& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  198. template <> struct gcd_traits<signed char> : public gcd_traits_defaults<signed char>
  199. { BOOST_FORCEINLINE static unsigned make_odd(signed char& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  200. template <> struct gcd_traits<char> : public gcd_traits_defaults<char>
  201. { BOOST_FORCEINLINE static unsigned make_odd(char& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  202. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  203. template <> struct gcd_traits<wchar_t> : public gcd_traits_defaults<wchar_t>
  204. { BOOST_FORCEINLINE static unsigned make_odd(wchar_t& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned long>::find_lsb(val); val >>= result; return result; } };
  205. #endif
  206. #ifdef _M_X64
  207. template <> struct gcd_traits<__int64> : public gcd_traits_defaults<__int64>
  208. { BOOST_FORCEINLINE static unsigned make_odd(__int64& val)BOOST_NOEXCEPT{ unsigned result = gcd_traits<unsigned __int64>::find_lsb(val); val >>= result; return result; } };
  209. #endif
  210. #elif defined(BOOST_GCC) || defined(__clang__) || (defined(BOOST_INTEL) && defined(__GNUC__))
  211. template <>
  212. struct gcd_traits<unsigned> : public gcd_traits_defaults<unsigned>
  213. {
  214. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(unsigned mask)BOOST_NOEXCEPT
  215. {
  216. return __builtin_ctz(mask);
  217. }
  218. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned& val)BOOST_NOEXCEPT
  219. {
  220. unsigned result = find_lsb(val);
  221. val >>= result;
  222. return result;
  223. }
  224. };
  225. template <>
  226. struct gcd_traits<unsigned long> : public gcd_traits_defaults<unsigned long>
  227. {
  228. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(unsigned long mask)BOOST_NOEXCEPT
  229. {
  230. return __builtin_ctzl(mask);
  231. }
  232. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(unsigned long& val)BOOST_NOEXCEPT
  233. {
  234. unsigned result = find_lsb(val);
  235. val >>= result;
  236. return result;
  237. }
  238. };
  239. template <>
  240. struct gcd_traits<boost::ulong_long_type> : public gcd_traits_defaults<boost::ulong_long_type>
  241. {
  242. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned find_lsb(boost::ulong_long_type mask)BOOST_NOEXCEPT
  243. {
  244. return __builtin_ctzll(mask);
  245. }
  246. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(boost::ulong_long_type& val)BOOST_NOEXCEPT
  247. {
  248. unsigned result = find_lsb(val);
  249. val >>= result;
  250. return result;
  251. }
  252. };
  253. //
  254. // Other integer type are trivial adaptations of the above,
  255. // this works for signed types too, as by the time these functions
  256. // are called, all values are > 0.
  257. //
  258. template <> struct gcd_traits<boost::long_long_type> : public gcd_traits_defaults<boost::long_long_type>
  259. {
  260. 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; }
  261. };
  262. template <> struct gcd_traits<long> : public gcd_traits_defaults<long>
  263. {
  264. 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; }
  265. };
  266. template <> struct gcd_traits<int> : public gcd_traits_defaults<int>
  267. {
  268. 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; }
  269. };
  270. template <> struct gcd_traits<unsigned short> : public gcd_traits_defaults<unsigned short>
  271. {
  272. 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; }
  273. };
  274. template <> struct gcd_traits<short> : public gcd_traits_defaults<short>
  275. {
  276. 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; }
  277. };
  278. template <> struct gcd_traits<unsigned char> : public gcd_traits_defaults<unsigned char>
  279. {
  280. 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; }
  281. };
  282. template <> struct gcd_traits<signed char> : public gcd_traits_defaults<signed char>
  283. {
  284. BOOST_FORCEINLINE static BOOST_CXX14_CONSTEXPR unsigned make_odd(signed char& val)BOOST_NOEXCEPT { unsigned result = gcd_traits<unsigned>::find_lsb(val); val >>= result; return result; }
  285. };
  286. template <> struct gcd_traits<char> : public gcd_traits_defaults<char>
  287. {
  288. 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; }
  289. };
  290. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  291. template <> struct gcd_traits<wchar_t> : public gcd_traits_defaults<wchar_t>
  292. {
  293. 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; }
  294. };
  295. #endif
  296. #endif
  297. //
  298. // The Mixed Binary Euclid Algorithm
  299. // Sidi Mohamed Sedjelmaci
  300. // Electronic Notes in Discrete Mathematics 35 (2009) 169-176
  301. //
  302. template <class T>
  303. BOOST_CXX14_CONSTEXPR T mixed_binary_gcd(T u, T v) BOOST_GCD_NOEXCEPT(T)
  304. {
  305. if(gcd_traits<T>::less(u, v))
  306. constexpr_swap(u, v);
  307. unsigned shifts = 0;
  308. if(u == T(0))
  309. return v;
  310. if(v == T(0))
  311. return u;
  312. shifts = constexpr_min(gcd_traits<T>::make_odd(u), gcd_traits<T>::make_odd(v));
  313. while(gcd_traits<T>::less(1, v))
  314. {
  315. u %= v;
  316. v -= u;
  317. if(u == T(0))
  318. return v << shifts;
  319. if(v == T(0))
  320. return u << shifts;
  321. gcd_traits<T>::make_odd(u);
  322. gcd_traits<T>::make_odd(v);
  323. if(gcd_traits<T>::less(u, v))
  324. constexpr_swap(u, v);
  325. }
  326. return (v == 1 ? v : u) << shifts;
  327. }
  328. /** Stein gcd (aka 'binary gcd')
  329. *
  330. * From Mathematics to Generic Programming, Alexander Stepanov, Daniel Rose
  331. */
  332. template <typename SteinDomain>
  333. BOOST_CXX14_CONSTEXPR SteinDomain Stein_gcd(SteinDomain m, SteinDomain n) BOOST_GCD_NOEXCEPT(SteinDomain)
  334. {
  335. BOOST_ASSERT(m >= 0);
  336. BOOST_ASSERT(n >= 0);
  337. if (m == SteinDomain(0))
  338. return n;
  339. if (n == SteinDomain(0))
  340. return m;
  341. // m > 0 && n > 0
  342. unsigned d_m = gcd_traits<SteinDomain>::make_odd(m);
  343. unsigned d_n = gcd_traits<SteinDomain>::make_odd(n);
  344. // odd(m) && odd(n)
  345. while (m != n)
  346. {
  347. if (n > m)
  348. constexpr_swap(n, m);
  349. m -= n;
  350. gcd_traits<SteinDomain>::make_odd(m);
  351. }
  352. // m == n
  353. m <<= constexpr_min(d_m, d_n);
  354. return m;
  355. }
  356. /** Euclidean algorithm
  357. *
  358. * From Mathematics to Generic Programming, Alexander Stepanov, Daniel Rose
  359. *
  360. */
  361. template <typename EuclideanDomain>
  362. inline BOOST_CXX14_CONSTEXPR EuclideanDomain Euclid_gcd(EuclideanDomain a, EuclideanDomain b) BOOST_GCD_NOEXCEPT(EuclideanDomain)
  363. {
  364. while (b != EuclideanDomain(0))
  365. {
  366. a %= b;
  367. constexpr_swap(a, b);
  368. }
  369. return a;
  370. }
  371. template <typename T>
  372. inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_mixed, T>::type
  373. optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
  374. {
  375. return gcd_detail::mixed_binary_gcd(a, b);
  376. }
  377. template <typename T>
  378. inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_binary, T>::type
  379. optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
  380. {
  381. return gcd_detail::Stein_gcd(a, b);
  382. }
  383. template <typename T>
  384. inline BOOST_CXX14_CONSTEXPR BOOST_DEDUCED_TYPENAME enable_if_c<gcd_traits<T>::method == method_euclid, T>::type
  385. optimal_gcd_select(T const &a, T const &b) BOOST_GCD_NOEXCEPT(T)
  386. {
  387. return gcd_detail::Euclid_gcd(a, b);
  388. }
  389. template <class T>
  390. inline BOOST_CXX14_CONSTEXPR T lcm_imp(const T& a, const T& b) BOOST_GCD_NOEXCEPT(T)
  391. {
  392. T temp = boost::integer::gcd_detail::optimal_gcd_select(a, b);
  393. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
  394. return (temp != T(0)) ? T(a / temp * b) : T(0);
  395. #else
  396. return temp != T(0) ? T(a / temp * b) : T(0);
  397. #endif
  398. }
  399. } // namespace detail
  400. template <typename Integer>
  401. inline BOOST_CXX14_CONSTEXPR Integer gcd(Integer const &a, Integer const &b) BOOST_GCD_NOEXCEPT(Integer)
  402. {
  403. if(a == (std::numeric_limits<Integer>::min)())
  404. return a == static_cast<Integer>(0) ? gcd_detail::gcd_traits<Integer>::abs(b) : boost::integer::gcd(static_cast<Integer>(a % b), b);
  405. else if (b == (std::numeric_limits<Integer>::min)())
  406. return b == static_cast<Integer>(0) ? gcd_detail::gcd_traits<Integer>::abs(a) : boost::integer::gcd(a, static_cast<Integer>(b % a));
  407. 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)));
  408. }
  409. template <typename Integer>
  410. inline BOOST_CXX14_CONSTEXPR Integer lcm(Integer const &a, Integer const &b) BOOST_GCD_NOEXCEPT(Integer)
  411. {
  412. 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)));
  413. }
  414. #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
  415. //
  416. // This looks slightly odd, but the variadic forms must have 3 or more arguments, and the variadic argument pack may be empty.
  417. // This matters not at all for most compilers, but Oracle C++ selects the wrong overload in the 2-arg case unless we do this.
  418. //
  419. template <typename Integer, typename... Args>
  420. inline BOOST_CXX14_CONSTEXPR Integer gcd(Integer const &a, Integer const &b, const Integer& c, Args const&... args) BOOST_GCD_NOEXCEPT(Integer)
  421. {
  422. Integer t = gcd(b, c, args...);
  423. return t == 1 ? 1 : gcd(a, t);
  424. }
  425. template <typename Integer, typename... Args>
  426. inline BOOST_CXX14_CONSTEXPR Integer lcm(Integer const &a, Integer const &b, Integer const& c, Args const&... args) BOOST_GCD_NOEXCEPT(Integer)
  427. {
  428. return lcm(a, lcm(b, c, args...));
  429. }
  430. #endif
  431. //
  432. // Special handling for rationals:
  433. //
  434. template <typename Integer>
  435. 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)
  436. {
  437. return boost::rational<Integer>(static_cast<Integer>(gcd(a.numerator(), b.numerator())), static_cast<Integer>(lcm(a.denominator(), b.denominator())));
  438. }
  439. template <typename Integer>
  440. 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)
  441. {
  442. return boost::rational<Integer>(static_cast<Integer>(lcm(a.numerator(), b.numerator())), static_cast<Integer>(gcd(a.denominator(), b.denominator())));
  443. }
  444. /**
  445. * Knuth, The Art of Computer Programming: Volume 2, Third edition, 1998
  446. * Chapter 4.5.2, Algorithm C: Greatest common divisor of n integers.
  447. *
  448. * Knuth counts down from n to zero but we naturally go from first to last.
  449. * We also return the termination position because it might be useful to know.
  450. *
  451. * Partly by quirk, partly by design, this algorithm is defined for n = 1,
  452. * because the gcd of {x} is x. It is not defined for n = 0.
  453. *
  454. * @tparam I Input iterator.
  455. * @return The gcd of the range and the iterator position at termination.
  456. */
  457. template <typename I>
  458. std::pair<typename std::iterator_traits<I>::value_type, I>
  459. gcd_range(I first, I last) BOOST_GCD_NOEXCEPT(I)
  460. {
  461. BOOST_ASSERT(first != last);
  462. typedef typename std::iterator_traits<I>::value_type T;
  463. T d = *first;
  464. ++first;
  465. while (d != T(1) && first != last)
  466. {
  467. d = gcd(d, *first);
  468. ++first;
  469. }
  470. return std::make_pair(d, first);
  471. }
  472. template <typename I>
  473. std::pair<typename std::iterator_traits<I>::value_type, I>
  474. lcm_range(I first, I last) BOOST_GCD_NOEXCEPT(I)
  475. {
  476. BOOST_ASSERT(first != last);
  477. typedef typename std::iterator_traits<I>::value_type T;
  478. T d = *first;
  479. ++first;
  480. while (d != T(0) && first != last)
  481. {
  482. d = lcm(d, *first);
  483. ++first;
  484. }
  485. return std::make_pair(d, first);
  486. }
  487. template < typename IntegerType >
  488. class gcd_evaluator
  489. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  490. : public std::binary_function<IntegerType, IntegerType, IntegerType>
  491. #endif
  492. {
  493. public:
  494. #ifndef BOOST_NO_CXX11_HDR_FUNCTIONAL
  495. typedef IntegerType first_argument_type;
  496. typedef IntegerType second_argument_type;
  497. typedef IntegerType result_type;
  498. #endif
  499. IntegerType operator()(IntegerType const &a, IntegerType const &b) const
  500. {
  501. return boost::integer::gcd(a, b);
  502. }
  503. };
  504. template < typename IntegerType >
  505. class lcm_evaluator
  506. #ifdef BOOST_NO_CXX11_HDR_FUNCTIONAL
  507. : public std::binary_function<IntegerType, IntegerType, IntegerType>
  508. #endif
  509. {
  510. public:
  511. #ifndef BOOST_NO_CXX11_HDR_FUNCTIONAL
  512. typedef IntegerType first_argument_type;
  513. typedef IntegerType second_argument_type;
  514. typedef IntegerType result_type;
  515. #endif
  516. IntegerType operator()(IntegerType const &a, IntegerType const &b)const
  517. {
  518. return boost::integer::lcm(a, b);
  519. }
  520. };
  521. } // namespace integer
  522. } // namespace boost
  523. #ifdef BOOST_MSVC
  524. #pragma warning(pop)
  525. #endif
  526. #endif // BOOST_INTEGER_COMMON_FACTOR_RT_HPP