1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
\r
3 * LibTomMath is library that provides for multiple-precision
\r
4 * integer arithmetic as well as number theoretic functionality.
\r
6 * The library is designed directly after the MPI library by
\r
7 * Michael Fromberger but has been written from scratch with
\r
8 * additional optimizations in place.
\r
10 * The library is free for all purposes without any express
\r
11 * guarantee it works.
\r
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
\r
15 #include <tommath.h>
\r
17 /* divide by three (based on routine from MPI and the GMP manual) */
\r
19 mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
\r
26 /* b = 2**DIGIT_BIT / 3 */
\r
27 b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
\r
29 if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
\r
36 for (ix = a->used - 1; ix >= 0; ix--) {
\r
37 w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
\r
40 t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
\r
41 w -= (t << ((mp_word)1)) + t;
\r
49 q.dp[ix] = (mp_digit)t;
\r