divide.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. double_limb_type v = a / b;
  162. guess = static_cast<limb_type>(v);
  163. }
  164. BOOST_ASSERT(guess); // If the guess ever gets to zero we go on forever....
  165. //
  166. // Update result:
  167. //
  168. limb_type shift = r_order - y_order;
  169. if (result)
  170. {
  171. if (r_neg)
  172. {
  173. if (pr[shift] > guess)
  174. pr[shift] -= guess;
  175. else
  176. {
  177. t.resize(shift + 1, shift + 1);
  178. t.limbs()[shift] = guess;
  179. for (unsigned i = 0; i < shift; ++i)
  180. t.limbs()[i] = 0;
  181. eval_subtract(*result, t);
  182. }
  183. }
  184. else if (CppInt1::max_limb_value - pr[shift] > guess)
  185. pr[shift] += guess;
  186. else
  187. {
  188. t.resize(shift + 1, shift + 1);
  189. t.limbs()[shift] = guess;
  190. for (unsigned i = 0; i < shift; ++i)
  191. t.limbs()[i] = 0;
  192. eval_add(*result, t);
  193. }
  194. }
  195. //
  196. // Calculate guess * y, we use a fused mutiply-shift O(N) for this
  197. // rather than a full O(N^2) multiply:
  198. //
  199. double_limb_type carry = 0;
  200. t.resize(y.size() + shift + 1, y.size() + shift);
  201. bool truncated_t = (t.size() != y.size() + shift + 1);
  202. typename CppInt1::limb_pointer pt = t.limbs();
  203. for (unsigned i = 0; i < shift; ++i)
  204. pt[i] = 0;
  205. for (unsigned i = 0; i < y.size(); ++i)
  206. {
  207. carry += static_cast<double_limb_type>(py[i]) * static_cast<double_limb_type>(guess);
  208. #ifdef __MSVC_RUNTIME_CHECKS
  209. pt[i + shift] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
  210. #else
  211. pt[i + shift] = static_cast<limb_type>(carry);
  212. #endif
  213. carry >>= CppInt1::limb_bits;
  214. }
  215. if (carry && !truncated_t)
  216. {
  217. #ifdef __MSVC_RUNTIME_CHECKS
  218. pt[t.size() - 1] = static_cast<limb_type>(carry & ~static_cast<limb_type>(0));
  219. #else
  220. pt[t.size() - 1] = static_cast<limb_type>(carry);
  221. #endif
  222. }
  223. else if (!truncated_t)
  224. {
  225. t.resize(t.size() - 1, t.size() - 1);
  226. }
  227. //
  228. // Update r in a way that won't actually produce a negative result
  229. // in case the argument types are unsigned:
  230. //
  231. if (truncated_t && carry)
  232. {
  233. // We need to calculate 2^n + t - r
  234. // where n is the number of bits in this type.
  235. // Simplest way is to get 2^n - r by complementing
  236. // r, then add t to it. Note that we can't call eval_complement
  237. // in case this is a signed checked type:
  238. for (unsigned i = 0; i <= r_order; ++i)
  239. r.limbs()[i] = ~prem[i];
  240. r.normalize();
  241. eval_increment(r);
  242. eval_add(r, t);
  243. r_neg = !r_neg;
  244. }
  245. else if (r.compare(t) > 0)
  246. {
  247. eval_subtract(r, t);
  248. }
  249. else
  250. {
  251. r.swap(t);
  252. eval_subtract(r, t);
  253. prem = r.limbs();
  254. r_neg = !r_neg;
  255. }
  256. //
  257. // First time through we need to strip any leading zero, otherwise
  258. // the termination condition goes belly-up:
  259. //
  260. if (result && first_pass)
  261. {
  262. first_pass = false;
  263. while (pr[result->size() - 1] == 0)
  264. result->resize(result->size() - 1, result->size() - 1);
  265. }
  266. //
  267. // Update r_order:
  268. //
  269. r_order = r.size() - 1;
  270. if (r_order < y_order)
  271. break;
  272. }
  273. // Termination condition is really just a check that r > y, but with a common
  274. // short-circuit case handled first:
  275. while ((r_order > y_order) || (r.compare_unsigned(y) >= 0));
  276. //
  277. // We now just have to normalise the result:
  278. //
  279. if (r_neg && eval_get_sign(r))
  280. {
  281. // We have one too many in the result:
  282. if (result)
  283. eval_decrement(*result);
  284. if (y.sign())
  285. {
  286. r.negate();
  287. eval_subtract(r, y);
  288. }
  289. else
  290. eval_subtract(r, y, r);
  291. }
  292. BOOST_ASSERT(r.compare_unsigned(y) < 0); // remainder must be less than the divisor or our code has failed
  293. }
  294. template <class CppInt1, class CppInt2>
  295. BOOST_MP_CXX14_CONSTEXPR void divide_unsigned_helper(
  296. CppInt1* result,
  297. const CppInt2& x,
  298. limb_type y,
  299. CppInt1& r)
  300. {
  301. if (((void*)result == (void*)&x) || ((void*)&r == (void*)&x))
  302. {
  303. CppInt2 t(x);
  304. divide_unsigned_helper(result, t, y, r);
  305. return;
  306. }
  307. if (result == &r)
  308. {
  309. CppInt1 rem;
  310. divide_unsigned_helper(result, x, y, rem);
  311. r = rem;
  312. return;
  313. }
  314. // As above, but simplified for integer divisor:
  315. using default_ops::eval_subtract;
  316. if (y == 0)
  317. {
  318. BOOST_THROW_EXCEPTION(std::overflow_error("Integer Division by zero."));
  319. }
  320. //
  321. // Find the most significant word of numerator.
  322. //
  323. limb_type r_order = x.size() - 1;
  324. //
  325. // Set remainder and result to their initial values:
  326. //
  327. r = x;
  328. r.sign(false);
  329. typename CppInt1::limb_pointer pr = r.limbs();
  330. //
  331. // check for x < y, try to do this without actually having to
  332. // do a full comparison:
  333. //
  334. if ((r_order == 0) && (*pr < y))
  335. {
  336. if (result)
  337. *result = static_cast<limb_type>(0u);
  338. return;
  339. }
  340. //
  341. // See if we can short-circuit long division, and use basic arithmetic instead:
  342. //
  343. if (r_order == 0)
  344. {
  345. if (result)
  346. {
  347. *result = *pr / y;
  348. result->sign(x.sign());
  349. }
  350. *pr %= y;
  351. r.sign(x.sign());
  352. return;
  353. }
  354. else if (r_order == 1)
  355. {
  356. double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[0];
  357. if (result)
  358. {
  359. *result = a / y;
  360. result->sign(x.sign());
  361. }
  362. r = a % y;
  363. r.sign(x.sign());
  364. return;
  365. }
  366. // This is initialised just to keep the compiler from emitting useless warnings later on:
  367. typename CppInt1::limb_pointer pres = typename CppInt1::limb_pointer();
  368. if (result)
  369. {
  370. result->resize(r_order + 1, r_order + 1);
  371. pres = result->limbs();
  372. if (result->size() > r_order)
  373. pres[r_order] = 0; // just in case we don't set the most significant limb below.
  374. }
  375. do
  376. {
  377. //
  378. // Calculate our best guess for how many times y divides into r:
  379. //
  380. if ((pr[r_order] < y) && r_order)
  381. {
  382. double_limb_type a = (static_cast<double_limb_type>(pr[r_order]) << CppInt1::limb_bits) | pr[r_order - 1];
  383. double_limb_type b = a % y;
  384. r.resize(r.size() - 1, r.size() - 1);
  385. --r_order;
  386. pr[r_order] = static_cast<limb_type>(b);
  387. if (result)
  388. pres[r_order] = static_cast<limb_type>(a / y);
  389. if (r_order && pr[r_order] == 0)
  390. {
  391. --r_order; // No remainder, division was exact.
  392. r.resize(r.size() - 1, r.size() - 1);
  393. if (result)
  394. pres[r_order] = static_cast<limb_type>(0u);
  395. }
  396. }
  397. else
  398. {
  399. if (result)
  400. pres[r_order] = pr[r_order] / y;
  401. pr[r_order] %= y;
  402. if (r_order && pr[r_order] == 0)
  403. {
  404. --r_order; // No remainder, division was exact.
  405. r.resize(r.size() - 1, r.size() - 1);
  406. if (result)
  407. pres[r_order] = static_cast<limb_type>(0u);
  408. }
  409. }
  410. }
  411. // Termination condition is really just a check that r >= y, but with two common
  412. // short-circuit cases handled first:
  413. while (r_order || (pr[r_order] >= y));
  414. if (result)
  415. {
  416. result->normalize();
  417. result->sign(x.sign());
  418. }
  419. r.normalize();
  420. r.sign(x.sign());
  421. BOOST_ASSERT(r.compare(y) < 0); // remainder must be less than the divisor or our code has failed
  422. }
  423. 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>
  424. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  425. eval_divide(
  426. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  427. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  428. const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
  429. {
  430. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  431. bool s = a.sign() != b.sign();
  432. divide_unsigned_helper(&result, a, b, r);
  433. result.sign(s);
  434. }
  435. 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>
  436. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  437. eval_divide(
  438. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  439. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  440. limb_type& b)
  441. {
  442. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  443. bool s = a.sign();
  444. divide_unsigned_helper(&result, a, b, r);
  445. result.sign(s);
  446. }
  447. 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>
  448. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  449. eval_divide(
  450. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  451. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  452. signed_limb_type& b)
  453. {
  454. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> r;
  455. bool s = a.sign() != (b < 0);
  456. divide_unsigned_helper(&result, a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(b)), r);
  457. result.sign(s);
  458. }
  459. 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>
  460. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  461. eval_divide(
  462. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  463. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
  464. {
  465. // There is no in place divide:
  466. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  467. eval_divide(result, a, b);
  468. }
  469. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  470. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  471. eval_divide(
  472. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  473. limb_type b)
  474. {
  475. // There is no in place divide:
  476. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  477. eval_divide(result, a, b);
  478. }
  479. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  480. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  481. eval_divide(
  482. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  483. signed_limb_type b)
  484. {
  485. // There is no in place divide:
  486. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  487. eval_divide(result, a, b);
  488. }
  489. 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>
  490. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  491. eval_modulus(
  492. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  493. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  494. const cpp_int_backend<MinBits3, MaxBits3, SignType3, Checked3, Allocator3>& b)
  495. {
  496. bool s = a.sign();
  497. divide_unsigned_helper(static_cast<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>*>(0), a, b, result);
  498. result.sign(s);
  499. }
  500. 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>
  501. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  502. eval_modulus(
  503. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  504. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a, limb_type b)
  505. {
  506. bool s = a.sign();
  507. divide_unsigned_helper(static_cast<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>*>(0), a, b, result);
  508. result.sign(s);
  509. }
  510. 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>
  511. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  512. eval_modulus(
  513. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  514. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& a,
  515. signed_limb_type b)
  516. {
  517. bool s = a.sign();
  518. divide_unsigned_helper(static_cast<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>*>(0), a, static_cast<limb_type>(boost::multiprecision::detail::unsigned_abs(b)), result);
  519. result.sign(s);
  520. }
  521. 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>
  522. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!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
  523. eval_modulus(
  524. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  525. const cpp_int_backend<MinBits2, MaxBits2, SignType2, Checked2, Allocator2>& b)
  526. {
  527. // There is no in place divide:
  528. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  529. eval_modulus(result, a, b);
  530. }
  531. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  532. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  533. eval_modulus(
  534. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  535. limb_type b)
  536. {
  537. // There is no in place divide:
  538. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  539. eval_modulus(result, a, b);
  540. }
  541. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  542. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<!is_trivial_cpp_int<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value>::type
  543. eval_modulus(
  544. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  545. signed_limb_type b)
  546. {
  547. // There is no in place divide:
  548. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> a(result);
  549. eval_modulus(result, a, b);
  550. }
  551. //
  552. // Over again for trivial cpp_int's:
  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 enable_if_c<
  556. 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
  557. eval_divide(
  558. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  559. const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
  560. {
  561. if (!*o.limbs())
  562. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  563. *result.limbs() /= *o.limbs();
  564. result.sign(result.sign() != o.sign());
  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 enable_if_c<
  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_unsigned_number<cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1> >::value && is_unsigned_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. }
  577. template <unsigned MinBits1, unsigned MaxBits1, cpp_integer_type SignType1, cpp_int_check_type Checked1, class Allocator1>
  578. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<
  579. 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
  580. eval_modulus(
  581. cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& result,
  582. const cpp_int_backend<MinBits1, MaxBits1, SignType1, Checked1, Allocator1>& o)
  583. {
  584. if (!*o.limbs())
  585. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  586. *result.limbs() %= *o.limbs();
  587. result.sign(result.sign());
  588. }
  589. }}} // namespace boost::multiprecision::backends
  590. #endif