mpih-div.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* mpihelp-div.c - MPI helper functions
  2. * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
  3. * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
  4. *
  5. * This file is part of GnuPG.
  6. *
  7. * GnuPG is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GnuPG is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. * Note: This code is heavily based on the GNU MP Library.
  22. * Actually it's the same code with only minor changes in the
  23. * way the data is stored; this is to support the abstraction
  24. * of an optional secure memory allocation which may be used
  25. * to avoid revealing of sensitive data due to paging etc.
  26. * The GNU MP Library itself is published under the LGPL;
  27. * however I decided to publish this code under the plain GPL.
  28. */
  29. #include "mpi-internal.h"
  30. #include "longlong.h"
  31. #ifndef UMUL_TIME
  32. #define UMUL_TIME 1
  33. #endif
  34. #ifndef UDIV_TIME
  35. #define UDIV_TIME UMUL_TIME
  36. #endif
  37. /* FIXME: We should be using invert_limb (or invert_normalized_limb)
  38. * here (not udiv_qrnnd).
  39. */
  40. mpi_limb_t
  41. mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  42. mpi_limb_t divisor_limb)
  43. {
  44. mpi_size_t i;
  45. mpi_limb_t n1, n0, r;
  46. int dummy;
  47. /* Botch: Should this be handled at all? Rely on callers? */
  48. if (!dividend_size)
  49. return 0;
  50. /* If multiplication is much faster than division, and the
  51. * dividend is large, pre-invert the divisor, and use
  52. * only multiplications in the inner loop.
  53. *
  54. * This test should be read:
  55. * Does it ever help to use udiv_qrnnd_preinv?
  56. * && Does what we save compensate for the inversion overhead?
  57. */
  58. if (UDIV_TIME > (2 * UMUL_TIME + 6)
  59. && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME) {
  60. int normalization_steps;
  61. count_leading_zeros(normalization_steps, divisor_limb);
  62. if (normalization_steps) {
  63. mpi_limb_t divisor_limb_inverted;
  64. divisor_limb <<= normalization_steps;
  65. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  66. * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  67. * most significant bit (with weight 2**N) implicit.
  68. *
  69. * Special case for DIVISOR_LIMB == 100...000.
  70. */
  71. if (!(divisor_limb << 1))
  72. divisor_limb_inverted = ~(mpi_limb_t) 0;
  73. else
  74. udiv_qrnnd(divisor_limb_inverted, dummy,
  75. -divisor_limb, 0, divisor_limb);
  76. n1 = dividend_ptr[dividend_size - 1];
  77. r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
  78. /* Possible optimization:
  79. * if (r == 0
  80. * && divisor_limb > ((n1 << normalization_steps)
  81. * | (dividend_ptr[dividend_size - 2] >> ...)))
  82. * ...one division less...
  83. */
  84. for (i = dividend_size - 2; i >= 0; i--) {
  85. n0 = dividend_ptr[i];
  86. UDIV_QRNND_PREINV(dummy, r, r,
  87. ((n1 << normalization_steps)
  88. | (n0 >>
  89. (BITS_PER_MPI_LIMB -
  90. normalization_steps))),
  91. divisor_limb,
  92. divisor_limb_inverted);
  93. n1 = n0;
  94. }
  95. UDIV_QRNND_PREINV(dummy, r, r,
  96. n1 << normalization_steps,
  97. divisor_limb, divisor_limb_inverted);
  98. return r >> normalization_steps;
  99. } else {
  100. mpi_limb_t divisor_limb_inverted;
  101. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  102. * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  103. * most significant bit (with weight 2**N) implicit.
  104. *
  105. * Special case for DIVISOR_LIMB == 100...000.
  106. */
  107. if (!(divisor_limb << 1))
  108. divisor_limb_inverted = ~(mpi_limb_t) 0;
  109. else
  110. udiv_qrnnd(divisor_limb_inverted, dummy,
  111. -divisor_limb, 0, divisor_limb);
  112. i = dividend_size - 1;
  113. r = dividend_ptr[i];
  114. if (r >= divisor_limb)
  115. r = 0;
  116. else
  117. i--;
  118. for (; i >= 0; i--) {
  119. n0 = dividend_ptr[i];
  120. UDIV_QRNND_PREINV(dummy, r, r,
  121. n0, divisor_limb,
  122. divisor_limb_inverted);
  123. }
  124. return r;
  125. }
  126. } else {
  127. if (UDIV_NEEDS_NORMALIZATION) {
  128. int normalization_steps;
  129. count_leading_zeros(normalization_steps, divisor_limb);
  130. if (normalization_steps) {
  131. divisor_limb <<= normalization_steps;
  132. n1 = dividend_ptr[dividend_size - 1];
  133. r = n1 >> (BITS_PER_MPI_LIMB -
  134. normalization_steps);
  135. /* Possible optimization:
  136. * if (r == 0
  137. * && divisor_limb > ((n1 << normalization_steps)
  138. * | (dividend_ptr[dividend_size - 2] >> ...)))
  139. * ...one division less...
  140. */
  141. for (i = dividend_size - 2; i >= 0; i--) {
  142. n0 = dividend_ptr[i];
  143. udiv_qrnnd(dummy, r, r,
  144. ((n1 << normalization_steps)
  145. | (n0 >>
  146. (BITS_PER_MPI_LIMB -
  147. normalization_steps))),
  148. divisor_limb);
  149. n1 = n0;
  150. }
  151. udiv_qrnnd(dummy, r, r,
  152. n1 << normalization_steps,
  153. divisor_limb);
  154. return r >> normalization_steps;
  155. }
  156. }
  157. /* No normalization needed, either because udiv_qrnnd doesn't require
  158. * it, or because DIVISOR_LIMB is already normalized. */
  159. i = dividend_size - 1;
  160. r = dividend_ptr[i];
  161. if (r >= divisor_limb)
  162. r = 0;
  163. else
  164. i--;
  165. for (; i >= 0; i--) {
  166. n0 = dividend_ptr[i];
  167. udiv_qrnnd(dummy, r, r, n0, divisor_limb);
  168. }
  169. return r;
  170. }
  171. }
  172. /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
  173. * the NSIZE-DSIZE least significant quotient limbs at QP
  174. * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
  175. * non-zero, generate that many fraction bits and append them after the
  176. * other quotient limbs.
  177. * Return the most significant limb of the quotient, this is always 0 or 1.
  178. *
  179. * Preconditions:
  180. * 0. NSIZE >= DSIZE.
  181. * 1. The most significant bit of the divisor must be set.
  182. * 2. QP must either not overlap with the input operands at all, or
  183. * QP + DSIZE >= NP must hold true. (This means that it's
  184. * possible to put the quotient in the high part of NUM, right after the
  185. * remainder in NUM.
  186. * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
  187. */
  188. mpi_limb_t
  189. mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
  190. mpi_ptr_t np, mpi_size_t nsize, mpi_ptr_t dp, mpi_size_t dsize)
  191. {
  192. mpi_limb_t most_significant_q_limb = 0;
  193. switch (dsize) {
  194. case 0:
  195. /* We are asked to divide by zero, so go ahead and do it! (To make
  196. the compiler not remove this statement, return the value.) */
  197. return 1 / dsize;
  198. case 1:
  199. {
  200. mpi_size_t i;
  201. mpi_limb_t n1;
  202. mpi_limb_t d;
  203. d = dp[0];
  204. n1 = np[nsize - 1];
  205. if (n1 >= d) {
  206. n1 -= d;
  207. most_significant_q_limb = 1;
  208. }
  209. qp += qextra_limbs;
  210. for (i = nsize - 2; i >= 0; i--)
  211. udiv_qrnnd(qp[i], n1, n1, np[i], d);
  212. qp -= qextra_limbs;
  213. for (i = qextra_limbs - 1; i >= 0; i--)
  214. udiv_qrnnd(qp[i], n1, n1, 0, d);
  215. np[0] = n1;
  216. }
  217. break;
  218. case 2:
  219. {
  220. mpi_size_t i;
  221. mpi_limb_t n1, n0, n2;
  222. mpi_limb_t d1, d0;
  223. np += nsize - 2;
  224. d1 = dp[1];
  225. d0 = dp[0];
  226. n1 = np[1];
  227. n0 = np[0];
  228. if (n1 >= d1 && (n1 > d1 || n0 >= d0)) {
  229. sub_ddmmss(n1, n0, n1, n0, d1, d0);
  230. most_significant_q_limb = 1;
  231. }
  232. for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--) {
  233. mpi_limb_t q;
  234. mpi_limb_t r;
  235. if (i >= qextra_limbs)
  236. np--;
  237. else
  238. np[0] = 0;
  239. if (n1 == d1) {
  240. /* Q should be either 111..111 or 111..110. Need special
  241. * treatment of this rare case as normal division would
  242. * give overflow. */
  243. q = ~(mpi_limb_t) 0;
  244. r = n0 + d1;
  245. if (r < d1) { /* Carry in the addition? */
  246. add_ssaaaa(n1, n0, r - d0,
  247. np[0], 0, d0);
  248. qp[i] = q;
  249. continue;
  250. }
  251. n1 = d0 - (d0 != 0 ? 1 : 0);
  252. n0 = -d0;
  253. } else {
  254. udiv_qrnnd(q, r, n1, n0, d1);
  255. umul_ppmm(n1, n0, d0, q);
  256. }
  257. n2 = np[0];
  258. q_test:
  259. if (n1 > r || (n1 == r && n0 > n2)) {
  260. /* The estimated Q was too large. */
  261. q--;
  262. sub_ddmmss(n1, n0, n1, n0, 0, d0);
  263. r += d1;
  264. if (r >= d1) /* If not carry, test Q again. */
  265. goto q_test;
  266. }
  267. qp[i] = q;
  268. sub_ddmmss(n1, n0, r, n2, n1, n0);
  269. }
  270. np[1] = n1;
  271. np[0] = n0;
  272. }
  273. break;
  274. default:
  275. {
  276. mpi_size_t i;
  277. mpi_limb_t dX, d1, n0;
  278. np += nsize - dsize;
  279. dX = dp[dsize - 1];
  280. d1 = dp[dsize - 2];
  281. n0 = np[dsize - 1];
  282. if (n0 >= dX) {
  283. if (n0 > dX
  284. || mpihelp_cmp(np, dp, dsize - 1) >= 0) {
  285. mpihelp_sub_n(np, np, dp, dsize);
  286. n0 = np[dsize - 1];
  287. most_significant_q_limb = 1;
  288. }
  289. }
  290. for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
  291. mpi_limb_t q;
  292. mpi_limb_t n1, n2;
  293. mpi_limb_t cy_limb;
  294. if (i >= qextra_limbs) {
  295. np--;
  296. n2 = np[dsize];
  297. } else {
  298. n2 = np[dsize - 1];
  299. MPN_COPY_DECR(np + 1, np, dsize - 1);
  300. np[0] = 0;
  301. }
  302. if (n0 == dX) {
  303. /* This might over-estimate q, but it's probably not worth
  304. * the extra code here to find out. */
  305. q = ~(mpi_limb_t) 0;
  306. } else {
  307. mpi_limb_t r;
  308. udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
  309. umul_ppmm(n1, n0, d1, q);
  310. while (n1 > r
  311. || (n1 == r
  312. && n0 > np[dsize - 2])) {
  313. q--;
  314. r += dX;
  315. if (r < dX) /* I.e. "carry in previous addition?" */
  316. break;
  317. n1 -= n0 < d1;
  318. n0 -= d1;
  319. }
  320. }
  321. /* Possible optimization: We already have (q * n0) and (1 * n1)
  322. * after the calculation of q. Taking advantage of that, we
  323. * could make this loop make two iterations less. */
  324. cy_limb = mpihelp_submul_1(np, dp, dsize, q);
  325. if (n2 != cy_limb) {
  326. mpihelp_add_n(np, np, dp, dsize);
  327. q--;
  328. }
  329. qp[i] = q;
  330. n0 = np[dsize - 1];
  331. }
  332. }
  333. }
  334. return most_significant_q_limb;
  335. }
  336. /****************
  337. * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
  338. * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
  339. * Return the single-limb remainder.
  340. * There are no constraints on the value of the divisor.
  341. *
  342. * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
  343. */
  344. mpi_limb_t
  345. mpihelp_divmod_1(mpi_ptr_t quot_ptr,
  346. mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  347. mpi_limb_t divisor_limb)
  348. {
  349. mpi_size_t i;
  350. mpi_limb_t n1, n0, r;
  351. int dummy;
  352. if (!dividend_size)
  353. return 0;
  354. /* If multiplication is much faster than division, and the
  355. * dividend is large, pre-invert the divisor, and use
  356. * only multiplications in the inner loop.
  357. *
  358. * This test should be read:
  359. * Does it ever help to use udiv_qrnnd_preinv?
  360. * && Does what we save compensate for the inversion overhead?
  361. */
  362. if (UDIV_TIME > (2 * UMUL_TIME + 6)
  363. && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME) {
  364. int normalization_steps;
  365. count_leading_zeros(normalization_steps, divisor_limb);
  366. if (normalization_steps) {
  367. mpi_limb_t divisor_limb_inverted;
  368. divisor_limb <<= normalization_steps;
  369. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  370. * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  371. * most significant bit (with weight 2**N) implicit.
  372. */
  373. /* Special case for DIVISOR_LIMB == 100...000. */
  374. if (!(divisor_limb << 1))
  375. divisor_limb_inverted = ~(mpi_limb_t) 0;
  376. else
  377. udiv_qrnnd(divisor_limb_inverted, dummy,
  378. -divisor_limb, 0, divisor_limb);
  379. n1 = dividend_ptr[dividend_size - 1];
  380. r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
  381. /* Possible optimization:
  382. * if (r == 0
  383. * && divisor_limb > ((n1 << normalization_steps)
  384. * | (dividend_ptr[dividend_size - 2] >> ...)))
  385. * ...one division less...
  386. */
  387. for (i = dividend_size - 2; i >= 0; i--) {
  388. n0 = dividend_ptr[i];
  389. UDIV_QRNND_PREINV(quot_ptr[i + 1], r, r,
  390. ((n1 << normalization_steps)
  391. | (n0 >>
  392. (BITS_PER_MPI_LIMB -
  393. normalization_steps))),
  394. divisor_limb,
  395. divisor_limb_inverted);
  396. n1 = n0;
  397. }
  398. UDIV_QRNND_PREINV(quot_ptr[0], r, r,
  399. n1 << normalization_steps,
  400. divisor_limb, divisor_limb_inverted);
  401. return r >> normalization_steps;
  402. } else {
  403. mpi_limb_t divisor_limb_inverted;
  404. /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
  405. * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
  406. * most significant bit (with weight 2**N) implicit.
  407. */
  408. /* Special case for DIVISOR_LIMB == 100...000. */
  409. if (!(divisor_limb << 1))
  410. divisor_limb_inverted = ~(mpi_limb_t) 0;
  411. else
  412. udiv_qrnnd(divisor_limb_inverted, dummy,
  413. -divisor_limb, 0, divisor_limb);
  414. i = dividend_size - 1;
  415. r = dividend_ptr[i];
  416. if (r >= divisor_limb)
  417. r = 0;
  418. else
  419. quot_ptr[i--] = 0;
  420. for (; i >= 0; i--) {
  421. n0 = dividend_ptr[i];
  422. UDIV_QRNND_PREINV(quot_ptr[i], r, r,
  423. n0, divisor_limb,
  424. divisor_limb_inverted);
  425. }
  426. return r;
  427. }
  428. } else {
  429. if (UDIV_NEEDS_NORMALIZATION) {
  430. int normalization_steps;
  431. count_leading_zeros(normalization_steps, divisor_limb);
  432. if (normalization_steps) {
  433. divisor_limb <<= normalization_steps;
  434. n1 = dividend_ptr[dividend_size - 1];
  435. r = n1 >> (BITS_PER_MPI_LIMB -
  436. normalization_steps);
  437. /* Possible optimization:
  438. * if (r == 0
  439. * && divisor_limb > ((n1 << normalization_steps)
  440. * | (dividend_ptr[dividend_size - 2] >> ...)))
  441. * ...one division less...
  442. */
  443. for (i = dividend_size - 2; i >= 0; i--) {
  444. n0 = dividend_ptr[i];
  445. udiv_qrnnd(quot_ptr[i + 1], r, r,
  446. ((n1 << normalization_steps)
  447. | (n0 >>
  448. (BITS_PER_MPI_LIMB -
  449. normalization_steps))),
  450. divisor_limb);
  451. n1 = n0;
  452. }
  453. udiv_qrnnd(quot_ptr[0], r, r,
  454. n1 << normalization_steps,
  455. divisor_limb);
  456. return r >> normalization_steps;
  457. }
  458. }
  459. /* No normalization needed, either because udiv_qrnnd doesn't require
  460. * it, or because DIVISOR_LIMB is already normalized. */
  461. i = dividend_size - 1;
  462. r = dividend_ptr[i];
  463. if (r >= divisor_limb)
  464. r = 0;
  465. else
  466. quot_ptr[i--] = 0;
  467. for (; i >= 0; i--) {
  468. n0 = dividend_ptr[i];
  469. udiv_qrnnd(quot_ptr[i], r, r, n0, divisor_limb);
  470. }
  471. return r;
  472. }
  473. }