basearith.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright (c) 2008-2016 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "constants.h"
  33. #include "memory.h"
  34. #include "typearith.h"
  35. #include "basearith.h"
  36. /*********************************************************************/
  37. /* Calculations in base MPD_RADIX */
  38. /*********************************************************************/
  39. /*
  40. * Knuth, TAOCP, Volume 2, 4.3.1:
  41. * w := sum of u (len m) and v (len n)
  42. * n > 0 and m >= n
  43. * The calling function has to handle a possible final carry.
  44. */
  45. mpd_uint_t
  46. _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  47. mpd_size_t m, mpd_size_t n)
  48. {
  49. mpd_uint_t s;
  50. mpd_uint_t carry = 0;
  51. mpd_size_t i;
  52. assert(n > 0 && m >= n);
  53. /* add n members of u and v */
  54. for (i = 0; i < n; i++) {
  55. s = u[i] + (v[i] + carry);
  56. carry = (s < u[i]) | (s >= MPD_RADIX);
  57. w[i] = carry ? s-MPD_RADIX : s;
  58. }
  59. /* if there is a carry, propagate it */
  60. for (; carry && i < m; i++) {
  61. s = u[i] + carry;
  62. carry = (s == MPD_RADIX);
  63. w[i] = carry ? 0 : s;
  64. }
  65. /* copy the rest of u */
  66. for (; i < m; i++) {
  67. w[i] = u[i];
  68. }
  69. return carry;
  70. }
  71. /*
  72. * Add the contents of u to w. Carries are propagated further. The caller
  73. * has to make sure that w is big enough.
  74. */
  75. void
  76. _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n)
  77. {
  78. mpd_uint_t s;
  79. mpd_uint_t carry = 0;
  80. mpd_size_t i;
  81. if (n == 0) return;
  82. /* add n members of u to w */
  83. for (i = 0; i < n; i++) {
  84. s = w[i] + (u[i] + carry);
  85. carry = (s < w[i]) | (s >= MPD_RADIX);
  86. w[i] = carry ? s-MPD_RADIX : s;
  87. }
  88. /* if there is a carry, propagate it */
  89. for (; carry; i++) {
  90. s = w[i] + carry;
  91. carry = (s == MPD_RADIX);
  92. w[i] = carry ? 0 : s;
  93. }
  94. }
  95. /*
  96. * Add v to w (len m). The calling function has to handle a possible
  97. * final carry. Assumption: m > 0.
  98. */
  99. mpd_uint_t
  100. _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v)
  101. {
  102. mpd_uint_t s;
  103. mpd_uint_t carry;
  104. mpd_size_t i;
  105. assert(m > 0);
  106. /* add v to w */
  107. s = w[0] + v;
  108. carry = (s < v) | (s >= MPD_RADIX);
  109. w[0] = carry ? s-MPD_RADIX : s;
  110. /* if there is a carry, propagate it */
  111. for (i = 1; carry && i < m; i++) {
  112. s = w[i] + carry;
  113. carry = (s == MPD_RADIX);
  114. w[i] = carry ? 0 : s;
  115. }
  116. return carry;
  117. }
  118. /* Increment u. The calling function has to handle a possible carry. */
  119. mpd_uint_t
  120. _mpd_baseincr(mpd_uint_t *u, mpd_size_t n)
  121. {
  122. mpd_uint_t s;
  123. mpd_uint_t carry = 1;
  124. mpd_size_t i;
  125. assert(n > 0);
  126. /* if there is a carry, propagate it */
  127. for (i = 0; carry && i < n; i++) {
  128. s = u[i] + carry;
  129. carry = (s == MPD_RADIX);
  130. u[i] = carry ? 0 : s;
  131. }
  132. return carry;
  133. }
  134. /*
  135. * Knuth, TAOCP, Volume 2, 4.3.1:
  136. * w := difference of u (len m) and v (len n).
  137. * number in u >= number in v;
  138. */
  139. void
  140. _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  141. mpd_size_t m, mpd_size_t n)
  142. {
  143. mpd_uint_t d;
  144. mpd_uint_t borrow = 0;
  145. mpd_size_t i;
  146. assert(m > 0 && n > 0);
  147. /* subtract n members of v from u */
  148. for (i = 0; i < n; i++) {
  149. d = u[i] - (v[i] + borrow);
  150. borrow = (u[i] < d);
  151. w[i] = borrow ? d + MPD_RADIX : d;
  152. }
  153. /* if there is a borrow, propagate it */
  154. for (; borrow && i < m; i++) {
  155. d = u[i] - borrow;
  156. borrow = (u[i] == 0);
  157. w[i] = borrow ? MPD_RADIX-1 : d;
  158. }
  159. /* copy the rest of u */
  160. for (; i < m; i++) {
  161. w[i] = u[i];
  162. }
  163. }
  164. /*
  165. * Subtract the contents of u from w. w is larger than u. Borrows are
  166. * propagated further, but eventually w can absorb the final borrow.
  167. */
  168. void
  169. _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n)
  170. {
  171. mpd_uint_t d;
  172. mpd_uint_t borrow = 0;
  173. mpd_size_t i;
  174. if (n == 0) return;
  175. /* subtract n members of u from w */
  176. for (i = 0; i < n; i++) {
  177. d = w[i] - (u[i] + borrow);
  178. borrow = (w[i] < d);
  179. w[i] = borrow ? d + MPD_RADIX : d;
  180. }
  181. /* if there is a borrow, propagate it */
  182. for (; borrow; i++) {
  183. d = w[i] - borrow;
  184. borrow = (w[i] == 0);
  185. w[i] = borrow ? MPD_RADIX-1 : d;
  186. }
  187. }
  188. /* w := product of u (len n) and v (single word) */
  189. void
  190. _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v)
  191. {
  192. mpd_uint_t hi, lo;
  193. mpd_uint_t carry = 0;
  194. mpd_size_t i;
  195. assert(n > 0);
  196. for (i=0; i < n; i++) {
  197. _mpd_mul_words(&hi, &lo, u[i], v);
  198. lo = carry + lo;
  199. if (lo < carry) hi++;
  200. _mpd_div_words_r(&carry, &w[i], hi, lo);
  201. }
  202. w[i] = carry;
  203. }
  204. /*
  205. * Knuth, TAOCP, Volume 2, 4.3.1:
  206. * w := product of u (len m) and v (len n)
  207. * w must be initialized to zero
  208. */
  209. void
  210. _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,
  211. mpd_size_t m, mpd_size_t n)
  212. {
  213. mpd_uint_t hi, lo;
  214. mpd_uint_t carry;
  215. mpd_size_t i, j;
  216. assert(m > 0 && n > 0);
  217. for (j=0; j < n; j++) {
  218. carry = 0;
  219. for (i=0; i < m; i++) {
  220. _mpd_mul_words(&hi, &lo, u[i], v[j]);
  221. lo = w[i+j] + lo;
  222. if (lo < w[i+j]) hi++;
  223. lo = carry + lo;
  224. if (lo < carry) hi++;
  225. _mpd_div_words_r(&carry, &w[i+j], hi, lo);
  226. }
  227. w[j+m] = carry;
  228. }
  229. }
  230. /*
  231. * Knuth, TAOCP Volume 2, 4.3.1, exercise 16:
  232. * w := quotient of u (len n) divided by a single word v
  233. */
  234. mpd_uint_t
  235. _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v)
  236. {
  237. mpd_uint_t hi, lo;
  238. mpd_uint_t rem = 0;
  239. mpd_size_t i;
  240. assert(n > 0);
  241. for (i=n-1; i != MPD_SIZE_MAX; i--) {
  242. _mpd_mul_words(&hi, &lo, rem, MPD_RADIX);
  243. lo = u[i] + lo;
  244. if (lo < u[i]) hi++;
  245. _mpd_div_words(&w[i], &rem, hi, lo, v);
  246. }
  247. return rem;
  248. }
  249. /*
  250. * Knuth, TAOCP Volume 2, 4.3.1:
  251. * q, r := quotient and remainder of uconst (len nplusm)
  252. * divided by vconst (len n)
  253. * nplusm >= n
  254. *
  255. * If r is not NULL, r will contain the remainder. If r is NULL, the
  256. * return value indicates if there is a remainder: 1 for true, 0 for
  257. * false. A return value of -1 indicates an error.
  258. */
  259. int
  260. _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r,
  261. const mpd_uint_t *uconst, const mpd_uint_t *vconst,
  262. mpd_size_t nplusm, mpd_size_t n)
  263. {
  264. mpd_uint_t ustatic[MPD_MINALLOC_MAX];
  265. mpd_uint_t vstatic[MPD_MINALLOC_MAX];
  266. mpd_uint_t *u = ustatic;
  267. mpd_uint_t *v = vstatic;
  268. mpd_uint_t d, qhat, rhat, w2[2];
  269. mpd_uint_t hi, lo, x;
  270. mpd_uint_t carry;
  271. mpd_size_t i, j, m;
  272. int retval = 0;
  273. assert(n > 1 && nplusm >= n);
  274. m = sub_size_t(nplusm, n);
  275. /* D1: normalize */
  276. d = MPD_RADIX / (vconst[n-1] + 1);
  277. if (nplusm >= MPD_MINALLOC_MAX) {
  278. if ((u = mpd_alloc(nplusm+1, sizeof *u)) == NULL) {
  279. return -1;
  280. }
  281. }
  282. if (n >= MPD_MINALLOC_MAX) {
  283. if ((v = mpd_alloc(n+1, sizeof *v)) == NULL) {
  284. mpd_free(u);
  285. return -1;
  286. }
  287. }
  288. _mpd_shortmul(u, uconst, nplusm, d);
  289. _mpd_shortmul(v, vconst, n, d);
  290. /* D2: loop */
  291. for (j=m; j != MPD_SIZE_MAX; j--) {
  292. /* D3: calculate qhat and rhat */
  293. rhat = _mpd_shortdiv(w2, u+j+n-1, 2, v[n-1]);
  294. qhat = w2[1] * MPD_RADIX + w2[0];
  295. while (1) {
  296. if (qhat < MPD_RADIX) {
  297. _mpd_singlemul(w2, qhat, v[n-2]);
  298. if (w2[1] <= rhat) {
  299. if (w2[1] != rhat || w2[0] <= u[j+n-2]) {
  300. break;
  301. }
  302. }
  303. }
  304. qhat -= 1;
  305. rhat += v[n-1];
  306. if (rhat < v[n-1] || rhat >= MPD_RADIX) {
  307. break;
  308. }
  309. }
  310. /* D4: multiply and subtract */
  311. carry = 0;
  312. for (i=0; i <= n; i++) {
  313. _mpd_mul_words(&hi, &lo, qhat, v[i]);
  314. lo = carry + lo;
  315. if (lo < carry) hi++;
  316. _mpd_div_words_r(&hi, &lo, hi, lo);
  317. x = u[i+j] - lo;
  318. carry = (u[i+j] < x);
  319. u[i+j] = carry ? x+MPD_RADIX : x;
  320. carry += hi;
  321. }
  322. q[j] = qhat;
  323. /* D5: test remainder */
  324. if (carry) {
  325. q[j] -= 1;
  326. /* D6: add back */
  327. (void)_mpd_baseadd(u+j, u+j, v, n+1, n);
  328. }
  329. }
  330. /* D8: unnormalize */
  331. if (r != NULL) {
  332. _mpd_shortdiv(r, u, n, d);
  333. /* we are not interested in the return value here */
  334. retval = 0;
  335. }
  336. else {
  337. retval = !_mpd_isallzero(u, n);
  338. }
  339. if (u != ustatic) mpd_free(u);
  340. if (v != vstatic) mpd_free(v);
  341. return retval;
  342. }
  343. /*
  344. * Left shift of src by 'shift' digits; src may equal dest.
  345. *
  346. * dest := area of n mpd_uint_t with space for srcdigits+shift digits.
  347. * src := coefficient with length m.
  348. *
  349. * The case splits in the function are non-obvious. The following
  350. * equations might help:
  351. *
  352. * Let msdigits denote the number of digits in the most significant
  353. * word of src. Then 1 <= msdigits <= rdigits.
  354. *
  355. * 1) shift = q * rdigits + r
  356. * 2) srcdigits = qsrc * rdigits + msdigits
  357. * 3) destdigits = shift + srcdigits
  358. * = q * rdigits + r + qsrc * rdigits + msdigits
  359. * = q * rdigits + (qsrc * rdigits + (r + msdigits))
  360. *
  361. * The result has q zero words, followed by the coefficient that
  362. * is left-shifted by r. The case r == 0 is trivial. For r > 0, it
  363. * is important to keep in mind that we always read m source words,
  364. * but write m+1 destination words if r + msdigits > rdigits, m words
  365. * otherwise.
  366. */
  367. void
  368. _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n, mpd_size_t m,
  369. mpd_size_t shift)
  370. {
  371. #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  372. /* spurious uninitialized warnings */
  373. mpd_uint_t l=l, lprev=lprev, h=h;
  374. #else
  375. mpd_uint_t l, lprev, h;
  376. #endif
  377. mpd_uint_t q, r;
  378. mpd_uint_t ph;
  379. assert(m > 0 && n >= m);
  380. _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS);
  381. if (r != 0) {
  382. ph = mpd_pow10[r];
  383. --m; --n;
  384. _mpd_divmod_pow10(&h, &lprev, src[m--], MPD_RDIGITS-r);
  385. if (h != 0) { /* r + msdigits > rdigits <==> h != 0 */
  386. dest[n--] = h;
  387. }
  388. /* write m-1 shifted words */
  389. for (; m != MPD_SIZE_MAX; m--,n--) {
  390. _mpd_divmod_pow10(&h, &l, src[m], MPD_RDIGITS-r);
  391. dest[n] = ph * lprev + h;
  392. lprev = l;
  393. }
  394. /* write least significant word */
  395. dest[q] = ph * lprev;
  396. }
  397. else {
  398. while (--m != MPD_SIZE_MAX) {
  399. dest[m+q] = src[m];
  400. }
  401. }
  402. mpd_uint_zero(dest, q);
  403. }
  404. /*
  405. * Right shift of src by 'shift' digits; src may equal dest.
  406. * Assumption: srcdigits-shift > 0.
  407. *
  408. * dest := area with space for srcdigits-shift digits.
  409. * src := coefficient with length 'slen'.
  410. *
  411. * The case splits in the function rely on the following equations:
  412. *
  413. * Let msdigits denote the number of digits in the most significant
  414. * word of src. Then 1 <= msdigits <= rdigits.
  415. *
  416. * 1) shift = q * rdigits + r
  417. * 2) srcdigits = qsrc * rdigits + msdigits
  418. * 3) destdigits = srcdigits - shift
  419. * = qsrc * rdigits + msdigits - (q * rdigits + r)
  420. * = (qsrc - q) * rdigits + msdigits - r
  421. *
  422. * Since destdigits > 0 and 1 <= msdigits <= rdigits:
  423. *
  424. * 4) qsrc >= q
  425. * 5) qsrc == q ==> msdigits > r
  426. *
  427. * The result has slen-q words if msdigits > r, slen-q-1 words otherwise.
  428. */
  429. mpd_uint_t
  430. _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen,
  431. mpd_size_t shift)
  432. {
  433. #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  434. /* spurious uninitialized warnings */
  435. mpd_uint_t l=l, h=h, hprev=hprev; /* low, high, previous high */
  436. #else
  437. mpd_uint_t l, h, hprev; /* low, high, previous high */
  438. #endif
  439. mpd_uint_t rnd, rest; /* rounding digit, rest */
  440. mpd_uint_t q, r;
  441. mpd_size_t i, j;
  442. mpd_uint_t ph;
  443. assert(slen > 0);
  444. _mpd_div_word(&q, &r, (mpd_uint_t)shift, MPD_RDIGITS);
  445. rnd = rest = 0;
  446. if (r != 0) {
  447. ph = mpd_pow10[MPD_RDIGITS-r];
  448. _mpd_divmod_pow10(&hprev, &rest, src[q], r);
  449. _mpd_divmod_pow10(&rnd, &rest, rest, r-1);
  450. if (rest == 0 && q > 0) {
  451. rest = !_mpd_isallzero(src, q);
  452. }
  453. /* write slen-q-1 words */
  454. for (j=0,i=q+1; i<slen; i++,j++) {
  455. _mpd_divmod_pow10(&h, &l, src[i], r);
  456. dest[j] = ph * l + hprev;
  457. hprev = h;
  458. }
  459. /* write most significant word */
  460. if (hprev != 0) { /* always the case if slen==q-1 */
  461. dest[j] = hprev;
  462. }
  463. }
  464. else {
  465. if (q > 0) {
  466. _mpd_divmod_pow10(&rnd, &rest, src[q-1], MPD_RDIGITS-1);
  467. /* is there any non-zero digit below rnd? */
  468. if (rest == 0) rest = !_mpd_isallzero(src, q-1);
  469. }
  470. for (j = 0; j < slen-q; j++) {
  471. dest[j] = src[q+j];
  472. }
  473. }
  474. /* 0-4 ==> rnd+rest < 0.5 */
  475. /* 5 ==> rnd+rest == 0.5 */
  476. /* 6-9 ==> rnd+rest > 0.5 */
  477. return (rnd == 0 || rnd == 5) ? rnd + !!rest : rnd;
  478. }
  479. /*********************************************************************/
  480. /* Calculations in base b */
  481. /*********************************************************************/
  482. /*
  483. * Add v to w (len m). The calling function has to handle a possible
  484. * final carry. Assumption: m > 0.
  485. */
  486. mpd_uint_t
  487. _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v, mpd_uint_t b)
  488. {
  489. mpd_uint_t s;
  490. mpd_uint_t carry;
  491. mpd_size_t i;
  492. assert(m > 0);
  493. /* add v to w */
  494. s = w[0] + v;
  495. carry = (s < v) | (s >= b);
  496. w[0] = carry ? s-b : s;
  497. /* if there is a carry, propagate it */
  498. for (i = 1; carry && i < m; i++) {
  499. s = w[i] + carry;
  500. carry = (s == b);
  501. w[i] = carry ? 0 : s;
  502. }
  503. return carry;
  504. }
  505. /* w := product of u (len n) and v (single word). Return carry. */
  506. mpd_uint_t
  507. _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n, mpd_uint_t v)
  508. {
  509. mpd_uint_t hi, lo;
  510. mpd_uint_t carry = 0;
  511. mpd_size_t i;
  512. assert(n > 0);
  513. for (i=0; i < n; i++) {
  514. _mpd_mul_words(&hi, &lo, u[i], v);
  515. lo = carry + lo;
  516. if (lo < carry) hi++;
  517. _mpd_div_words_r(&carry, &w[i], hi, lo);
  518. }
  519. return carry;
  520. }
  521. /* w := product of u (len n) and v (single word) */
  522. mpd_uint_t
  523. _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  524. mpd_uint_t v, mpd_uint_t b)
  525. {
  526. mpd_uint_t hi, lo;
  527. mpd_uint_t carry = 0;
  528. mpd_size_t i;
  529. assert(n > 0);
  530. for (i=0; i < n; i++) {
  531. _mpd_mul_words(&hi, &lo, u[i], v);
  532. lo = carry + lo;
  533. if (lo < carry) hi++;
  534. _mpd_div_words(&carry, &w[i], hi, lo, b);
  535. }
  536. return carry;
  537. }
  538. /*
  539. * Knuth, TAOCP Volume 2, 4.3.1, exercise 16:
  540. * w := quotient of u (len n) divided by a single word v
  541. */
  542. mpd_uint_t
  543. _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,
  544. mpd_uint_t v, mpd_uint_t b)
  545. {
  546. mpd_uint_t hi, lo;
  547. mpd_uint_t rem = 0;
  548. mpd_size_t i;
  549. assert(n > 0);
  550. for (i=n-1; i != MPD_SIZE_MAX; i--) {
  551. _mpd_mul_words(&hi, &lo, rem, b);
  552. lo = u[i] + lo;
  553. if (lo < u[i]) hi++;
  554. _mpd_div_words(&w[i], &rem, hi, lo, v);
  555. }
  556. return rem;
  557. }