divide.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // 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. //
  6. // Comparison operators for cpp_int_backend:
  7. //
  8. #ifndef BOOST_MP_CPP_INT_DIV_HPP
  9. #define BOOST_MP_CPP_INT_DIV_HPP
  10. namespace boost { namespace multiprecision { namespace backends {
  11. template <class CppInt1, class CppInt2, class CppInt3>
  12. BOOST_MP_CXX14_CONSTEXPR void divide_unsigned_helper(
  13. CppInt1* result,
  14. const CppInt2& x,
  15. const CppInt3& y,
  16. CppInt1& r)
  17. {
  18. if (((void*)result == (void*)&x) || ((void*)&r == (void*)&x))
  19. {
  20. CppInt2 t(x);
  21. divide_unsigned_helper(result, t, y, r);
  22. return;
  23. }
  24. if (((void*)result == (void*)&y) || ((void*)&r == (void*)&y))
  25. {
  26. CppInt3 t(y);
  27. divide_unsigned_helper(result, x, t, r);
  28. return;
  29. }
  30. /*
  31. Very simple, fairly braindead long division.
  32. Start by setting the remainder equal to x, and the
  33. result equal to 0. Then in each loop we calculate our
  34. "best guess" for how many times y divides into r,
  35. add our guess to the result, and subtract guess*y
  36. from the remainder r. One wrinkle is that the remainder
  37. may go negative, in which case we subtract the current guess
  38. from the result rather than adding. The value of the guess
  39. is determined by dividing the most-significant-limb of the
  40. current remainder by the most-significant-limb of y.
  41. Note that there are more efficient algorithms than this
  42. available, in particular see Knuth Vol 2. However for small
  43. numbers of limbs this generally outperforms the alternatives
  44. and avoids the normalisation step which would require extra storage.
  45. */
  46. using default_ops::eval_subtract;
  47. if (result == &r)
  48. {
  49. CppInt1 rem;
  50. divide_unsigned_helper(result, x, y, rem);
  51. r = rem;
  52. return;
  53. }
  54. //
  55. // Find the most significant words of numerator and denominator.
  56. //
  57. limb_type y_order = y.size() - 1;
  58. if (y_order == 0)
  59. {
  60. //
  61. // Only a single non-zero limb in the denominator, in this case
  62. // we can use a specialized divide-by-single-limb routine which is
  63. // much faster. This also handles division by zero:
  64. //
  65. divide_unsigned_helper(result, x, y.limbs()[y_order], r);
  66. return;
  67. }
  68. typename CppInt2::const_limb_pointer px = x.limbs();
  69. typename CppInt3::const_limb_pointer py = y.limbs();
  70. limb_type r_order = x.size() - 1;
  71. if ((r_order == 0) && (*px == 0))
  72. {
  73. // x is zero, so is the result:
  74. r = x;
  75. if (result)
  76. *result = x;
  77. return;
  78. }
  79. r = x;
  80. r.sign(false);
  81. if (result)
  82. *result = static_cast<limb_type>(0u);
  83. //
  84. // Check if the remainder is already less than the divisor, if so
  85. // we already have the result. Note we try and avoid a full compare
  86. // if we can:
  87. //
  88. if (r_order <= y_order)
  89. {
  90. if ((r_order < y_order) || (r.compare_unsigned(y) < 0))
  91. {
  92. return;
  93. }
  94. }
  95. CppInt1 t;
  96. bool r_neg = false;
  97. //
  98. // See if we can short-circuit long division, and use basic arithmetic instead:
  99. //
  100. if (r_order == 0)
  101. {
  102. if (result)
  103. {
  104. *result = px[0] / py[0];
  105. }
  106. r = px[0] % py[0];
  107. return;
  108. }
  109. else if (r_order == 1)
  110. {
  111. double_limb_type a = (static_cast<double_limb_type>(px[1]) << CppInt1::limb_bits) | px[0];
  112. double_limb_type b = y_order ? (static_cast<double_limb_type>(py[1]) << CppInt1::limb_bits) | py[0]
  113. : py[0];
  114. if (result)
  115. {
  116. *result = a / b;
  117. }
  118. r = a % b;
  119. return;
  120. }
  121. //
  122. // prepare result:
  123. //
  124. if (result)
  125. result->resize(1 + r_order - y_order, 1 + r_order - y_order);
  126. typename CppInt1::const_limb_pointer prem = r.limbs();
  127. // This is initialised just to keep the compiler from emitting useless warnings later on:
  128. typename CppInt1::limb_pointer pr = typename CppInt1::limb_pointer();
  129. if (result)
  130. {
  131. pr = result->limbs();
  132. for (unsigned i = 1; i < 1 + r_order - y_order; ++i)
  133. pr[i] = 0;
  134. }
  135. bool first_pass = true;
  136. do
  137. {
  138. //
  139. // Calculate our best guess for how many times y divides into r:
  140. //
  141. limb_type guess = 1;
  142. if ((prem[r_order] <= py[y_order]) && (r_order > 0))
  143. {
  144. double_limb_type a = (static_cast<double_limb_type>(prem[r_order]) << CppInt1::limb_bits) | prem[r_order - 1];
  145. double_limb_type b = py[y_order];
  146. double_limb_type v = a / b;
  147. if (v <= CppInt1::max_limb_value)
  148. {
  149. guess = static_cast<limb_type>(v);
  150. --r_order;
  151. }
  152. }
  153. else if (r_order == 0)
  154. {
  155. guess = prem[0] / py[y_order];
  156. }
  157. else
  158. {
  159. double_limb_type a = (static_cast<double_limb_type>(prem[r_order]) << CppInt1::limb_bits) | prem[r_order - 1];
  160. double_limb_type b = (y_order > 0) ? (static_cast<double_limb_type>(py[y_order]) << CppInt1::limb_bits) | py[y_order - 1] : (static_cast<double_limb_type>(py[y_order]) << CppInt1::limb_bits);
  161. BOOST_ASSERT(b);
  162. double_limb_type v = a / b;
  163. guess = static_cast<limb_type>(v);
  164. }
  165. BOOST_ASSERT(guess); // If the guess ever gets to zero we go on forever....
  166. //
  167. // Update result:
  168. //
  169. limb_type shift = r_order - y_order;
  170. if (result)
  171. {
  172. if (r_neg)
  173. {
  174. if (pr[shift] > guess)
  175. pr[shift] -= guess;
  176. else
  177. {
  178. t.resize(shift + 1, shift + 1);
  179. t.limbs()[shift] = guess;
  180. for (unsigned i = 0; i < shift; ++i)
  181. t.limbs()[i] = 0;
  182. eval_subtract(*result, t);
  183. }
  184. }
  185. else if (CppInt1::max_limb_value - pr[shift] > guess)
  186. pr[shift] += guess;
  187. else
  188. {
  189. t.resize(shift + 1, shift + 1);
  190. t.limbs()[shift] = guess;
  191. for (unsigned i = 0; i < shift; ++i)
  192. t.limbs()[i] = 0;
  193. eval_add(*result, t);
  194. }
  195. }
  196. //
  197. // Calculate guess * y, we use a fused mutiply-shift O(N) for this
  198. // rather than a full O(N^2) multiply:
  199. //
  200. double_limb_type carry = 0;
  201. t.resize(y.size() + shift + 1, y.size() + shift);
  202. bool truncated_t = (t.size() != y.size() + shift + 1);
  203. typename CppInt1::limb_pointer pt = t.limbs();
  204. for (unsigned i = 0; i < shift; ++i)
  205. pt[i] = 0;
  206. for (unsigned i = 0; i < y.size(); ++i)
  207. {
  208. carry += static_cast<double_limb_type>(py[i]) * static_cast<double_limb_type>(guess);
  209. #ifdef __MSVC_RUNTIME_CHECKS
  210. pt[i + shift] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
  211. #else
  212. pt[i + shift] = static_cast<limb_type>(carry);
  213. #endif
  214. carry >>= CppInt1::limb_bits;
  215. }
  216. if (carry && !truncated_t)
  217. {
  218. #ifdef __MSVC_RUNTIME_CHECKS
  219. pt[t.size() - 1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
  220. #else
  221. pt[t.size() - 1] = static_cast<limb_type>(carry);
  222. #endif
  223. }
  224. else if (!truncated_t)
  225. {
  226. t.resize(t.size() - 1, t.size() - 1);
  227. }
  228. //
  229. // Update r in a way that won't actually produce a negative result
  230. // in case the argument types are unsigned:
  231. //
  232. if (truncated_t && carry)
  233. {
  234. // We need to calculate 2^n + t - r
  235. // where n is the number of bits in this type.
  236. // Simplest way is to get 2^n - r by complementing
  237. // r, then add t to it. Note that we can't call eval_complement
  238. // in case this is a signed checked type:
  239. for (unsigned i = 0; i <= r_order; ++i)
  240. r.limbs()[i] = ~prem[i];
  241. r.normalize();
  242. eval_increment(r);
  243. eval_add(r, t);
  244. r_neg = !r_neg;
  245. }
  246. else if (r.compare(t) > 0)
  247. {
  248. eval_subtract(r, t);
  249. }
  250. else
  251. {
  252. r.swap(t);
  253. eval_subtract(r, t);
  254. prem = r.limbs();
  255. r_neg = !r_neg;
  256. }
  257. //
  258. // First time through we need to strip any leading zero, otherwise
  259. // the termination condition goes belly-up:
  260. //
  261. if (result && first_pass)
  262. {
  263. first_pass = false;
  264. while (pr[result->size() - 1] == 0)
  265. result->resize(result->size() - 1, result->size() - 1);
  266. }
  267. //
  268. // Update r_order:
  269. //
  270. r_order = r.size() - 1;
  271. if (r_order < y_order)
  272. break;
  273. }
  274. // Termination condition is really just a check that r > y, but with a common
  275. // short-circuit case handled first:
  276. while ((r_order > y_order) || (r.compare_unsigned(y) >= 0));
  277. //
  278. // We now just have to normalise the result:
  279. //
  280. if (r_neg && eval_get_sign(r))
  281. {
  282. // We have one too many in the result:
  283. if (result)
  284. eval_decrement(*result);
  285. if (y.sign())
  286. {
  287. r.negate();
  288. eval_subtract(r, y);
  289. }
  290. else
  291. eval_subtract(r, y, r);
  292. }
  293. BOOST_ASSERT(r.compare_unsigned(y) < 0); // remainder must be less than the divisor or our code has failed
  294. }
  295. template <class CppInt1, class CppInt2>
  296. BOOST_MP_CXX14_CONSTEXPR void divide_unsigned_helper(
  297. CppInt1* result,
  298. const CppInt2& x,
  299. limb_type y,
  300. CppInt1& r)
  301. {
  302. if (((void*)result == (void*)&x) || ((void*)&r == (void*)&x))
  303. {
  304. CppInt2 t(x);
  305. divide_unsigned_helper(result, t, y, r);
  306. return;
  307. }
  308. if (result == &r)
  309. {
  310. CppInt1 rem;
  311. divide_unsigned_helper(result, x, y, rem);
  312. r = rem;
  313. return;
  314. }
  315. // As above, but simplified for integer divisor:
  316. using default_ops::eval_subtract;
  317. if (y == 0)
  318. {
  319. BOOST_THROW_EXCEPTION(std::overflow_error("Integer Division by zero."));
  320. }
  321. //
  322. // Find the most significant word of numerator.
  323. //
  324. limb_type r_order = x.size() - 1;
  325. //
  326. // Set remainder and result to their initial values:
  327. //
  328. r = x;
  329. r.sign(false);
  330. typename CppInt1::limb_pointer pr = r.limbs();
  331. //
  332. // check for x < y, try to do this without actually having to
  333. // do a full comparison:
  334. //
  335. if ((r_order == 0) && (*pr < y))
  336. {
  337. if (result)
  338. *result = static_cast<limb_type>(0u);
  339. return;
  340. }
  341. //
  342. // See if we can short-circuit long division, and use basic arithmetic instead:
  343. //
  344. if (r_order == 0)
  345. {
  346. if (result)
  347. {
  348. *result = *pr / y;
  349. result->sign(x.sign());
  350. }
  351. *pr %= y;
  352. r.sign(x.sign());
  353. return;
  354. }
  355. else if (r_order == 1)
  356. {
  357. double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[0];
  358. if (result)
  359. {
  360. *result = a / y;
  361. result->sign(x.sign());
  362. }
  363. r = a % y;
  364. r.sign(x.sign());
  365. return;
  366. }
  367. // This is initialised just to keep the compiler from emitting useless warnings later on:
  368. typename CppInt1::limb_pointer pres = typename CppInt1::limb_pointer();
  369. if (result)
  370. {
  371. result->resize(r_order + 1, r_order + 1);
  372. pres = result->limbs();
  373. if (result->size() > r_order)
  374. pres[r_order] = 0; // just in case we don't set the most significant limb below.
  375. }
  376. do
  377. {
  378. //
  379. // Calculate our best guess for how many times y divides into r:
  380. //
  381. if ((pr[r_order] < y) && r_order)
  382. {
  383. double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[r_order - 1];
  384. double_limb_type b = a % y;
  385. r.resize(r.size() - 1, r.size() - 1);
  386. --r_order;
  387. pr[r_order] = static_cast<limb_type>(b);
  388. if (result)
  389. pres[r_order] = static_cast<limb_type>(a / y);
  390. if (r_order && pr[r_order] == 0)
  391. {
  392. --r_order; // No remainder, division was exact.
  393. r.resize(r.size() - 1, r.size() - 1);
  394. if (result)
  395. pres[r_order] = static_cast<limb_type>(0u);
  396. }
  397. }
  398. else
  399. {
  400. if (result)
  401. pres[r_order] = pr[r_order] / y;
  402. pr[r_order] %= y;
  403. if (r_order && pr[r_order] == 0)
  404. {
  405. --r_order; // No remainder, division was exact.
  406. r.resize(r.size() - 1, r.size() - 1);
  407. if (result)
  408. pres[r_order] = static_cast<limb_type>(0u);
  409. }
  410. }
  411. }
  412. // Termination condition is really just a check that r >= y, but with two common
  413. // short-circuit cases handled first:
  414. while (r_order || (pr[r_order] >= y));
  415. if (result)
  416. {
  417. result->normalize();
  418. result->sign(x.sign());
  419. }
  420. r.normalize();
  421. r.sign(x.sign());
  422. BOOST_ASSERT(r.compare(y) < 0); // remainder must be less than the divisor or our code has failed
  423. }
  424. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, unsigned MinBits3, unsigned MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
  425. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
  426. eval_divide(
  427. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  428. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  429. const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
  430. {
  431. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  432. bool s = a.sign() != b.sign();
  433. divide_unsigned_helper(&result, a, b, r);
  434. result.sign(s);
  435. }
  436. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  437. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  438. eval_divide(
  439. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  440. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  441. limb_type& b)
  442. {
  443. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  444. bool s = a.sign();
  445. divide_unsigned_helper(&result, a, b, r);
  446. result.sign(s);
  447. }
  448. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  449. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  450. eval_divide(
  451. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  452. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  453. signed_limb_type& b)
  454. {
  455. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  456. bool s = a.sign() != (b < 0);
  457. divide_unsigned_helper(&result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(b)), r);
  458. result.sign(s);
  459. }
  460. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  461. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  462. eval_divide(
  463. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  464. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
  465. {
  466. // There is no in place divide:
  467. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  468. eval_divide(result, a, b);
  469. }
  470. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  471. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  472. eval_divide(
  473. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  474. limb_type b)
  475. {
  476. // There is no in place divide:
  477. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  478. eval_divide(result, a, b);
  479. }
  480. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  481. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  482. eval_divide(
  483. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  484. signed_limb_type b)
  485. {
  486. // There is no in place divide:
  487. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  488. eval_divide(result, a, b);
  489. }
  490. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2, unsigned MinBits3, unsigned MaxBits3, cpp_integer_type SignType3, cpp_int_check_type Checked3, class Allocator3>
  491. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3> >::value>::type
  492. eval_modulus(
  493. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  494. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  495. const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
  496. {
  497. bool s = a.sign();
  498. if (b.size() == 1)
  499. eval_modulus(result, a, *b.limbs());
  500. else
  501. divide_unsigned_helper(static_cast<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>*>(0), a, b, result);
  502. result.sign(s);
  503. }
  504. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  505. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  506. eval_modulus(
  507. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  508. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  509. const limb_type mod)
  510. {
  511. const int n = a.size();
  512. const double_limb_type two_n_mod = static_cast<limb_type>(1u) + (~static_cast<limb_type>(0u) - mod) % mod;
  513. limb_type res = a.limbs()[n - 1] % mod;
  514. for (int i = n - 2; i >= 0; --i)
  515. res = (res * two_n_mod + a.limbs()[i]) % mod;
  516. //
  517. // We must not modify result until here in case
  518. // result and a are the same object:
  519. //
  520. result.resize(1, 1);
  521. *result.limbs() = res;
  522. result.sign(a.sign());
  523. }
  524. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  525. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  526. eval_modulus(
  527. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  528. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  529. signed_limb_type b)
  530. {
  531. const limb_type t = b < 0 ? -b : b;
  532. eval_modulus(result, a, t);
  533. result.sign(a.sign());
  534. }
  535. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1, unsigned MinBits2, unsigned MaxBits2, cpp_integer_type SignType2, cpp_int_check_type Checked2, class Allocator2>
  536. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && !is_trivial_cpp_int<cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2> >::value>::type
  537. eval_modulus(
  538. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  539. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
  540. {
  541. // There is no in place divide:
  542. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  543. eval_modulus(result, a, b);
  544. }
  545. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  546. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  547. eval_modulus(
  548. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  549. limb_type b)
  550. {
  551. // Single limb modulus is in place:
  552. eval_modulus(result, result, b);
  553. }
  554. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  555. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  556. eval_modulus(
  557. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  558. signed_limb_type b)
  559. {
  560. // Single limb modulus is in place:
  561. eval_modulus(result, result, b);
  562. }
  563. //
  564. // Over again for trivial cpp_int's:
  565. //
  566. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  567. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  568. is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && (is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value || is_signed_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value)>::type
  569. eval_divide(
  570. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  571. const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
  572. {
  573. if (!*o.limbs())
  574. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  575. *result.limbs() /= *o.limbs();
  576. result.sign(result.sign() != o.sign());
  577. }
  578. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  579. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  580. is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  581. eval_divide(
  582. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  583. const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
  584. {
  585. if (!*o.limbs())
  586. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  587. *result.limbs() /= *o.limbs();
  588. }
  589. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  590. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  591. is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  592. eval_modulus(
  593. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  594. const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
  595. {
  596. if (!*o.limbs())
  597. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  598. *result.limbs() %= *o.limbs();
  599. result.sign(result.sign());
  600. }
  601. }}} // namespace boost::multiprecision::backends
  602. #endif