195
195
static void s_fake(mp_int z, int value, mp_digit vbuf[]);
197
197
/* Compare two runs of digits of given length, returns <0, 0, >0 */
198
static int s_cdig(mp_digit * da, mp_digit * db, mp_size len);
198
static int s_cdig(mp_digit *da, mp_digit *db, mp_size len);
200
200
/* Pack the unsigned digits of v into array t */
201
201
static int s_vpack(int v, mp_digit t[]);
209
209
/* Unsigned magnitude addition; assumes dc is big enough.
210
210
Carry out is returned (no memory allocated). */
211
static mp_digit s_uadd(mp_digit * da, mp_digit * db, mp_digit * dc,
211
static mp_digit s_uadd(mp_digit *da, mp_digit *db, mp_digit *dc,
212
212
mp_size size_a, mp_size size_b);
214
214
/* Unsigned magnitude subtraction. Assumes dc is big enough. */
215
static void s_usub(mp_digit * da, mp_digit * db, mp_digit * dc,
215
static void s_usub(mp_digit *da, mp_digit *db, mp_digit *dc,
216
216
mp_size size_a, mp_size size_b);
218
218
/* Unsigned recursive multiplication. Assumes dc is big enough. */
219
static int s_kmul(mp_digit * da, mp_digit * db, mp_digit * dc,
219
static int s_kmul(mp_digit *da, mp_digit *db, mp_digit *dc,
220
220
mp_size size_a, mp_size size_b);
222
222
/* Unsigned magnitude multiplication. Assumes dc is big enough. */
223
static void s_umul(mp_digit * da, mp_digit * db, mp_digit * dc,
223
static void s_umul(mp_digit *da, mp_digit *db, mp_digit *dc,
224
224
mp_size size_a, mp_size size_b);
226
226
/* Unsigned recursive squaring. Assumes dc is big enough. */
227
static int s_ksqr(mp_digit * da, mp_digit * dc, mp_size size_a);
227
static int s_ksqr(mp_digit *da, mp_digit *dc, mp_size size_a);
229
229
/* Unsigned magnitude squaring. Assumes dc is big enough. */
230
static void s_usqr(mp_digit * da, mp_digit * dc, mp_size size_a);
230
static void s_usqr(mp_digit *da, mp_digit *dc, mp_size size_a);
232
232
/* Single digit addition. Assumes a is big enough. */
233
233
static void s_dadd(mp_int a, mp_digit b);
236
236
static void s_dmul(mp_int a, mp_digit b);
238
238
/* Single digit multiplication on buffers; assumes dc is big enough. */
239
static void s_dbmul(mp_digit * da, mp_digit b, mp_digit * dc,
239
static void s_dbmul(mp_digit *da, mp_digit b, mp_digit *dc,
242
242
/* Single digit division. Replaces a with the quotient,
311
311
/* Dump a representation of the mp_int to standard output */
312
312
void s_print(char *tag, mp_int z);
313
void s_print_buf(char *tag, mp_digit * buf, mp_size num);
313
void s_print_buf(char *tag, mp_digit *buf, mp_size num);
316
316
/* {{{ get_default_precision() */
2294
2294
/* {{{ s_realloc(old, num) */
2296
2296
static mp_digit *
2297
s_realloc(mp_digit * old, mp_size num)
2297
s_realloc(mp_digit *old, mp_size num)
2299
2299
mp_digit *new = px_realloc(old, num * sizeof(mp_digit));
2375
2375
/* {{{ s_cdig(da, db, len) */
2378
s_cdig(mp_digit * da, mp_digit * db, mp_size len)
2378
s_cdig(mp_digit *da, mp_digit *db, mp_size len)
2380
2380
mp_digit *dat = da + len - 1,
2381
2381
*dbt = db + len - 1;
2460
2460
/* {{{ s_uadd(da, db, dc, size_a, size_b) */
2462
2462
static mp_digit
2463
s_uadd(mp_digit * da, mp_digit * db, mp_digit * dc,
2463
s_uadd(mp_digit *da, mp_digit *db, mp_digit *dc,
2464
2464
mp_size size_a, mp_size size_b)
2476
2476
/* Add corresponding digits until the shorter number runs out */
2477
2477
for (pos = 0; pos < size_b; ++pos, ++da, ++db, ++dc)
2479
w = w + (mp_word) * da + (mp_word) * db;
2479
w = w + (mp_word) *da + (mp_word) *db;
2480
2480
*dc = LOWER_HALF(w);
2481
2481
w = UPPER_HALF(w);
2499
2499
/* {{{ s_usub(da, db, dc, size_a, size_b) */
2502
s_usub(mp_digit * da, mp_digit * db, mp_digit * dc,
2502
s_usub(mp_digit *da, mp_digit *db, mp_digit *dc,
2503
2503
mp_size size_a, mp_size size_b)
2512
2512
for (pos = 0; pos < size_b; ++pos, ++da, ++db, ++dc)
2514
2514
w = ((mp_word) MP_DIGIT_MAX + 1 + /* MP_RADIX */
2515
(mp_word) * da) - w - (mp_word) * db;
2515
(mp_word) *da) - w - (mp_word) *db;
2517
2517
*dc = LOWER_HALF(w);
2518
2518
w = (UPPER_HALF(w) == 0);
2537
2537
/* {{{ s_kmul(da, db, dc, size_a, size_b) */
2540
s_kmul(mp_digit * da, mp_digit * db, mp_digit * dc,
2540
s_kmul(mp_digit *da, mp_digit *db, mp_digit *dc,
2541
2541
mp_size size_a, mp_size size_b)
2543
2543
mp_size bot_size;
2638
2638
/* {{{ s_umul(da, db, dc, size_a, size_b) */
2641
s_umul(mp_digit * da, mp_digit * db, mp_digit * dc,
2641
s_umul(mp_digit *da, mp_digit *db, mp_digit *dc,
2642
2642
mp_size size_a, mp_size size_b)
2671
2671
/* {{{ s_ksqr(da, dc, size_a) */
2674
s_ksqr(mp_digit * da, mp_digit * dc, mp_size size_a)
2674
s_ksqr(mp_digit *da, mp_digit *dc, mp_size size_a)
2676
2676
if (multiply_threshold && size_a > multiply_threshold)
2736
2736
/* {{{ s_usqr(da, dc, size_a) */
2739
s_usqr(mp_digit * da, mp_digit * dc, mp_size size_a)
2739
s_usqr(mp_digit *da, mp_digit *dc, mp_size size_a)
2760
2760
for (j = i + 1; j < size_a; ++j, ++dat, ++dct)
2762
mp_word t = (mp_word) * da * (mp_word) * dat;
2763
mp_word u = w + (mp_word) * dct,
2762
mp_word t = (mp_word) *da * (mp_word) *dat;
2763
mp_word u = w + (mp_word) *dct,
2766
2766
/* Check if doubling t will overflow a word */
2858
2858
/* {{{ s_dbmul(da, b, dc, size_a) */
2861
s_dbmul(mp_digit * da, mp_digit b, mp_digit * dc, mp_size size_a)
2861
s_dbmul(mp_digit *da, mp_digit b, mp_digit *dc, mp_size size_a)
2865
2865
while (size_a > 0)
2867
w = (mp_word) * da++ * (mp_word) b + w;
2867
w = (mp_word) *da++ * (mp_word) b + w;
2869
2869
*dc++ = LOWER_HALF(w);
2870
2870
w = UPPER_HALF(w);
3086
3086
for (pos = 0, zp = MP_DIGITS(z); pos < tdig; ++pos, ++zp)
3088
w = ((mp_word) MP_DIGIT_MAX + 1) - w - (mp_word) * zp;
3088
w = ((mp_word) MP_DIGIT_MAX + 1) - w - (mp_word) *zp;
3090
3090
*zp = LOWER_HALF(w);
3091
3091
w = UPPER_HALF(w) ? 0 : 1;
3094
w = ((mp_word) MP_DIGIT_MAX + 1 + hi) - w - (mp_word) * zp;
3094
w = ((mp_word) MP_DIGIT_MAX + 1 + hi) - w - (mp_word) *zp;
3095
3095
*zp = LOWER_HALF(w);
3097
3097
assert(UPPER_HALF(w) != 0); /* no borrow out should be possible */