basearith.c 17 KB

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