mpih-div.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
  38. * the NSIZE-DSIZE least significant quotient limbs at QP
  39. * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
  40. * non-zero, generate that many fraction bits and append them after the
  41. * other quotient limbs.
  42. * Return the most significant limb of the quotient, this is always 0 or 1.
  43. *
  44. * Preconditions:
  45. * 0. NSIZE >= DSIZE.
  46. * 1. The most significant bit of the divisor must be set.
  47. * 2. QP must either not overlap with the input operands at all, or
  48. * QP + DSIZE >= NP must hold true. (This means that it's
  49. * possible to put the quotient in the high part of NUM, right after the
  50. * remainder in NUM.
  51. * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
  52. */
  53. mpi_limb_t
  54. mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
  55. mpi_ptr_t np, mpi_size_t nsize, mpi_ptr_t dp, mpi_size_t dsize)
  56. {
  57. mpi_limb_t most_significant_q_limb = 0;
  58. switch (dsize) {
  59. case 0:
  60. /* We are asked to divide by zero, so go ahead and do it! (To make
  61. the compiler not remove this statement, return the value.) */
  62. /*
  63. * existing clients of this function have been modified
  64. * not to call it with dsize == 0, so this should not happen
  65. */
  66. return 1 / dsize;
  67. case 1:
  68. {
  69. mpi_size_t i;
  70. mpi_limb_t n1;
  71. mpi_limb_t d;
  72. d = dp[0];
  73. n1 = np[nsize - 1];
  74. if (n1 >= d) {
  75. n1 -= d;
  76. most_significant_q_limb = 1;
  77. }
  78. qp += qextra_limbs;
  79. for (i = nsize - 2; i >= 0; i--)
  80. udiv_qrnnd(qp[i], n1, n1, np[i], d);
  81. qp -= qextra_limbs;
  82. for (i = qextra_limbs - 1; i >= 0; i--)
  83. udiv_qrnnd(qp[i], n1, n1, 0, d);
  84. np[0] = n1;
  85. }
  86. break;
  87. case 2:
  88. {
  89. mpi_size_t i;
  90. mpi_limb_t n1, n0, n2;
  91. mpi_limb_t d1, d0;
  92. np += nsize - 2;
  93. d1 = dp[1];
  94. d0 = dp[0];
  95. n1 = np[1];
  96. n0 = np[0];
  97. if (n1 >= d1 && (n1 > d1 || n0 >= d0)) {
  98. sub_ddmmss(n1, n0, n1, n0, d1, d0);
  99. most_significant_q_limb = 1;
  100. }
  101. for (i = qextra_limbs + nsize - 2 - 1; i >= 0; i--) {
  102. mpi_limb_t q;
  103. mpi_limb_t r;
  104. if (i >= qextra_limbs)
  105. np--;
  106. else
  107. np[0] = 0;
  108. if (n1 == d1) {
  109. /* Q should be either 111..111 or 111..110. Need special
  110. * treatment of this rare case as normal division would
  111. * give overflow. */
  112. q = ~(mpi_limb_t) 0;
  113. r = n0 + d1;
  114. if (r < d1) { /* Carry in the addition? */
  115. add_ssaaaa(n1, n0, r - d0,
  116. np[0], 0, d0);
  117. qp[i] = q;
  118. continue;
  119. }
  120. n1 = d0 - (d0 != 0 ? 1 : 0);
  121. n0 = -d0;
  122. } else {
  123. udiv_qrnnd(q, r, n1, n0, d1);
  124. umul_ppmm(n1, n0, d0, q);
  125. }
  126. n2 = np[0];
  127. q_test:
  128. if (n1 > r || (n1 == r && n0 > n2)) {
  129. /* The estimated Q was too large. */
  130. q--;
  131. sub_ddmmss(n1, n0, n1, n0, 0, d0);
  132. r += d1;
  133. if (r >= d1) /* If not carry, test Q again. */
  134. goto q_test;
  135. }
  136. qp[i] = q;
  137. sub_ddmmss(n1, n0, r, n2, n1, n0);
  138. }
  139. np[1] = n1;
  140. np[0] = n0;
  141. }
  142. break;
  143. default:
  144. {
  145. mpi_size_t i;
  146. mpi_limb_t dX, d1, n0;
  147. np += nsize - dsize;
  148. dX = dp[dsize - 1];
  149. d1 = dp[dsize - 2];
  150. n0 = np[dsize - 1];
  151. if (n0 >= dX) {
  152. if (n0 > dX
  153. || mpihelp_cmp(np, dp, dsize - 1) >= 0) {
  154. mpihelp_sub_n(np, np, dp, dsize);
  155. n0 = np[dsize - 1];
  156. most_significant_q_limb = 1;
  157. }
  158. }
  159. for (i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
  160. mpi_limb_t q;
  161. mpi_limb_t n1, n2;
  162. mpi_limb_t cy_limb;
  163. if (i >= qextra_limbs) {
  164. np--;
  165. n2 = np[dsize];
  166. } else {
  167. n2 = np[dsize - 1];
  168. MPN_COPY_DECR(np + 1, np, dsize - 1);
  169. np[0] = 0;
  170. }
  171. if (n0 == dX) {
  172. /* This might over-estimate q, but it's probably not worth
  173. * the extra code here to find out. */
  174. q = ~(mpi_limb_t) 0;
  175. } else {
  176. mpi_limb_t r;
  177. udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
  178. umul_ppmm(n1, n0, d1, q);
  179. while (n1 > r
  180. || (n1 == r
  181. && n0 > np[dsize - 2])) {
  182. q--;
  183. r += dX;
  184. if (r < dX) /* I.e. "carry in previous addition?" */
  185. break;
  186. n1 -= n0 < d1;
  187. n0 -= d1;
  188. }
  189. }
  190. /* Possible optimization: We already have (q * n0) and (1 * n1)
  191. * after the calculation of q. Taking advantage of that, we
  192. * could make this loop make two iterations less. */
  193. cy_limb = mpihelp_submul_1(np, dp, dsize, q);
  194. if (n2 != cy_limb) {
  195. mpihelp_add_n(np, np, dp, dsize);
  196. q--;
  197. }
  198. qp[i] = q;
  199. n0 = np[dsize - 1];
  200. }
  201. }
  202. }
  203. return most_significant_q_limb;
  204. }