clarify documentation
[heimdal.git] / lib / hcrypto / libtommath / bn_mp_div_2.c
blob7ee3e5b70fd7ca91634c35323d1d342dc3feca33
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_2_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 /* b = a/2 */
19 int mp_div_2(mp_int * a, mp_int * b)
21 int x, res, oldused;
23 /* copy */
24 if (b->alloc < a->used) {
25 if ((res = mp_grow (b, a->used)) != MP_OKAY) {
26 return res;
30 oldused = b->used;
31 b->used = a->used;
33 register mp_digit r, rr, *tmpa, *tmpb;
35 /* source alias */
36 tmpa = a->dp + b->used - 1;
38 /* dest alias */
39 tmpb = b->dp + b->used - 1;
41 /* carry */
42 r = 0;
43 for (x = b->used - 1; x >= 0; x--) {
44 /* get the carry for the next iteration */
45 rr = *tmpa & 1;
47 /* shift the current digit, add in carry and store */
48 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
50 /* forward carry to next iteration */
51 r = rr;
54 /* zero excess digits */
55 tmpb = b->dp + b->used;
56 for (x = b->used; x < oldused; x++) {
57 *tmpb++ = 0;
60 b->sign = a->sign;
61 mp_clamp (b);
62 return MP_OKAY;
64 #endif
66 /* $Source: /cvs/libtom/libtommath/bn_mp_div_2.c,v $ */
67 /* $Revision: 1.4 $ */
68 /* $Date: 2006/12/28 01:25:13 $ */